Having a playbook that contains 2 plays,
- one that will run a command through the shell module and save the output to a result file on the Control Node (
localhost
) for all the hosts in the inventory - and another one that will only run on the Control Node and that is meant to change the ownership permissions to that result file,
like this:
---
- name: Run and save shell command - 1st play
hosts: all
gather_facts: False
serial: 10
become_flags: -i
become: yes
vars:
res: "/home/myuser/test"
tasks:
- name: run shell command
shell: 'pwd'
register: myshellout
- name: save output
shell: echo -e "{{ inventory_hostname }}{{ myshellout.stdout }}\n" >> "{{ res }}"
delegate_to: localhost
- name: change ownership - 2nd play
hosts: localhost
gather_facts: no
tasks:
- name: change ownership task
file:
path: "{{ res }}"
owner: myuser
group: myuser
# delegate_to: localhost
# run_once: true
Please note that I run the Ansible playbook like this:
ansible-playbook -i myinventory -e 'res=/home/myuser/mycustomfile' --limit 'host1,host2,host3' myansibleplaybook.yml
so it's not all the hosts in the inventory.
Why do I get the skipping: no hosts matched
comment for the 2nd play and how can I execute the change of ownership task only once, at the end of the play?