Hi RG,
Here is a solution for that problem which I have used for a long time, maybe before ansible tags were available.
tasks/main.yml:
- name: include client
import_tasks: client.yml
when: run_nfs_client is defined
- name: include server
import_tasks: server.yml
when: run_nfs_server is defined
playbook.yml:
---
- hosts: nfs_clients
become: true
roles:
- {role: 'nfs_role', run_nfs_client: true}
- hosts: nfs_servers
become: true
roles:
- {role: 'nfs_role', run_nfs_server: true}
---
Let's consider tags instead.
You wrote "this doesn't work". What exactly happened?
If you run your playbook with an ordinary call to ansible-playbook, it will follow the default behavior: "--tags all - run all tasks, ignore tags (default behavior)". So, it will 'ignore tags'. If it's ignoring tags, whatever solution you tried to implement with tags won't take effect.
Next, if you run this:
ansible-playbook -t create_nfs_cmp
It will execute on the cmp hosts. But it will run the whole nfs role (unless you have tagged specific tasks).
A step towards a solution could be to add the tags in tasks/main.yml instead.
tasks/main.yml:
- name: include nfs server
import_tasks: install_cmp.yml
tags:
- nfs_server_tag
- name: include nfs clients
import_tasks: install_machines.yml
tags:
- nfs_client_tag
Then if you have a playbook clients.yml as follows:
clients.yml:
- hosts: nfs_clients
become: true
roles:
- nfs_role
and run it:
ansible-playbook -t nfs_client_tag clients.yml
or similarly create this,
ansible-playbook -t nfs_server_tag servers.yml
So, that should work. But how to join them all together in one big playbook without resorting to the trick I mentioned at the beginning? Is that method still necessary?
A confusion with tags is to distinguish between labeling tasks versus choosing which ones will be processed. It's easy to get those two sides of the equation mixed up. In order to select which tags will get processed you have to run the ansible-playbook command with the -t flag.