I have a simple ansible main_task.yml file which is looped from a main.yml .
inventory.ini
[port_22]
192.168.0.189
192.168.0.199
[port_222]
192.168.0.199
[port_888]
192.168.0.200
main_task.yml
---
- name: "run this on {{ item }} hosts"
debug: msg= "this runs only on p{{ item }} hosts"
when: "{{ item }} in group_names"
#when: group_names | select("item|string") | list | count > 0
main.yml
- hosts: port_22, port_222
connection: local
vars:
ports:
- 22
- 222
tasks:
- name: Verification
include_tasks: main_task.yml
loop: "{{ ports }}"
Its complaining
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ item }} in group_names
when removing jinja2 template, getting this :
skipping: [192.168.0.189]
How can I achieve this ?
Update:
Modified main.yml
- hosts: host_22, host_222
#connection: local
vars:
ports:
- 22
tasks:
- name: deploy files with network address in them
include_tasks: main_task.yml
loop: "{{ ports }}"
Modifed main_task.yml
- name: "run this on {{ item }} hosts"
debug: msg= "this runs only on p{{ item }} hosts"
when: item in group_names
#when: group_names | select("item|string") | list | count > 0
- name: my hostname
shell: hostname; hostname -I
when: item in group_names
register: hostname_result
- debug:
var: hostname_result.stdout_lines
Output:
ansible-playbook main.yaml -i inventory.ini -u root
PLAY [host_22, host_222] *********************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************************************************
ok: [192.168.0.189]
ok: [192.168.0.199]
TASK [Verification] *********************************************************************************************************************************************************************************************************************************
included: /home/ubuntu/ansible/test/testing_loo/main_task.yml for 192.168.0.199, 192.168.0.189 => (item=22)
TASK [run this on 22 hosts] ******************************************************************************************************************************************************************************************************************************************************
skipping: [192.168.0.199]
skipping: [192.168.0.189]
TASK [my hostname] ***************************************************************************************************************************************************************************************************************************************************************
skipping: [192.168.0.199]
skipping: [192.168.0.189]
TASK [debug] *********************************************************************************************************************************************************************************************************************************************************************
ok: [192.168.0.199] => {
"hostname_result.stdout_lines": "VARIABLE IS NOT DEFINED!"
}
ok: [192.168.0.189] => {
"hostname_result.stdout_lines": "VARIABLE IS NOT DEFINED!"
}