Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include_role on a loop running unexpected number of times #21279

Closed
albertor24 opened this issue Feb 11, 2017 · 7 comments
Closed

include_role on a loop running unexpected number of times #21279

albertor24 opened this issue Feb 11, 2017 · 7 comments
Assignees
Labels
affects_2.2 This issue/PR affects Ansible v2.2 bug This issue/PR relates to a bug. include_role support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@albertor24
Copy link

albertor24 commented Feb 11, 2017

ISSUE TYPE
  • Bug Report
COMPONENT NAME

include_role

ANSIBLE VERSION
$ ansible --version
ansible 2.2.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o override
CONFIGURATION
[defaults]
inventory = /etc/ansible/hosts
private_key_file = /home/insecure_private_key
transport = ssh
host_key_checking = False
jinja2_extensions = jinja2.ext.do
gathering = smart
gather_subset = network
roles_path = /usr/share/ansible/roles

/etc/ansible/hosts

[cluster]
thisNode ansible_host=localhost ansible_connection=local
dww
OS / ENVIRONMENT

Ubuntu 14.04

SUMMARY

Using include_role in a loop seems to be executed twice: once with hosts, and another with the specified items.

STEPS TO REPRODUCE

ansible-playbook testPlaybook.yml

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

/usr/share/ansible/roles/testrole/tasks/main.yml

---
- name: Just debugging
  debug:
...
EXPECTED RESULTS

I expected to see testrole executed once, since I am only passing a single item to it

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

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

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

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

I see testrole executed twice

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

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

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

