0

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!"
}

1 Answers1

1
  when: item in group_names

when keywords are already implicitly in a Jinja expression, so delete {{ }} in them.

However your play still has problems.

Regarding the Verification task in task.yml, debug only prints things, it doesn't really do verification like an assert task does. Also, you are loop:ing and are running on multiple hosts, which runs 2 * 2 = 4 times, and probably not what you want, resulting in some extra skips.

I don't see how the debug task has value, and would delete it entirely. There are other ways to review the contents of groups, such as the ansible-inventory command line tool.

An expression in the name of a task will not work as you might expect. I think it will only reflect the first host or something weird like that.

- hosts: all
  connection: local

Do not use multiple hosts with connection local. It will run things multiple times but not ssh anywhere, which is probably not what you want. Action plugins like debug are already inherently run on localhost. Or you can put localhost is the hosts pattern. So:

  1. Delete connection: local at the play level.
  2. Use a hosts pattern more specific than all. hosts: port_22,port_222 will run on both groups, and is more clear as to what you intend.
John Mahowald
  • 30,009
  • 1
  • 17
  • 32
  • As you suggested, I modified the condition `when: item in group_names`,deleted the `connection: local` and added host pattern `hosts: port_22,port_222` , and pasted the output. But the execution not happening correctly on remote hosts and the output is coming empty as its `skipping` the hosts – divyashree kumar May 04 '22 at 02:27