Greetings,
I’m using the devel branch on Ansible Github (e.g. version 2.3) and have run into a curious problem the past few weeks regarding the use of lineinfile that was seemingly bulletproof in prior versions (e.g. 2.0, 2.1, 2.2). We’ve moved to 2.3 for needed enhancements.
I say that the problem is transient because it doesn’t always manifest. That is, I cannot reliably reproduce the problem. However, when it does manifest, it results in an empty file. I’ll attempt to describe my running scenario here, however the actual system we use is Internet-disconnected, so I cannot simply copy/paste results here.
I’m using lineinfile to ensure entries exist in /etc/hosts
My play looks like this:
-
name: Ensure all machines have /etc/hosts entries
become: true
hosts: [machines]
tasks: -
name: ensure /etc/hosts has entries
lineinfile:
dest: /etc/hosts
owner: root
group: root
mode: u=rw,g=r,o=r
line: “{{ hostvars[item][‘vm_ip’] }} {{ hostvars[item][‘vm_hostname’] }} {{ hostvars[item][‘vm_alias’] }}”
create: yes
state: present
backup: yes
with_items: “{{ groups[‘all’] }}”
In other words, I’m looping over my inventory and adding known facts about the inventory as lines in /etc/hosts on [machines]. The known facts are all stored in aptly named files under the host_vars folder. This seemed pretty trivial and worked well in the past. However now, some subset of [machines] end up with a 0-byte length /etc/hosts file. Moreover, it is not consistent on a run-to-run basis. On the majority of runs of the playbook, everything is as expected. But on some runs, certain hosts will have this empty file.
I added the “backup: yes” as a means to troubleshoot and when I do encounter one of these machines with an empty /etc/hosts file, I can see via a directory listing all of the instances of the backups of the file corresponding to each time lineinfile modified the file. I can even see the file size growing over these instances until at some point, the size goes to 0 bytes and stays there. For example:
ls -la /etc/hosts
-rw-r–r–. 1 root root 0 Feb 14 18:02 /etc/hosts
-rw-r–r–. 1 root root 225 Sep 16 18:21 /etc/hosts.2753.2017-02-14@18:02:06
-rw-r–r–. 1 root root 278 Feb14 18:02 /etc/hosts.2774.2017-02-14@18:02:06
-rw-r–r–. 1 root root 331 Feb14 18:02 /etc/hosts.2795.2017-02-14@18:02:06
-rw-r–r–. 1 root root 0 Feb14 18:02 /etc/hosts.2816.2017-02-14@18:02:07
-rw-r–r–. 1 root root 0 Feb14 18:02 /etc/hosts.2837.2017-02-14@18:02:07
-rw-r–r–. 1 root root 0 Feb14 18:02 /etc/hosts.2858.2017-02-14@18:02:07
-rw-r–r–. 1 root root 0 Feb14 18:02 /etc/hosts.2879.2017-02-14@18:02:07
When actually exeuting the ansible-playbook, I’m seeing yellow “changed” output thinking that everything is getting updated and is OK in the end, however I end up in the above situation. Is this a known bug in Ansible 2.3?