snippetyamlMinor
how to prevent Ansible from showing output on screen?
Viewed 0 times
preventshowingoutputhowscreenfromansible
Problem
I have this in my playbook which execute a command on remote server and enters its log on local server.
the output is always shown on screen and troubleshooting gets hard and distracting.
is it possible to prevent Ansible from printing these output on screen and just insert output to a file?
- name: run script
shell: runuser -l testuser -c "/tmp/test.sh"
register: myshell_output
- name: copy output to a local file
lineinfile:
dest: /thesaurus/output
line: "{{ item }}"
insertafter: EOF
with_items:
- "#####################Beginning##########################"
- "{{ myshell_output.stdout }}"
- "########################END#############################"
delegate_to: localhostthe output is always shown on screen and troubleshooting gets hard and distracting.
TASK [run script] ************
changed: [1.1.1.1]
changed: [2.2.2.2]
TASK [copy output to a local file] ***********
changed: [1.1.1.1 -> localhost] =>
(item=#####################Beginning##########################)
ok: [2.2.2.2 -> localhost] =>
(item=#####################Beginning##########################)
{
REALLY LONG AND BIG OUTPUT!
}
changed: [1.1.1.1 -> localhost] =>
(item=########################END#############################)
ok: [2.2.2.2 -> localhost] =>
(item=########################END#############################)is it possible to prevent Ansible from printing these output on screen and just insert output to a file?
Solution
This can be done by judicious use of one of the Ansible callback plugins. Set the relevant configuration in your
You have a few options...
In order to print nothing to the screen, you can use the null callback:
This callback prevents outputing events to screen
However, this may be overkill and doesn't do what you want, which is to write the output to a file. This can be done with either of the
modules.
I would probably suggest the log_plays module in your case.
Note: This is going straight from the documentation. I haven't used the tree or log_plays plugin myself, opting either for the yaml or profile_tasks one, to make things more readable. It also seems like you want to filter just a part of the output, not all of it. You may want to use a combination of filters in order to get the desired effect.
ansible.cfg file.You have a few options...
In order to print nothing to the screen, you can use the null callback:
This callback prevents outputing events to screen
However, this may be overkill and doesn't do what you want, which is to write the output to a file. This can be done with either of the
- tree or
- log_plays
modules.
I would probably suggest the log_plays module in your case.
Note: This is going straight from the documentation. I haven't used the tree or log_plays plugin myself, opting either for the yaml or profile_tasks one, to make things more readable. It also seems like you want to filter just a part of the output, not all of it. You may want to use a combination of filters in order to get the desired effect.
Context
StackExchange DevOps Q#10116, answer score: 2
Revisions (0)
No revisions yet.