Ansible - overwriting file lineinfile when run in parallel

30 views
Skip to first unread message

Josephsimon Arokiaraj

unread,
Jun 25, 2019, 6:25:31 AM6/25/19
to Ansible Project
Hi All,

I am running the below code in a playbook which iterates over many hosts. This script writes the value of ver.stdout to a file. I am seeing issue when I run in in parallel. It is overwriting. It works good when serial:1 but it consumes long time since we have large number of hosts. 

Any help to run in parallel and improve the time consumption?


- name: Write version output
  local_action: lineinfile create=yes line={{ item }} insertafter=EOF dest={{ dest_file }}
  when: ver.stdout != ""
  with_items:
    - "{{ ver.stdout | regex_replace('\r\n' , '#' )}},{{ inventory_hostname }}"


Cheers,
Joseph
  

Josephsimon Arokiaraj

unread,
Jun 26, 2019, 7:00:34 AM6/26/19
to Ansible Project

Josephsimon Arokiaraj

unread,
Jun 26, 2019, 7:01:09 AM6/26/19
to Ansible Project
Hi All,

Any update on this please?

Cheers,
Simon J


On Tuesday, June 25, 2019 at 11:25:31 AM UTC+1, Josephsimon Arokiaraj wrote:

Tony Chia

unread,
Jun 26, 2019, 2:28:56 PM6/26/19
to Ansible Project
Since you are using local_action, it will execute on the ansible controller instead of remote host
local_action is an alternative way of doing delegate_to: localhost. These actions will be executed on local machine (Ansible control host)
I don't see how it would behave differently if you use serial:1 or 5 which is the default.

Kai Stian Olstad

unread,
Jun 27, 2019, 2:01:32 AM6/27/19
to ansible...@googlegroups.com
On 25.06.2019 12:25, Josephsimon Arokiaraj wrote:
> Hi All,
>
> I am running the below code in a playbook which iterates over many
> hosts.
> This script writes the value of ver.stdout to a file. I am seeing issue
> when I run in in parallel. It is overwriting. It works good when
> serial:1
> but it consumes long time since we have large number of hosts.
>
> Any help to run in parallel and improve the time consumption?

It's not possible to write to the same file in parallel so you need to
serialize it.
Instead of doing it on the play, do it on the task.


> - name: Write version output
> local_action: lineinfile create=yes line={{ item }} insertafter=EOF
> dest={{ dest_file }}
> when: ver.stdout != ""
> with_items:
> - "{{ ver.stdout | regex_replace('\r\n' , '#' )}},{{
> inventory_hostname
> }}"

Loop through all host in the play with ansible_play_hosts and use
hostvars to get the variable and set run_once as true.
So something like this should work.

- name: Write version output
lineinfile:
create: yes
line: '{{ hostvars[item].ver.stdout | regex_replace('\r\n' , '#'
)}},{{ item }}'
insertafter: EOF
dest: {{ dest_file }}
when: hostvars[item].ver.stdout != ""
run_once: yes
delegate_to: localhost
with_items: '{{ ansible_play_hosts }}'


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages