0

I have a need to know the index of host names in the inventory. I am using the below code to create a variable file that I can use in a subsequent play book

- name: Debug me
  hosts: hosts
  tasks:
      - debug: msg="{{ inventory_hostname }}"
      - debug: msg="{{ play_hosts.index(inventory_hostname) }}"
      - local_action: 'lineinfile create=yes dest=/tmp/test.conf
                   line="host{{ play_hosts.index(inventory_hostname) }}=
                   {{ inventory_hostname }}"'

I have the following inventory file

[hosts]
my.host1.com
my.host2.com

Now when I run this, the test.conf that gets generated under /tmp sometimes has both hostnames like this

host1= my.host2.com
host0= my.host1.com

when I run the same playbook a few times each time emptying the test.conf before running. quite a few times the file only has one entry

host1= my.host2.com

or

host0= my.host1.com

how come the same ansible playbook behaving differently?

2 Answers 2

0

I believe the issue is your running two threads against different hosts, and using local_action is not thread safe.

Try using the serial keyword:

- name: Debug me
  hosts: hosts
  serial: 1
  tasks:
      - debug: msg="{{ inventory_hostname }}"
      - debug: msg="{{ play_hosts.index(inventory_hostname) }}"
      - local_action: 'lineinfile create=yes dest=/tmp/test.conf
                   line="host{{ play_hosts.index(inventory_hostname) }}=
                   {{ inventory_hostname }}"'

Edit: A better way to do this if just trying to operate on the list of host in inventory on the localhost, would be to avoid doing the action on the host and using local_action in the first place.

- name: Debug me
  hosts: localhost
  tasks:
  - lineinfile:
      create: yes
      dest: /tmp/test.conf
      line: "host{{ groups['hosts'].index(item)}}={{ item }}"
    with_items: " {{ groups['hosts'] }}"

This will get you the results you desire. Then you can add another play to do operations against the hosts themselves.

3
  • When i try with the serial, i get the below result host0= my.host2.com host0= my.host1.com both times it is host0
    – md1980
    Apr 30, 2017 at 5:22
  • Updated my answer to provide a better way to accomplish this task without using serial or local_action. May 1, 2017 at 18:02
  • thanks for that but that doesnt help because, i am trying to loop through remote inventory list and write the hostnames in a file. I cannot have hosts as localhost then it wont loop through the remote inventory list.
    – md1980
    May 12, 2017 at 11:43
0

The solution I am trying to avoid problems with race conditions with non-thread safe Local_action: lineinfile to write gathered data to local file. Split it across 2 different plays in the same file.

eg:

- name: gather_date
  hosts: all
  any_errors_fatal: false
  gather_facts: no
  tasks:
    - name: get_Aptus_device_count_list
      shell: gather_data.sh
      become: true
      register: Aptus_device_count_list
      changed_when: false

- name: Log_gathered_date
  hosts: all
  any_errors_fatal: false
  gather_facts: no
  tasks:
    - name: log_gathered_info
      local_action:  
          module: lineinfile
          dest: /home/rms-mit/MyAnsible/record_Device_count_collection.out 
          line: "\n--- {{ inventory_hostname }} --- \n
             {{ Aptus_device_count_list.stdout }} \n.\n---\n"
      changed_when: false

Your Answer

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

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