- name: Get primary lo0 address
hosts: all
connection: local
gather_facts: no
roles:
- Juniper.junos
- cmprescott.xml
vars:
temp_dir: /tmp
USER: jeffl
PASSWORD: pass123
tasks:
- name: create temporary filename
shell: mktemp "{{ temp_dir }}"/`date +"%Y%m%d%d%m"`"_{{ inventory_hostname }}".XXXXX.xml
register: temp_file
- name: Execute get_config RPC
junos_rpc:
host={{ inventory_hostname }}
user={{ USER }}
port=22
passwd={{ PASSWORD }}
rpc=get_config
logfile=logs/{{ inventory_hostname }}_cli.log
dest="{{ temp_file.stdout }}"
format=xml
filter_xml="<configuration><interfaces><interface><name>lo0</name></interface></interfaces></configuration>"
- name: Parse XML
xml:
file={{ temp_file.stdout }}
xpath=//unit[name='0']/family/inet/address/primary/preceding-sibling::name
content=text
register: loopback_address_match
failed_when: loopback_address_match['matches'] is not defined
- name: Set fact
set_fact:
loopback: "{{ loopback_address_match['matches'][0]['name'] }}"
- name: Print fact
debug:
var: loopback--
You received this message because you are subscribed to the Google Groups "Junos Python EZ" group.
To unsubscribe from this group and stop receiving emails from it, send an email to junos-python-ez+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/junos-python-ez.
To view this discussion on the web visit https://groups.google.com/d/msgid/junos-python-ez/47a3e890-17d3-4d55-ba58-2795cb458728%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Nitin Kr
To unsubscribe from this group and stop receiving emails from it, send an email to junos-python-...@googlegroups.com.
Thank you. I noticed that junos_get_facts didn't include the loopack. I didn't know that the junos_facts module does.
You are right about the difficultly of parsing the data structure. I tried for a while this morning to parse the output of junos_command for the primary lo0 address. I couldn't figure it out.
I'll probably stick with using xpath via the cmprescott.xml role. Xpath is well-documented, and it's easy to check your expression using validators such as http://www.xpathtester.com/xpath.
fatal: [10.63.64.96]: FAILED! => { "msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'xml'\n\nThe error appears to have been in '/home/automation/ansible/playbooks/get_loopback_ip_address.yml': line 19, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n register: result\n - name: Parse the lo0 IP\n ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'dict object' has no attribute 'xml'"}
---- name: Get lo0 address from Junos routers hosts: lab-acx-3 connection: local gather_facts: no roles: - Juniper.junos tasks: - name: Get lo0 IP junos_command:
host: "{{ inventory_hostname }}" username: "authorized_user" password: "authorized_password" rpcs: - "get-interface-information interface_name=lo0.0 terse=True" format: xml register: result - name: Parse the lo0 IP xml: xmlstring: "{{ result.xml[0] }}" xpath: "./logical-interface[name='lo0.0']/address-family[address-family-name='inet']/interface-address/ifa-local" content: 'text' register: result failed_when: result['matches'] is not defined - name: Set the lo0 IP Fact set_fact: lo0_ip: "{{ result['matches'][0]['ifa-local'] }}" - name: Print the lo0 IP debug: var: lo0_ip
# - name: Parse the lo0 IP# xml:# xmlstring: "{{ result.xml[0] }}"# xpath: "./logical-interface[name='lo0.0']/address-family[address-family-name='inet']/interface-address/ifa-local"# content: 'text'# register: result# failed_when: result['matches'] is not defined# - name: Set the lo0 IP Fact# set_fact:# lo0_ip: "{{ result['matches'][0]['ifa-local'] }}"# - name: Print the lo0 IP# debug:# var: lo0_ip
PLAY [Get lo0 address from Junos routers] ************************************************************************************************************************************************************************************************
TASK [Get lo0 IP] ************************************************************************************************************************************************************************************************************************ [WARNING]: argument username has been deprecated and will be removed in a future version
[WARNING]: argument host has been deprecated and will be removed in a future version
[WARNING]: argument password has been deprecated and will be removed in a future version
ok: [1.1.1.1]
TASK [Printe result variable] ************************************************************************************************************************************************************************************************************ok: [1.1.1.1] => { "result": { "changed": false, "failed": false, "output": [ { "rpc-reply": { "interface-information": { "logical-interface": { "address-family": { "address-family-name": "inet", "interface-address": [ { "ifa-destination": "0/0", "ifa-local": "1.1.1.1" }, { "ifa-destination": "0/0", "ifa-local": "127.0.0.1" } ] }, "admin-status": "up", "filter-information": "", "name": "lo0.0", "oper-status": "up" } } } } ], "stdout": [ "<rpc-reply message-id=\"urn:uuid:78a03c04-7052-42d4-8762-072c5f51e5d5\">\n<interface-information style=\"terse\">\n<logical-interface>\n<name>\nlo0.0\n</name>\n<admin-status>\nup\n</admin-status>\n<oper-status>\nup\n</oper-status>\n<filter-information>\n</filter-information>\n<address-family>\n<address-family-name>\ninet\n</address-family-name>\n<interface-address>\n<ifa-local>\n1.1.1.1\n</ifa-local>\n<ifa-destination emit=\"emit\">\n0/0\n</ifa-destination>\n</interface-address>\n<interface-address>\n<ifa-local>\n127.0.0.1\n</ifa-local>\n<ifa-destination emit=\"emit\">\n0/0\n</ifa-destination>\n</interface-address>\n</address-family>\n</logical-interface>\n</interface-information>\n</rpc-reply>" ], "stdout_lines": [ [ "<rpc-reply message-id=\"urn:uuid:78a03c04-7052-42d4-8762-072c5f51e5d5\">", "<interface-information style=\"terse\">", "<logical-interface>", "<name>", "lo0.0", "</name>", "<admin-status>", "up", "</admin-status>", "<oper-status>", "up", "</oper-status>", "<filter-information>", "</filter-information>", "<address-family>", "<address-family-name>", "inet", "</address-family-name>", "<interface-address>", "<ifa-local>", "1.1.1.1", "</ifa-local>", "<ifa-destination emit=\"emit\">", "0/0", "</ifa-destination>", "</interface-address>", "<interface-address>", "<ifa-local>", "127.0.0.1", "</ifa-local>", "<ifa-destination emit=\"emit\">", "0/0", "</ifa-destination>", "</interface-address>", "</address-family>", "</logical-interface>", "</interface-information>", "</rpc-reply>" ] ], "warnings": [ "argument username has been deprecated and will be removed in a future version", "argument host has been deprecated and will be removed in a future version", "argument password has been deprecated and will be removed in a future version" ] }}
PLAY RECAP *******************************************************************************************************************************************************************************************************************************1.1.1.1 : ok=2 changed=0 unreachable=0 failed=0
# set_fact:# lo0_ip: "{{ result['matches'][0]['ifa-local'] }}"
Debug output
"interface-address": [{"ifa-destination": "0/0","ifa-local": "1.1.1.1"},{"ifa-destination": "0/0","ifa-local": "127.0.0.1"}]
},
ansible-playbook -vvvv -i ../inventory/junos get_loopback_ip_address.ymlansible-playbook 2.4.2.0 config file = /etc/ansible/ansible.cfg configured module search path = [u'/home/zshivers/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/site-packages/ansible executable location = /usr/bin/ansible-playbook python version = 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]Using /etc/ansible/ansible.cfg as config filesetting up inventory pluginsParsed /home/automation/ansible/inventory/junos inventory source with ini pluginLoading callback plugin default of type stdout, v2.0 from /usr/lib/python2.7/site-packages/ansible/plugins/callback/__init__.pycLoading callback plugin jsnapy of type aggregate, v2.0 from /usr/lib/python2.7/site-packages/ansible/plugins/callback/__init__.pyc
PLAYBOOK: get_loopback_ip_address.yml ****************************************************************************************************************************************************************************************************1 plays in get_loopback_ip_address.yml
PLAY [Get lo0 address from Junos routers] ************************************************************************************************************************************************************************************************META: ran handlers
TASK [Get lo0 IP] ************************************************************************************************************************************************************************************************************************task path: /home/automation/ansible/playbooks/get_loopback_ip_address.yml:9<1.1.1.1> using connection plugin netconf<1.1.1.1> socket_path: /home/zshivers/.ansible/pc/5d32538b86Using module file /usr/lib/python2.7/site-packages/ansible/modules/network/junos/junos_command.py<1.1.1.1> ESTABLISH LOCAL CONNECTION FOR USER: zshivers<1.1.1.1> EXEC /bin/sh -c 'echo ~ && sleep 0'<1.1.1.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/zshivers/.ansible/tmp/ansible-tmp-1517708698.49-193065815375954 `" && echo ansible-tmp-1517708698.49-193065815375954="` echo /home/zshivers/.ansible/tmp/ansible-tmp-1517708698.49-193065815375954 `" ) && sleep 0'<1.1.1.1> PUT /tmp/tmpVo3rnL TO /home/zshivers/.ansible/tmp/ansible-tmp-1517708698.49-193065815375954/junos_command.py<1.1.1.1> EXEC /bin/sh -c 'chmod u+x /home/zshivers/.ansible/tmp/ansible-tmp-1517708698.49-193065815375954/ /home/zshivers/.ansible/tmp/ansible-tmp-1517708698.49-193065815375954/junos_command.py && sleep 0'<1.1.1.1> EXEC /bin/sh -c '/usr/bin/python /home/zshivers/.ansible/tmp/ansible-tmp-1517708698.49-193065815375954/junos_command.py; rm -rf "/home/zshivers/.ansible/tmp/ansible-tmp-1517708698.49-193065815375954/" > /dev/null 2>&1 && sleep 0' [WARNING]: argument username has been deprecated and will be removed in a future version
[WARNING]: argument host has been deprecated and will be removed in a future version
[WARNING]: argument password has been deprecated and will be removed in a future version
ok: [1.1.1.1] => { "changed": false, "invocation": { "module_args": { "commands": null, "display": "xml", "format": "xml", "host": "1.1.1.1", "interval": 1, "match": "all", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "port": null, "provider": null, "retries": 10, "rpcs": [ "get-interface-information interface_name=lo0.0 terse=True" ], "ssh_keyfile": null, "timeout": null, "transport": null, "username": "authorized_user", "wait_for": null } }, "output": [ { "rpc-reply": { "interface-information": { "logical-interface": { "address-family": { "address-family-name": "inet", "interface-address": [ { "ifa-destination": "0/0", "ifa-local": "1.1.1.1" }, { "ifa-destination": "0/0", "ifa-local": "127.0.0.1" } ] }, "admin-status": "up", "filter-information": "", "name": "lo0.0", "oper-status": "up" } } } } ], "stdout": [ "<rpc-reply message-id=\"urn:uuid:379319a3-45fe-4997-8c7a-1704cb4057ef\">\n<interface-information style=\"terse\">\n<logical-interface>\n<name>\nlo0.0\n</name>\n<admin-status>\nup\n</admin-status>\n<oper-status>\nup\n</oper-status>\n<filter-information>\n</filter-information>\n<address-family>\n<address-family-name>\ninet\n</address-family-name>\n<interface-address>\n<ifa-local>\n1.1.1.1\n</ifa-local>\n<ifa-destination emit=\"emit\">\n0/0\n</ifa-destination>\n</interface-address>\n<interface-address>\n<ifa-local>\n127.0.0.1\n</ifa-local>\n<ifa-destination emit=\"emit\">\n0/0\n</ifa-destination>\n</interface-address>\n</address-family>\n</logical-interface>\n</interface-information>\n</rpc-reply>" ], "stdout_lines": [ [ "<rpc-reply message-id=\"urn:uuid:379319a3-45fe-4997-8c7a-1704cb4057ef\">",TASK [Printe result variable] ************************************************************************************************************************************************************************************************************task path: /home/automation/ansible/playbooks/get_loopback_ip_address.yml:19ok: [1.1.1.1] => { "result": { "changed": false, "failed": false, "output": [ { "rpc-reply": { "interface-information": { "logical-interface": { "address-family": { "address-family-name": "inet", "interface-address": [ { "ifa-destination": "0/0", "ifa-local": "1.1.1.1" }, { "ifa-destination": "0/0", "ifa-local": "127.0.0.1" } ] }, "admin-status": "up", "filter-information": "", "name": "lo0.0", "oper-status": "up" } } } } ], "stdout": [ "<rpc-reply message-id=\"urn:uuid:379319a3-45fe-4997-8c7a-1704cb4057ef\">\n<interface-information style=\"terse\">\n<logical-interface>\n<name>\nlo0.0\n</name>\n<admin-status>\nup\n</admin-status>\n<oper-status>\nup\n</oper-status>\n<filter-information>\n</filter-information>\n<address-family>\n<address-family-name>\ninet\n</address-family-name>\n<interface-address>\n<ifa-local>\n1.1.1.1\n</ifa-local>\n<ifa-destination emit=\"emit\">\n0/0\n</ifa-destination>\n</interface-address>\n<interface-address>\n<ifa-local>\n127.0.0.1\n</ifa-local>\n<ifa-destination emit=\"emit\">\n0/0\n</ifa-destination>\n</interface-address>\n</address-family>\n</logical-interface>\n</interface-information>\n</rpc-reply>" ], "stdout_lines": [ [ "<rpc-reply message-id=\"urn:uuid:379319a3-45fe-4997-8c7a-1704cb4057ef\">",TASK [Parse the lo0 IP] ******************************************************************************************************************************************************************************************************************task path: /home/automation/ansible/playbooks/get_loopback_ip_address.yml:22fatal: [1.1.1.1]: FAILED! => { "msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'xml'\n\nThe error appears to have been in '/home/automation/ansible/playbooks/get_loopback_ip_address.yml': line 22, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n var: result\n - name: Parse the lo0 IP\n ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'dict object' has no attribute 'xml'"} to retry, use: --limit @/home/automation/ansible/playbooks/get_loopback_ip_address.retry
PLAY RECAP *******************************************************************************************************************************************************************************************************************************1.1.1.1 : ok=2 changed=0 unreachable=0 failed=1
-bash-4.2$