Open
Description
- Ansible modules for network uses APIs, which means "connection: local" must be set in the playbooks, which means all the python library dependencies has be installed locally.
- This isn't working using st2 ansible pack despite I pip installed the dependencies locally.
- Broadly, how the manage the st2 virtual environnement (python libraries, ansible versions ...)
Install Ansible Roles from galaxy
mab@mab-infra:~/automation/ansible$ sudo ansible-galaxy list
mab@mab-infra:~/automation/ansible$ sudo ansible-galaxy install Juniper.junos
- downloading role 'junos', owned by Juniper
- downloading role from https://github.com/Juniper/ansible-junos-stdlib/archive/1.4.2.tar.gz
- extracting Juniper.junos to /etc/ansible/roles/Juniper.junos
- Juniper.junos (1.4.2) was installed successfully
mab@mab-infra:~/automation/ansible$ sudo ansible-galaxy list
- Juniper.junos, 1.4.2
Install python libraries required for the role:
mab@mab-infra:~/automation/ansible$ pip list | grep junos
junos-eznc (2.1.1)
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
mab@mab-infra:~/automation/ansible$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from jnpr.junos import Device
>>> dir()
['Device', '__builtins__', '__doc__', '__name__', '__package__']
>>> exit()
Ansible configuration file:
mab@mab-infra:~/automation/ansible$ more /home/mab/automation/ansible/ansible.cfg
[defaults]
inventory = hosts
roles_path = /etc/ansible/roles:./
deprecation_warnings=False
Ansible version:
mab@mab-infra:~/automation/ansible$ ansible --version
ansible 2.3.0.0
config file = /home/mab/automation/ansible/ansible.cfg
configured module search path = Default w/o overrides
python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]
Ansible playbook details:
- The junos_get_facts module requires jnpr.junos python library as shown here: https://github.com/Juniper/ansible-junos-stdlib/blob/master/library/junos_get_facts#L163
- The playbook uses "connection: local". so the jnpr.junos python library has to be installed locally (hence the above pip and python commands)
mab@mab-infra:~/automation/ansible$ cat junos_get_facts/pb.yml
---
- name: create inventory directory
hosts: localhost
gather_facts: no
tasks:
- name: create inventory directory
file: path={{playbook_dir}}/inventory state=directory
- name: Get Facts
hosts: vmx
roles:
- Juniper.junos
connection: local
gather_facts: no
tasks:
- name: remove host from inventory directory
file: path={{playbook_dir}}/inventory/{{inventory_hostname}}.conf state=absent
- name: Retrieve information from devices running Junos
junos_get_facts:
host={{ credentials.host }}
user={{ credentials.username }}
passwd={{ credentials.password }}
savedir={{playbook_dir}}/inventory
register: junos
- name: Print some facts
debug: msg="device {{junos["facts"]["hostname"]}} runs version {{junos.facts.version}}"
when: junos.facts.version != "12.3R11.2"
Ansible variables:
mab@mab-infra:~/automation/ansible$ cat group_vars/vmx/credentials.yml
---
credentials:
host: "{{ junos_host }}"
ssh_key: /home/mab/.ssh/id_rsa
username: mab
password: mab123
Ansible playbook execution using ansible-playbook command (OK):
mab@mab-infra:~/automation/ansible$ sudo ansible-playbook junos_get_facts/pb.yml
PLAY [create inventory directory] **************************************************************************************************************************************************************************
TASK [create inventory directory] **************************************************************************************************************************************************************************
ok: [localhost]
PLAY [Get Facts] *******************************************************************************************************************************************************************************************
TASK [remove host from inventory directory] ****************************************************************************************************************************************************************
ok: [vmx1]
ok: [vmx2]
TASK [Retrieve information from devices running Junos] *****************************************************************************************************************************************************
ok: [vmx2]
ok: [vmx1]
TASK [Print some facts] ************************************************************************************************************************************************************************************
ok: [vmx2] => {
"changed": false,
"msg": "device vmx2 runs version 14.1R4.9"
}
ok: [vmx1] => {
"changed": false,
"msg": "device vmx1 runs version 14.1R4.9"
}
PLAY RECAP *************************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
vmx1 : ok=3 changed=0 unreachable=0 failed=0
vmx2 : ok=3 changed=0 unreachable=0 failed=0
Ansible pack for st2 installed:
mab@mab-infra:~/automation/ansible$ sudo st2 pack get ansible
+-------------+--------------------------------------------------+
| Property | Value |
+-------------+--------------------------------------------------+
| name | ansible |
| version | 0.4.0 |
| author | StackStorm, Inc. |
| email | [email protected] |
| keywords | [ |
| | "ansible", |
| | "cfg management", |
| | "configuration management" |
| | ] |
| description | st2 content pack containing ansible integrations |
+-------------+--------------------------------------------------+
Verify the details st2 ansible pack is going to use:
mab@mab-infra:~/automation/ansible$ sudo st2 run ansible.playbook playbook=/home/mab/automation/ansible/junos_get_facts/pb.yml cwd=/home/mab/automation/ansible/ version=true
.
id: 590f2f8e7cae220956aeb02d
status: succeeded
parameters:
cwd: /home/mab/automation/ansible/
playbook: /home/mab/automation/ansible/junos_get_facts/pb.yml
version: true
result:
failed: false
return_code: 0
stderr: ''
stdout: "ansible-playbook 2.3.0.0
config file = /home/mab/automation/ansible/ansible.cfg
configured module search path = Default w/o overrides
python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]"
succeeded: true
Ansible playbook execution using st2 Ansible pack (KO):
- See error displayed: ImportError: No module named jnpr.junos
- Despite jnpr.junos python library correctly installed as proved above
mab@mab-infra:~/automation/ansible$ sudo st2 run ansible.playbook playbook=/home/mab/automation/ansible/junos_get_facts/pb.yml cwd=/home/mab/automation/ansible/
.
id: 590f2fd47cae220956aeb030
status: failed
parameters:
cwd: /home/mab/automation/ansible/
playbook: /home/mab/automation/ansible/junos_get_facts/pb.yml
result:
failed: true
return_code: 2
stderr: Executed command "/opt/stackstorm/virtualenvs/ansible/bin/ansible-playbook /home/mab/automation/ansible/junos_get_facts/pb.yml"
stdout: "
PLAY [create inventory directory] **********************************************
TASK [create inventory directory] **********************************************
ok: [localhost]
PLAY [Get Facts] ***************************************************************
TASK [remove host from inventory directory] ************************************
ok: [vmx1]
ok: [vmx2]
TASK [Retrieve information from devices running Junos] *************************
fatal: [vmx2]: FAILED! => {"changed": false, "failed": true, "msg": "ImportError: No module named jnpr.junos"}
fatal: [vmx1]: FAILED! => {"changed": false, "failed": true, "msg": "ImportError: No module named jnpr.junos"}
to retry, use: --limit @/home/mab/automation/ansible/junos_get_facts/pb.retry
PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
vmx1 : ok=1 changed=0 unreachable=0 failed=1
vmx2 : ok=1 changed=0 unreachable=0 failed=1
"
succeeded: false
Same command in verbose mode:
mab@mab-infra:~/automation/ansible$ sudo st2 run ansible.playbook playbook=/home/mab/automation/ansible/junos_get_facts/pb.yml cwd=/home/mab/automation/ansible/ verbose=vvvv
.
id: 590f2fe17cae220956aeb033
status: failed
parameters:
cwd: /home/mab/automation/ansible/
playbook: /home/mab/automation/ansible/junos_get_facts/pb.yml
verbose: vvvv
result:
failed: true
return_code: 2
stderr: Executed command "/opt/stackstorm/virtualenvs/ansible/bin/ansible-playbook -vvvv /home/mab/automation/ansible/junos_get_facts/pb.yml"
stdout: "Using /home/mab/automation/ansible/ansible.cfg as config file
Loading callback plugin default of type stdout, v2.0 from /opt/stackstorm/virtualenvs/ansible/lib/python2.7/site-packages/ansible/plugins/callback/__init__.pyc
Loading callback plugin jsnapy of type aggregate, v2.0 from /opt/stackstorm/virtualenvs/ansible/lib/python2.7/site-packages/ansible/plugins/callback/__init__.pyc
PLAYBOOK: pb.yml ***************************************************************
2 plays in /home/mab/automation/ansible/junos_get_facts/pb.yml
PLAY [create inventory directory] **********************************************
META: ran handlers
TASK [create inventory directory] **********************************************
task path: /home/mab/automation/ansible/junos_get_facts/pb.yml:8
Using module file /opt/stackstorm/virtualenvs/ansible/lib/python2.7/site-packages/ansible/modules/files/file.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root
<127.0.0.1> EXEC /bin/sh -c 'echo ~ && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1494167521.98-137850814807689 `" && echo ansible-tmp-1494167521.98-137850814807689="` echo /root/.ansible/tmp/ansible-tmp-1494167521.98-137850814807689 `" ) && sleep 0'
<127.0.0.1> PUT /tmp/tmpDz9pYO TO /root/.ansible/tmp/ansible-tmp-1494167521.98-137850814807689/file.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1494167521.98-137850814807689/ /root/.ansible/tmp/ansible-tmp-1494167521.98-137850814807689/file.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/opt/stackstorm/virtualenvs/ansible/bin/python /root/.ansible/tmp/ansible-tmp-1494167521.98-137850814807689/file.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1494167521.98-137850814807689/" > /dev/null 2>&1 && sleep 0'
ok: [localhost] => {
"changed": false,
"diff": {
"after": {
"path": "/home/mab/automation/ansible/junos_get_facts/inventory"
},
"before": {
"path": "/home/mab/automation/ansible/junos_get_facts/inventory"
}
},
"gid": 0,
"group": "root",
"invocation": {
"module_args": {
"attributes": null,
"backup": null,
"content": null,
"delimiter": null,
"diff_peek": null,
"directory_mode": null,
"follow": false,
"force": false,
"group": null,
"mode": null,
"original_basename": null,
"owner": null,
"path": "/home/mab/automation/ansible/junos_get_facts/inventory",
"recurse": false,
"regexp": null,
"remote_src": null,
"selevel": null,
"serole": null,
"setype": null,
"seuser": null,
"src": null,
"state": "directory",
"unsafe_writes": null,
"validate": null
}
},
"mode": "0755",
"owner": "root",
"path": "/home/mab/automation/ansible/junos_get_facts/inventory",
"size": 4096,
"state": "directory",
"uid": 0
}
META: ran handlers
META: ran handlers
PLAY [Get Facts] ***************************************************************
META: ran handlers
TASK [remove host from inventory directory] ************************************
task path: /home/mab/automation/ansible/junos_get_facts/pb.yml:20
Using module file /opt/stackstorm/virtualenvs/ansible/lib/python2.7/site-packages/ansible/modules/files/file.py
<vmx1> ESTABLISH LOCAL CONNECTION FOR USER: root
<vmx1> EXEC /bin/sh -c 'echo ~ && sleep 0'
Using module file /opt/stackstorm/virtualenvs/ansible/lib/python2.7/site-packages/ansible/modules/files/file.py
<vmx2> ESTABLISH LOCAL CONNECTION FOR USER: root
<vmx2> EXEC /bin/sh -c 'echo ~ && sleep 0'
<vmx1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1494167522.14-179148203693750 `" && echo ansible-tmp-1494167522.14-179148203693750="` echo /root/.ansible/tmp/ansible-tmp-1494167522.14-179148203693750 `" ) && sleep 0'
<vmx2> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1494167522.14-144286707166271 `" && echo ansible-tmp-1494167522.14-144286707166271="` echo /root/.ansible/tmp/ansible-tmp-1494167522.14-144286707166271 `" ) && sleep 0'
<vmx1> PUT /tmp/tmpDZeWox TO /root/.ansible/tmp/ansible-tmp-1494167522.14-179148203693750/file.py
<vmx1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1494167522.14-179148203693750/ /root/.ansible/tmp/ansible-tmp-1494167522.14-179148203693750/file.py && sleep 0'
<vmx2> PUT /tmp/tmpdozB1Q TO /root/.ansible/tmp/ansible-tmp-1494167522.14-144286707166271/file.py
<vmx2> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1494167522.14-144286707166271/ /root/.ansible/tmp/ansible-tmp-1494167522.14-144286707166271/file.py && sleep 0'
<vmx1> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1494167522.14-179148203693750/file.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1494167522.14-179148203693750/" > /dev/null 2>&1 && sleep 0'
<vmx2> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1494167522.14-144286707166271/file.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1494167522.14-144286707166271/" > /dev/null 2>&1 && sleep 0'
ok: [vmx1] => {
"changed": false,
"invocation": {
"module_args": {
"attributes": null,
"backup": null,
"content": null,
"delimiter": null,
"diff_peek": null,
"directory_mode": null,
"follow": false,
"force": false,
"group": null,
"mode": null,
"original_basename": null,
"owner": null,
"path": "/home/mab/automation/ansible/junos_get_facts/inventory/vmx1.conf",
"recurse": false,
"regexp": null,
"remote_src": null,
"selevel": null,
"serole": null,
"setype": null,
"seuser": null,
"src": null,
"state": "absent",
"unsafe_writes": null,
"validate": null
}
},
"path": "/home/mab/automation/ansible/junos_get_facts/inventory/vmx1.conf",
"state": "absent"
}
ok: [vmx2] => {
"changed": false,
"invocation": {
"module_args": {
"attributes": null,
"backup": null,
"content": null,
"delimiter": null,
"diff_peek": null,
"directory_mode": null,
"follow": false,
"force": false,
"group": null,
"mode": null,
"original_basename": null,
"owner": null,
"path": "/home/mab/automation/ansible/junos_get_facts/inventory/vmx2.conf",
"recurse": false,
"regexp": null,
"remote_src": null,
"selevel": null,
"serole": null,
"setype": null,
"seuser": null,
"src": null,
"state": "absent",
"unsafe_writes": null,
"validate": null
}
},
"path": "/home/mab/automation/ansible/junos_get_facts/inventory/vmx2.conf",
"state": "absent"
}
TASK [Retrieve information from devices running Junos] *************************
task path: /home/mab/automation/ansible/junos_get_facts/pb.yml:23
Using module file /etc/ansible/roles/Juniper.junos/library/junos_get_facts
Using module file /etc/ansible/roles/Juniper.junos/library/junos_get_facts
<vmx1> ESTABLISH LOCAL CONNECTION FOR USER: root
<vmx1> EXEC /bin/sh -c 'echo ~ && sleep 0'
<vmx2> ESTABLISH LOCAL CONNECTION FOR USER: root
<vmx2> EXEC /bin/sh -c 'echo ~ && sleep 0'
<vmx1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1494167522.41-211162944277752 `" && echo ansible-tmp-1494167522.41-211162944277752="` echo /root/.ansible/tmp/ansible-tmp-1494167522.41-211162944277752 `" ) && sleep 0'
<vmx2> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1494167522.41-231247062974027 `" && echo ansible-tmp-1494167522.41-231247062974027="` echo /root/.ansible/tmp/ansible-tmp-1494167522.41-231247062974027 `" ) && sleep 0'
<vmx1> PUT /tmp/tmpHmAo7T TO /root/.ansible/tmp/ansible-tmp-1494167522.41-211162944277752/junos_get_facts
<vmx1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1494167522.41-211162944277752/ /root/.ansible/tmp/ansible-tmp-1494167522.41-211162944277752/junos_get_facts && sleep 0'
<vmx2> PUT /tmp/tmpvj5ql9 TO /root/.ansible/tmp/ansible-tmp-1494167522.41-231247062974027/junos_get_facts
<vmx2> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1494167522.41-231247062974027/ /root/.ansible/tmp/ansible-tmp-1494167522.41-231247062974027/junos_get_facts && sleep 0'
<vmx1> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1494167522.41-211162944277752/junos_get_facts; rm -rf "/root/.ansible/tmp/ansible-tmp-1494167522.41-211162944277752/" > /dev/null 2>&1 && sleep 0'
<vmx2> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1494167522.41-231247062974027/junos_get_facts; rm -rf "/root/.ansible/tmp/ansible-tmp-1494167522.41-231247062974027/" > /dev/null 2>&1 && sleep 0'
fatal: [vmx1]: FAILED! => {
"changed": false,
"failed": true,
"invocation": {
"module_args": {
"console": null,
"host": "192.168.0.30",
"logfile": null,
"mode": null,
"passwd": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"port": "830",
"savedir": "/home/mab/automation/ansible/junos_get_facts/inventory",
"ssh_private_key_file": null,
"user": "mab"
}
},
"msg": "ImportError: No module named jnpr.junos"
}
fatal: [vmx2]: FAILED! => {
"changed": false,
"failed": true,
"invocation": {
"module_args": {
"console": null,
"host": "192.168.0.40",
"logfile": null,
"mode": null,
"passwd": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"port": "830",
"savedir": "/home/mab/automation/ansible/junos_get_facts/inventory",
"ssh_private_key_file": null,
"user": "mab"
}
},
"msg": "ImportError: No module named jnpr.junos"
}
to retry, use: --limit @/home/mab/automation/ansible/junos_get_facts/pb.retry
PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
vmx1 : ok=1 changed=0 unreachable=0 failed=1
vmx2 : ok=1 changed=0 unreachable=0 failed=1
"
succeeded: false