Ansible - add new line to a file

Ron_1984

New Member
Joined
Feb 8, 2021
Messages
18
Reaction score
4
Credits
251
Hi

I am trying to add a line in a file on the control node after performing the yum update on the managed nodes. I have created the below playbook. Adding new lines to the file in control node is getting skipped for some random servers.

- name: Mail function
hosts: SSS
vars:
date: "{{ lookup('pipe', 'date +%Y%m%d') }}"
tasks:
- name: Send Command to users logged in
command: wall -n "System will go down for 2 hours maintenance at 13:00 PM"
- name: Add new Line
lineinfile:
state: present
dest: /ansible/update{{date}}
line: 'System {{ inventory_hostname }} has been successfully provisioned at {{ date }} "'
insertafter: EOF
regexp: '^HELLO'
delegate_to: dockermgr1


and the hosts SSS has the below servers in the inventory
dockermgr1
dockermgr2
dockermgr3


File /ansible/update is updated as below in first run
We have patched the dockermgr1 successfully at 20210826
We have patched the dockermgr3 successfully at 20210826

File /ansible/update is updated as below in second run
We have patched the dockermgr1 successfully at 20210826
We have patched the dockermgr2 successfully at 20210826
We have patched the dockermgr3 successfully at 20210826


File /ansible/update is updated as below in third run
We have patched the dockermgr2 successfully at 20210826

Can you help?
 


If you use ansible -v while doing the run you will see more of why ansible is skipping something. Looking at your playbook there are no conditions so no reason there why ansible would be skipping something. Maybe else where you have code that is causing this?
 
ran the playbook with -v, it is getting executed successfully on all nodes, however the file is not updated for every nodes.

Is something related to lineinfile statements? or is there any alternate options to add a new line to the file in the control nodes?

below is the output from -V

[test1@dockermgr1 ansible]$ ansible-playbook test.yml -v
Using /ansible/ansible.cfg as config file
SSH password:

PLAY [Mail function] *************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************
ok: [dockermgr1]
ok: [dockermgr2]
ok: [dockermgr3]


TASK [Line Insert] ***************************************************************************************************************************************************************************
changed: [dockermgr1 -> dockermgr1] => {"backup": "", "changed": true, "msg": "line added"}
changed: [dockermgr3 -> dockermgr1] => {"backup": "", "changed": true, "msg": "line added"}
changed: [dockermgr3 -> dockermgr1] => {"backup": "", "changed": true, "msg": "line added"}


PLAY RECAP ***********************************************************************************************************************************************************************************
dockermgr1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
dockermgr2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
dockermgr3 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
 
I don't think the problem is with your code because if it was I would actually expect none of the files to be updated on the nodes because you have no conditionals in your code or I would expect an error at some point. On which node is the file not getting updated?
 
I don't think the problem is with your code because if it was I would actually expect none of the files to be updated on the nodes because you have no conditionals in your code or I would expect an error at some point. On which node is the file not getting updated?
its the random nodes getting missed. Each time I run the playbook a random node(s) is missed and sometime it worked on all nodes.

any suggestions to add a line to the file in control node other thn module lineinfile ?
 
I took part of your code and put it in a playbook, but I removed delegate_to because that didn't work but you must do it for a reason in your setup.
YAML:
- name: Mail function
  hosts: SSS
  become_user: ansible
  vars:
          #date: "{{ lookup('pipe', 'date +%Y%m%d') }}"
          date: "{{ lookup('pipe', 'date +%H:%M:%S:%Y') }}"
  tasks:
   - name: Add new Line
     lineinfile:
      state: present
      dest: /ansible/update
      line: "System {{ inventory_hostname }} has been successfully provisioned at {{ date }}"
      insertafter: EOF
      #regexp: '^HELLO'
      #delegate_to: node1
Then I ran it several times and for me the file update gets updates for all 3 nodes every time.
[ansible@ansible example]$ for n in node1 node2 node3; do echo "$n:"; ssh $n "cat /ansible/update"; echo " "; done
node1:
System node1 has been successfully provisioned at 13:53:31:2021
System node1 has been successfully provisioned at 13:53:42:2021
System node1 has been successfully provisioned at 13:54:03:2021

node2:
System node2 has been successfully provisioned at 13:53:31:2021
System node2 has been successfully provisioned at 13:53:42:2021
System node2 has been successfully provisioned at 13:54:03:2021

node3:
System node3 has been successfully provisioned at 13:53:31:2021
System node3 has been successfully provisioned at 13:53:42:2021
System node3 has been successfully provisioned at 13:54:03:2021

Btw you are adding the date of today in your playbook task for the update files. I would think that if the line already exists it may just overwrite the existing line and then seem as if it got skipped? Try adding the hour, minute and second instead of the date of today during your testing and then see what happens?
 
Last edited:
I took part of your code and put it in a playbook, but I removed delegate_to because that didn't work but you must do it for a reason in your setup.
YAML:
- name: Mail function
  hosts: SSS
  become_user: ansible
  vars:
          #date: "{{ lookup('pipe', 'date +%Y%m%d') }}"
          date: "{{ lookup('pipe', 'date +%H:%M:%S:%Y') }}"
  tasks:
   - name: Add new Line
     lineinfile:
      state: present
      dest: /ansible/update
      line: "System {{ inventory_hostname }} has been successfully provisioned at {{ date }}"
      insertafter: EOF
      #regexp: '^HELLO'
      #delegate_to: node1
Then I ran it several times and for me the file update gets updates for all 3 nodes every time.


Btw you are adding the date of today in your playbook task for the update files. I would think that if the line already exists it may just overwrite the existing line and then seem as if it got skipped? Try adding the hour, minute and second instead of the date of today during your testing and then see what happens?
Yes you are right, Since I had the date in the playbook that could cased the problem. I changed to add hour, min and sec. however they playbook got executed immediately at the same time on all nodes, this was same as having the date and did not update the file for all three nodes.

output /ansible/update:
We have patched the dockermgr1 successfully at 14:58:27:2021
We have patched the dockermgr3 successfully at 14:58:27:2021

I tried to execute the playbook with serial: 1 and it helped me.


I used delegate_to:node1 to ensure the file is updated on the control nodes even when the play run on managed nodes. But in your playbook you had removed the delegate_to and still the file /ansible/update is updated with the three lines on all nodes? doesn't it update only one line per node based on the execution?
 
The problem isn't with your playbook because if it was I would be having the same problem.
 

Members online


Top