I'm struggling with the title for this question, so feel free to edit it to make it more meaningful.
Let's say you have a task in Ansible and you register
the output. For example:
- name: Set up working directory
shell: mktemp -d
register: workdir
And I want to use the registered output to do another task. For example:
- name: Create a file
with_items: "{{ workdir.stdout }}"
shell: touch {{ item }}/test-file
That all works great. Now, I want to loop both tasks N times. (I know I could extract this into a separate yaml file, and use include_tasks
combined with loop
, but I don't want to have two playbooks.) Thus, I update to:
- name: Set up working directory
shell: mktemp -d
register: workdir
loop:
- 1
- 2
As a result of the loop, the workdir
variable has taken a different structure, so iterating through it is now slightly different. For example:
- name: Create a file
with_items: "{{ workdir.results }}"
shell: touch {{ item.stdout }}/test-file
When the playbook is running, the Ansible console displays the entire value for "item" on each iteration. That makes the output noisy and difficult for a human to parse visually. For example:
TASK [Create a file] ***************************************************
changed: [localhost] => (item={'cmd': 'mktemp -d', 'stdout': '/tmp/tmp.a
ncF0iBqzP', 'stderr': '', 'rc': 0, 'start': '2023-05-26 15:22:46.458962'
, 'end': '2023-05-26 15:22:46.465557', 'delta': '0:00:00.006595', 'chang
ed': True, 'invocation': {'module_args': {'_raw_params': 'mktemp -d', '_
uses_shell': True, 'warn': True, 'stdin_add_newline': True, 'strip_empty
_ends': True, 'argv': None, 'chdir': None, 'executable': None, 'creates'
: None, 'removes': None, 'stdin': None}}, 'stdout_lines': ['/tmp/tmp.anc
F0iBqzP'], 'stderr_lines': [], 'failed': False, 'item': 1, 'ansible_loop
_var': 'item'})
changed: [localhost] => (item={'cmd': 'mktemp -d', 'stdout': '/tmp/tmp.v
Tpkvx6RK0', 'stderr': '', 'rc': 0, 'start': '2023-05-26 15:22:46.727879'
, 'end': '2023-05-26 15:22:46.734876', 'delta': '0:00:00.006997', 'chang
ed': True, 'invocation': {'module_args': {'_raw_params': 'mktemp -d', '_
uses_shell': True, 'warn': True, 'stdin_add_newline': True, 'strip_empty
_ends': True, 'argv': None, 'chdir': None, 'executable': None, 'creates'
: None, 'removes': None, 'stdin': None}}, 'stdout_lines': ['/tmp/tmp.vTp
kvx6RK0'], 'stderr_lines': [], 'failed': False, 'item': 2, 'ansible_loop
_var': 'item'})
Here's my question:
Is there a way to
- abbreviate the output of 'item' in the Ansible console output?
- change the loop so that it iterates differently, like:
with_items: "{{ workdir.results.*.stdout }}"
shell: touch {{ item }}/test-file
- some filter that can modify the items?
- some other clever solution?