Search code examples
ansiblenfsansible-inventory

Ansible "skipping" last host from inventory


I have a simple host inventory :

server1.com
server2.com
server3.com
server4.com

I run some tasks on this inventory and at the end, create file and insert line with lineinfile on a shared folder between all hosts:

  - name: "END - Insert infos in /sharefolder/{{ inventory_hostname|upper }}"
    lineinfile:
      path: "/sharefolder/{{ inventory_hostname|upper }}"
      state: present
      create: yes
      regexp: "^{{ inventory_hostname }}"
      line: "{{ inventory_hostname|upper }};This is my line"
      owner: owner
      group: group
      mode: '0644'

Everything is fine when I run the playbook:

PLAY RECAP **************************************************************************************************************************
server1.com  : ok=14   changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
server2.com  : ok=14   changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
server3.com  : ok=14   changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
server4.com  : ok=14   changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

But when I look into my shared folder, I don't have the file from the last host:

$ ls -l /sharefolder/
-rw-r--r-- 1 owner group 123 Apr  3 11:48 SERVER1.COM
-rw-r--r-- 1 owner group 105 Apr  3 11:48 SERVER2.COM
-rw-r--r-- 1 owner group 123 Apr  3 11:48 SERVER3.COM

I tested with several hosts, adding blank lines at the end of host inventory, and with option --forks=1 when launching the playbook, but issue is still there. Option -vvv returns that everything is OK, file has been created and line added for the last hosts, but it isn't the case.


Solution

  • Why are the file sizes different? If you create the files and write in every file the same, they should have the same size. Maybe you look at the same directory on different hosts?

    If you run your playbook on 4 hosts, then on every hosts one file gets created with one line. If you do not want to execute the task on the remote system, you have to use a local action.