Question:
my inventory file’s contents –
1 2 3 4 5 6 |
[webservers] x.x.x.x ansible_ssh_user=ubuntu [dbservers] x.x.x.x ansible_ssh_user=ubuntu |
in my tasks file which is in common role i.e. it will run on both hosts but I want to run a following task on host webservers not in dbservers which is defined in inventory file
1 2 3 4 5 6 7 |
- name: Install required packages apt: name={{ item }} state=present with_items: - '{{ programs }}' become: yes tags: programs |
is when module helpful or there is any other way? How could I do this ?
Answer:
If you want to run your role on all hosts but only a single task limited to the webservers
group, then – like you already suggested – when
is your friend.
You could define a condition like:
1 2 |
when: inventory_hostname in groups['webservers'] |