TASK [testrole : Just debugging] ***********************************************
ok: [dww] => {
    "msg": "Hello world!"
}
ok: [thisNode] => {
    "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
@ansibot ansibot added affects_2.2 This issue/PR affects Ansible v2.2 bug_report module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. labels Feb 11, 2017
@bcoca bcoca removed the needs_triage Needs a first human triage before being processed. label Feb 13, 2017
@bcoca
Copy link
Member

bcoca commented Feb 13, 2017

confirmed the behaviour, it should run include_role per host but not for all hosts per host.

@LuckySB
Copy link
Contributor

LuckySB commented Mar 26, 2017

ansible version: 2.4.0-100.git201703152001.517cdbe.devel
I wanted to make a config with dynamic roles

But each role is played as many times as the number of hosts with it is specified in the inventory
And each role is played on each host!

my roles and playbook:
https://github.com/LuckySB/ansible_include_role_bug
3 host with list variable roles:

ds02.test.com

ansible_host: 192.168.100.2
roles:
  - base
  - admin
  - sshd

vs01.test.com

ansible_host: 192.168.100.1
roles:
  - base
  - sshd

vs110.test.com

ansible_host: 192.168.100.110
roles:
  - base

main role:

- name: include role
  include: '{{inner_item}}.yml'
  with_items: "{{ roles }}"
  loop_control:
    loop_var: inner_item
  static: no

include task:

- debug:
    msg: "include: base, host: {{ansible_hostname}}"

- name: base role
  include_role:
    name: base

Actual result:

role base played 3 times on every host
role sshd played 2 times on every host
role admin played 1 time on every host

each

  include: '{{inner_item}}.yml'

played one time only for host with item in variable 'roles', but
each include_roles launch role execution on ALL host from playbook

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [vs01.centosadmin.ru]
ok: [vs110.centosadmin.ru]
ok: [ds02.centosadmin.ru]

TASK [main : include role] *****************************************************
included: /home/robot/bug/roles/main/tasks/base.yml for vs110.centosadmin.ru, vs                                                                                        01.centosadmin.ru, ds02.centosadmin.ru
included: /home/robot/bug/roles/main/tasks/sshd.yml for vs01.centosadmin.ru, ds0                                                                                        2.centosadmin.ru
included: /home/robot/bug/roles/main/tasks/admin.yml for ds02.centosadmin.ru

TASK [main : debug] ************************************************************
ok: [ds02.centosadmin.ru] => {
    "changed": false,
    "msg": "include: base, host: ds02"
}
ok: [vs01.centosadmin.ru] => {
    "changed": false,
    "msg": "include: base, host: vs01"
}
ok: [vs110.centosadmin.ru] => {
    "changed": false,
    "msg": "include: base, host: vs110"
}

TASK [main : base role] ********************************************************

TASK [base : debug] ************************************************************
ok: [ds02.centosadmin.ru] => {
    "changed": false,
    "msg": "This is role base, host: ds02"
}
ok: [vs01.centosadmin.ru] => {
    "changed": false,
    "msg": "This is role base, host: vs01"
}
ok: [vs110.centosadmin.ru] => {
    "changed": false,
    "msg": "This is role base, host: vs110"
}

TASK [base : debug] ************************************************************
ok: [ds02.centosadmin.ru] => {
    "changed": false,
    "msg": "This is role base, host: ds02"
}
ok: [vs01.centosadmin.ru] => {
    "changed": false,
    "msg": "This is role base, host: vs01"
}
ok: [vs110.centosadmin.ru] => {
    "changed": false,
    "msg": "This is role base, host: vs110"
}

TASK [base : debug] ************************************************************
ok: [ds02.centosadmin.ru] => {
    "changed": false,
    "msg": "This is role base, host: ds02"
}
ok: [vs01.centosadmin.ru] => {
    "changed": false,
    "msg": "This is role base, host: vs01"
}
ok: [vs110.centosadmin.ru] => {
    "changed": false,
    "msg": "This is role base, host: vs110"
}

TASK [main : debug] ************************************************************
ok: [ds02.centosadmin.ru] => {
    "changed": false,
    "msg": "include: sshd, host: ds02"
}
ok: [vs01.centosadmin.ru] => {
    "changed": false,
    "msg": "include: sshd, host: vs01"
}

TASK [main : sshd role] ********************************************************

TASK [sshd : debug] ************************************************************
ok: [ds02.centosadmin.ru] => {
    "changed": false,
    "msg": "This is role sshd, host: ds02"
}
ok: [vs01.centosadmin.ru] => {
    "changed": false,
    "msg": "This is role sshd, host: vs01"
}
ok: [vs110.centosadmin.ru] => {
    "changed": false,
    "msg": "This is role sshd, host: vs110"
}

TASK [sshd : debug] ************************************************************
ok: [ds02.centosadmin.ru] => {
    "changed": false,
    "msg": "This is role sshd, host: ds02"
}
ok: [vs01.centosadmin.ru] => {
    "changed": false,
    "msg": "This is role sshd, host: vs01"
}
ok: [vs110.centosadmin.ru] => {
    "changed": false,
    "msg": "This is role sshd, host: vs110"
}

TASK [main : debug] ************************************************************
ok: [ds02.centosadmin.ru] => {
    "changed": false,
    "msg": "include: admin, host: ds02"
}

TASK [main : admin role] *******************************************************

TASK [admin : debug] ***********************************************************
ok: [ds02.centosadmin.ru] => {
    "changed": false,
    "msg": "This is role admin, host: ds02"
}
ok: [vs01.centosadmin.ru] => {
    "changed": false,
    "msg": "This is role admin, host: vs01"
}
ok: [vs110.centosadmin.ru] => {
    "changed": false,
    "msg": "This is role admin, host: vs110"
}

PLAY RECAP *********************************************************************
ds02.centosadmin.ru        : ok=13   changed=0    unreachable=0    failed=0
vs01.centosadmin.ru        : ok=11   changed=0    unreachable=0    failed=0
vs110.centosadmin.ru       : ok=9    changed=0    unreachable=0    failed=0

@snowsky
Copy link
Contributor

snowsky commented Mar 26, 2017

Each item in loop runs twice.

@snowsky
Copy link
Contributor

snowsky commented Mar 30, 2017

With this PR, #23104, and the below playbook:

---
- hosts: cluster
  strategy: debug
  gather_facts: no
  tasks:
    - name: Execute test role
      include_role:
        name: "testrole_{{item}}"
      with_items:
      - 'one'
      - 'two'
      - 'three'

I got the below output:

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

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

TASK [testrole_one : Just debugging] ***************************************************************************
ok: [localhost] => {
    "changed": false,
    "msg": "Hello world!"
}
ok: [thisNode] => {
    "changed": false,
    "msg": "Hello world!"
}

TASK [testrole_two : Just debugging] ***************************************************************************
ok: [thisNode] => {
    "changed": false,
    "msg": "Hello world!"
}
ok: [localhost] => {
    "changed": false,
    "msg": "Hello world!"
}

TASK [testrole_three : Just debugging] *************************************************************************
ok: [thisNode] => {
    "changed": false,
    "msg": "Hello world!"
}
ok: [localhost] => {
    "changed": false,
    "msg": "Hello world!"
}

TASK [testrole_one : Just debugging] ***************************************************************************
ok: [thisNode] => {
    "changed": false,
    "msg": "Hello world!"
}
ok: [localhost] => {
    "changed": false,
    "msg": "Hello world!"
}

TASK [testrole_two : Just debugging] ***************************************************************************
ok: [thisNode] => {
    "changed": false,
    "msg": "Hello world!"
}
ok: [localhost] => {
    "changed": false,
    "msg": "Hello world!"
}

TASK [testrole_three : Just debugging] *************************************************************************
ok: [thisNode] => {
    "changed": false,
    "msg": "Hello world!"
}
ok: [localhost] => {
    "changed": false,
    "msg": "Hello world!"
}

PLAY RECAP *****************************************************************************************************
localhost                  : ok=6    changed=0    unreachable=0    failed=0
thisNode                   : ok=6    changed=0    unreachable=0    failed=0

Each item in loop runs once in sequence, but this happened twice.

@callipeo
Copy link
Contributor

callipeo commented Apr 3, 2017

Could this be a duplicate of #18748?

@ansibot ansibot added the support:core This issue/PR relates to code supported by the Ansible Engineering Team. label Jun 29, 2017
@Vladimir-csp
Copy link

I got distracted by the reference after comment about the duplicate. Yes, this is clearly a duplicate of #18748

@ansibot ansibot removed the module This issue/PR relates to a module. label Nov 23, 2017
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 1, 2018
@mkrizek
Copy link
Contributor

mkrizek commented Apr 10, 2018

Fixed in #30372

@mkrizek mkrizek closed this as completed Apr 10, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.2 This issue/PR affects Ansible v2.2 bug This issue/PR relates to a bug. include_role support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

No branches or pull requests

10 participants