registered variables being dumped to local files

19 views
Skip to first unread message

dewey....@gmail.com

unread,
Aug 3, 2017, 3:18:02 PM8/3/17
to ansible...@googlegroups.com
hi all. i run a python script on a linux server that produces a report relating to
patch levels and such. it's a fairly simple thing that was requested for audit
purposes. it's a home-grown script. it works fine, and does what it needs to do.
the report it produces is simply sent to stdout.

with the hope of producing a single report covering all of these servers, i have
created a simple playbook which is designed to run the script on all targets,
capture their report via stdout, and add the reports to a single local file via
local_action. sometimes this works, and sometimes it does not. in all cases i
can see that the report is generated (stdout for each target shows on the
ansible controller during task execution). but sometimes all output for a given
target is missing from the local logfile. here is the playbook:

in case it gets munged in email:
https://paste.pound-python.org/show/Q8hdyPC38ADSYCX82wwV/

---
- hosts: all

tasks:
- name: run report
script: pkgReport.py {{ inventory_hostname }}
register: report

- name: build local report
lineinfile:
path: "/tmp/pkgReport.txt"
insertafter: EOF
line: "{{ item }}"
state: present
create: yes
with_items: "{{ report.stdout.lines }}"
delegate_to: 127.0.0.1


so first question ... can someone point out what i may be doing wrong?
and of course next question ... what is the smartest way of doing this?

dewey....@gmail.com

unread,
Aug 3, 2017, 3:40:34 PM8/3/17
to ansible-project
sorry, i pasted an older (broken) yml ...

https://paste.pound-python.org/show/QAOL5ukLVvAtqtCN8I3V/

the difference is "report.stdout" vs "report.stdout.lines" ...
this issue also happens with blockinfile.

Kai Stian Olstad

unread,
Aug 3, 2017, 4:16:04 PM8/3/17
to ansible...@googlegroups.com
On 03. aug. 2017 21:18, dewey....@gmail.com wrote:
> capture their report via stdout, and add the reports to a single local file via
> local_action. sometimes this works, and sometimes it does not. in all cases i
> can see that the report is generated (stdout for each target shows on the
> ansible controller during task execution). but sometimes all output for a given
> target is missing from the local logfile. here is the playbook:
>
> ---
> - hosts: all
>
> tasks:
> - name: run report
> script: pkgReport.py {{ inventory_hostname }}
> register: report
>
> - name: build local report
> lineinfile:
> path: "/tmp/pkgReport.txt"
> insertafter: EOF
> line: "{{ item }}"
> state: present
> create: yes
> with_items: "{{ report.stdout.lines }}"
> delegate_to: 127.0.0.1
>
>
> so first question ... can someone point out what i may be doing wrong?
> and of course next question ... what is the smartest way of doing this?

Lets say you have 20 host, then you would have 20 task trying to write
to the same file at once(or at least 5 at a time with default, because
forks is default set to 5), this will seldom if at all work.

You can use assemble or the template module, personally I would use the
template.

local_report.j2
---------------
{% for item in ansible_play_hosts %}
Report for host {{ item }}:
{{ hostvars[item].report.stdout }}
{% endfor %}

And a task to create the file:
- name: build local report
template:
src: local_report.j2
dest: /tmp/pkgReport.txt
run_once: true
delegate_to: localhost

--
Kai Stian Olstad

Dewey Hylton

unread,
Aug 3, 2017, 4:26:43 PM8/3/17
to Ansible Project, ansible-pr...@olstad.com

This makes a lot of sense. I thought i had multiple things running into each other, and I suppose
the fact that it seemed to work sometimes threw me off somewhat.

Thanks!
 
Reply all
Reply to author
Forward
0 new messages