0

I am trying to use include_role with items

---
- hosts: cluster
  tasks:
    - block:
      - name: Execute test role
        include_role:
          name: testrole
        with_items:
        - 'one'
...

My role is

---
- name: Just debugging
  debug:
...

The issue is that it seems that the role is being ran by each host X times per item where X is the number of hosts.

PLAY [cluster] *****************************************************************

TASK [setup] *******************************************************************
ok: [thisNode]
ok: [dww]

TASK [Execute test role] *******************************************************

TASK [testrole : Just debugging] ***********************************************
ok: [thisNode] => {
    "msg": "Hello world!"
}
ok: [dww] => {
    "msg": "Hello world!"
}

TASK [testrole : Just debugging] ***********************************************
ok: [thisNode] => {
    "msg": "Hello world!"
}
ok: [dww] => {
    "msg": "Hello world!"
}

PLAY RECAP *********************************************************************
dww                        : ok=3    changed=0    unreachable=0    failed=0   
thisNode                   : ok=3    changed=0    unreachable=0    failed=0 

Why is this happening and how can I fix it?

Ansible hosts:

[cluster]
thisNode ansible_host=localhost ansible_connection=local
dww

I cannot delegate the task because, in the real role, the task must be executed in each of the hosts.

Using allow_duplicates: no still outputs the same.

---
- hosts: cluster
  tasks:
    - name: Execute test role
      include_role:
        name: testrole
        allow_duplicates: False
      with_items:
      - 'one'
...
Alberto Rivera
  • 3,652
  • 3
  • 19
  • 33

2 Answers2

0

As a workaround you can add allow_duplicates: false to prevent Ansible from running the same role twice with the same parameters.


Clearly the module is looped twice: once with hosts, the other time with the specified items. As it is supposed to runs the action against all hosts, the outer loop gets executed twice.

It's a new module in preview state and this behaviour should probably be files as an issue.

Ansible has an internal parameter BYPASS_HOST_LOOP to avoid such situations and this mechanism should probably be used by this module.

techraf
  • 64,883
  • 27
  • 193
  • 198
  • Thanks for the suggestions. It seems that neither workaround does it for me... I will edit the original post with the results of both workarounds – Alberto Rivera Feb 11 '17 at 02:04
  • It's strange, because I tested `allow_duplicates: false` with your playbook and it did work. So it looks like you are not showing everything. – techraf Feb 11 '17 at 02:09
  • Maybe I am adding it to the wrong place. Where should I add it? – Alberto Rivera Feb 11 '17 at 02:12
  • The block was for a rescue (in the real role). I am, however, just using this mock role to test. BTW, my role is in /etc/ansible/roles/testrole/tasks/main.yml ; how are you running your role? – Alberto Rivera Feb 11 '17 at 02:18
  • Yes, I am running the role and `allow_duplicates: false` does change the behaviour. – techraf Feb 11 '17 at 02:36
0

I had the same problem and allow_duplicates: False did not change anything. It seems that setting serial: 1 on the play and it somehow solved it. This is a workaround that probably will work for a small number of hosts.

  • It didn't work for me. I already raised a ticket for it anyways on ansible's repository: https://github.com/ansible/ansible/issues/21279 – Alberto Rivera Mar 03 '17 at 22:57