Running a task on multi groups using when

22 views
Skip to first unread message

דודו דודו

unread,
Mar 17, 2020, 2:33:46 AM3/17/20
to Ansible Project
HI, 

I have a task that runs only on servers that are under Kafka group. How can i run the task on Kafka and Webserver groups only.

Thanks

- name: install java-8-openjdk' packages
yum:
name: ['java-1.8.0-openjdk']
state: latest
when: "'kafka' in group_names"

Albert Short

unread,
Mar 17, 2020, 3:14:39 AM3/17/20
to Ansible Project
This is one option I have used in the past

דודו דודו

unread,
Mar 17, 2020, 6:04:36 AM3/17/20
to Ansible Project
I have a partial success that I can not figure what is the problem.
I have 2 tasks, one not in group and the other in group 

The first task is running as required, the second task is skipping all groups - Can not understand why 

- name: install java-11-openjdk' packages
yum:
name: ['java-11-openjdk']
state: latest
when:
- inventory_hostname not in groups.kafka
- inventory_hostname not in groups.kafka_master



- name: install java-8-openjdk' packages
yum:
name: ['java-1.8.0-openjdk']
state: latest
when:
    - inventory_hostname in groups.kafka
- inventory_hostname in groups.kafka_master




TASK [install java-11-openjdk' packages] **********************************************************************************************************************************************************************************
ok: [10.201.51.34]
skipping: [10.201.51.39]
skipping: [10.201.51.40]
skipping: [10.201.51.41]
ok: [10.201.51.38]
ok: [10.201.51.37]
ok: [10.201.51.42]
ok: [10.201.51.36]
skipping: [10.201.51.44]
skipping: [10.201.51.45]
skipping: [10.201.51.46]
ok: [10.201.51.30]
ok: [10.201.51.31]
ok: [10.201.51.35]
ok: [10.201.51.32]
ok: [10.201.51.33]
ok: [10.201.51.5]

TASK [install java-8-openjdk' packages] ***********************************************************************************************************************************************************************************
skipping: [10.201.51.42]
skipping: [10.201.51.34]
skipping: [10.201.51.36]
skipping: [10.201.51.37]
skipping: [10.201.51.38]
skipping: [10.201.51.39]
skipping: [10.201.51.40]
skipping: [10.201.51.41]
skipping: [10.201.51.30]
skipping: [10.201.51.31]
skipping: [10.201.51.32]
skipping: [10.201.51.44]
skipping: [10.201.51.45]
skipping: [10.201.51.46]
skipping: [10.201.51.5]
skipping: [10.201.51.35]
skipping: [10.201.51.33]

Stefan Hornburg (Racke)

unread,
Mar 17, 2020, 6:10:38 AM3/17/20
to ansible...@googlegroups.com
On 3/17/20 11:04 AM, דודו דודו wrote:
> I have a partial success that I can not figure what is the problem.
> I have 2 tasks, one not in group and the other in group 
>
> The first task is running as required, the second task is skipping all groups - Can not understand why 
>
> - name: install java-11-openjdk' packages
> yum:
> name: ['java-11-openjdk']
> state: latest
> when:
> - inventory_hostname not in groups.kafka
> - inventory_hostname not in groups.kafka_master
>
>
> - name: install java-8-openjdk' packages
> yum:
> name: ['java-1.8.0-openjdk']
> state: latest
> when:
> - inventory_hostname in groups.kafka
> - inventory_hostname in groups.kafka_master

That is because both conditions are combined with an AND, so I can't be true.

You can try or (untested):

when: inventory_hostname in groups.kafka or inventory_hostname in groups.kafka_master

Regards
Racke
> On Tuesday, 17 March 2020 08:33:46 UTC+2, דודו דודו wrote:
>
> HI, 
>
> I have a task that runs only on servers that are under Kafka group. How can i run the task on Kafka and
> Webserver groups only.
>
> Thanks
>
> - name: install java-8-openjdk' packages
> yum:
> name: ['java-1.8.0-openjdk']
> state: latest
> when: "'kafka' in group_names"
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/81d98394-39d7-4ec9-90bc-97f024638073%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/81d98394-39d7-4ec9-90bc-97f024638073%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc
Reply all
Reply to author
Forward
0 new messages