1

I want to check the ip on the server where ip list i have in the json file as below, cat /tmp/iplist.json

[

"10.10.10.182",
"182.10.10.2",
"192.168.200.2"

]

now the condition is only one ip exist on the system so i was executing the loop to store the only the success output in variable but i am not able to do that does any one knows how can i do this
here is my playbook

---
- name: Load values from json file
  hosts: localhost
  gather_facts: false

  vars:
    ip: "{{ lookup('file', '/tmp/iplist.json') | from_json }}"

  tasks:

    - name: Loop over imported iplist
      shell: ip a | grep {{ item }}
      loop: "{{ ip }}"
      changed_when: true
      register: echo

    - debug:
        msg: "{{ echo }}"

And this how it getting failed error

PLAY [Load values from json file] *************************************************************************************************************************

TASK [Loop over imported iplist] *******************************************************************************************************************************
changed: [localhost] => (item=10.10.10.182)
failed: [localhost] (item=182.10.10.2) => {"ansible_loop_var": "item", "changed": true, "cmd": "ip a | grep 182.10.10.2", "delta": "0:00:00.012178", "end": "2020-05-09 11:30:06.919913", "item": "182.10.10.2", "msg": "non-zero return code", "rc": 1, "start": "2020-05-09 11:30:06.907735", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
failed: [localhost] (item=192.168.200.2) => {"ansible_loop_var": "item", "changed": true, "cmd": "ip a | grep 192.168.200.2", "delta": "0:00:00.029234", "end": "2020-05-09 11:30:07.178768", "item": "192.168.200.2", "msg": "non-zero return code", "rc": 1, "start": "2020-05-09 11:30:07.149534", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

PLAY RECAP *****************************************************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

1 Answer 1

3

When you enable gather_facts: true the variable ansible_all_ipv4_addresses will keep the list of all IPv4 addresses of the host. Use intersect to find common items. For example

    - debug:
        msg: "{{ ansible_all_ipv4_addresses | intersect(ip) }}"
Sign up to request clarification or add additional context in comments.

4 Comments

Hi this is just example have taken of ip since this is what i can test in live, actually i was taking this approach for query the mac address on the switch and if mac found it should get store in variable.
ansible_interfaces contains the list of interfaces, ansible_<interfacename> contains all the details of the interface, including the macaddress.
To get a global id of the available facts, you can execute ansible -i yourinventory.ini yourtargethost -m setup. This is what a playbook executes on each host when gather_facts: true.
I have taken down the Mac from the db from different system where ansible doesn't have reachability so I can't take fact from ansible ..now I want to query Mac address on the switch to find out the Mac but after running the playbook it will return what ever port are connected on the and that output only I want to store in variable to automate my network configuration...I have taken this option to test since I don't have iOS switch as of now to test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.