Hi All,
My host file looks like below where I am using specific host variables “smca_role”.
[smca]
smca1 ansible_host=10.66.69.194 smca_role=True
smca2 ansible_host=10.66.69.193 smca_role=False
My playbook task looks like below. I am delegating a task to the above ‘smca’ group and it is going to run on both the hosts which I don’t want.
- name: Check the flag for smca role
shell: Some Command
register: enable_result
delegate_to: ‘{{ item }}’
with_items: ‘{{ groups.smca }}’
My problem: I want to run it on only one host which matches the condition “smca_role=True”. How can I use jinja template & put if/when condition inside with_items: ‘{{ groups.smca }}’ so that it runs only on the required host ?
Try this.
`
- name: Check the flag for smca role
shell: Some Command
register: enable_result
delegate_to: ‘{{ groups.smca[0] }}’
`
The [0] gives you first member of the list.
To be honest if you are just doing tasks in a playbook I might just have a second play in the playbook to address the host I needed as I think it makes the playbook more readable.
`
-
hosts: smca
tasks:
-
name: connect test
wait_for_connection:
-
hosts: smca_lead_host
tasks:
-
name: do a shell thing
shell: echo Hello
register: enable_result
`
But it depends on what you are trying to achieve.
Hope this helps,
Jon
Hi Jon,
Thanks for your response. ‘{{ groups.smca[0] }}’ won’t work in my case as there is no guarantee that only the first host will have that condition true it can keep changing & smca group can have more than 2 hosts. The only thing unique would be one host will have **"**smca_role=True" and that’s the reason I want to check with that variable and then delegate. I guess there should be someway to specific conditions inside jinja template.
The separate playbook won’t work in what I am trying to achieve.
when: smca_role | bool
will give you that, only run on host where smca_role is True.
Hi Kai,
Thank You for the response. Using plain when condition won’t work in my case as the playbook is run on another host group where I’m gathering/setting facts and I’m using those facts when delegating it to “smca” host group.
My only requirement is to put this kind of when condition inside “with_items” so that it selects host which has "smca_role=True " and delegates the task only to that particular host.
And if you will try to separate main playbook to playbook + role?
Like that:
hosts:
[smca]
smca1 ansible_host=10.66.69.194
smca2 ansible_host=10.66.69.193
playbook.yml:
- hosts: localhost
roles:
- { role: some_role, node: smca1, smca_role: True }
- { role: some_role, node: smca2, smca_role: False }
some_role/tasks/main.yml
- name: Check the flag for smca role
shell: Some Command
register: enable_result
delegate_to: ‘{{ node }}’
when: smca_role == “True”
If it isn’t working for you I suggest splitting it up and doing a set_fact to determine the host that has smca_role=True set. That way you can just delegate_to: the relevant host and don’t need to use ‘with_items’.
Hope this helps,
Jon