0
votes

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.

1
I guess the hosts names are not the real ones? A maybe silly question would be: are you sure that, when uppercased, you don't have two servers having the same name? And the second one would be: what were you intending to do with the regexp part of your step? Why is it there?β.εηοιτ.βε
Uppercase or lowercase doesn't matter in my case, already tested without uppercase filter. The regex was used first when I was doing lineinfile on the same file for all hosts. I thought maybe the issue could come because all hosts would modify this file at the same time. But even doing it on separated files for each hosts makes no difference.Mat_

1 Answers

1
votes

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.