1

I am new in Ansible and wondering why I am getting invalid results. Here is the short script which should return an inventory informations like SN, model, SW version and name.

    ---
- name: Cisco facts
  vars_prompt:
    - name: "site"
      prompt: "Which device would you like to check?"
      private: no
      default: all
  hosts: "{{ site }}"
  gather_facts: false
  connection: local

  tasks:
    - name: facts
      # Collect hardware facts from the device
      ios_facts:
        gather_subset: "hardware"
      register: output  
    - local_action:
        module: lineinfile
        dest: /Users/username/Downloads/test2.txt
        insertafter: EOF
        line: "{{ ansible_net_hostname }},{{ ansible_net_model }},{{ ansible_net_version }},{{ ansible_net_serialnum }}"
        create: yes

When I run a playbook I am getting a correct results as long as devices what I am connecting to, have different IP address. For example, when I connect to 172.16.110.2 and 172.16.110.3 return is correct I have two row entries in created file. When I connect devices with IP 172.16.110.11 and then 172.16.110.1 it creates a file with one row entry (the one which was last connected). Play recap shows me that all devices has been connected, action redirected to localhost and changed=1.

Why the script doesn't add a line when connecting to similar device IP address?

  • Try rearranging the order of the hosts in the hostfile. Perhaps the problem has nothing to do with IP addresses. – Ron Trunk Sep 27 at 13:40
3

Lineinfile is a weird choice here. It’s a module used to add lines to a configuration file (for example) and make sure a line is there, it’s not meant for things like logging, which you seem to be doing. Since there’s a large overlap in the IP address, fair chance it matches and the line is not added since the module assumes it’s already there. I’d look for an ansible module which is better suited for logging. I only run ansible on network devices, don’t do anything like this, so I don’t have a good suggestion.

  • I am running it on network devices as well - Cisco switches. Thank you for your suggestions, I'll try to find something else for logging. – deuter Sep 28 at 7:30
  • What I meant is that i’m not running local actions as you are doing, all changes are done on my network devices. – Teun Vink Sep 28 at 7:37
  • Ok, so how can I save the result into a file? To make an inventory list not running local actions? – deuter Sep 28 at 11:41
  • There’s nothing wrong with local actions, you should just look in the ansible module list for a module which actually does what you need. lineinfile is for editing configuration files. – Teun Vink Sep 28 at 13:26
0

Problem has been solved, thank you for your suggestions. Fact are gathered for each host by ios_facts module. Accessing this data via hostvars saves correct information to the file.

    ---
- name: Cisco facts
  vars_prompt:
    - name: "site"
      prompt: "Which device would you like to check?"
      private: no
      default: all
  hosts: "{{ site }}"
  gather_facts: false
  connection: local

  tasks:
    - name: facts
      # Collect hardware facts from the device
      ios_facts:
        gather_subset: "hardware"
    - local_action:
        module: copy
        content: |
          Hostname,Model,SN,Version
          {% for host in groups [site] %}
          {{ hostvars[host].ansible_net_hostname }},{{ hostvars[host].ansible_net_model }},{{ hostvars[host].ansible_net_serialnum }},{{ hostvars[host].ansible_net_version }}
          {% endfor %}
        dest: /Users/slawomirkaczmarek/Downloads/test2.txt
      run_once: true  

You don't need to run the task as many times as hosts in the group, thus run_once in the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.