role dependencies, with variables as arguments, called from multiple roles

106 views
Skip to first unread message

Robb Wagoner

unread,
Nov 27, 2014, 10:32:27 AM11/27/14
to ansible...@googlegroups.com
I'd like for roles which configure Node apps, to generate some snippets for an HAProxy config. I'd like to be able to call the same role dependency, app_snippet, in a generalized way, across many apps (app1, app2, etc.). However, the dependency executes once, even though I am using the allow_duplicates: yes in the meta/main.yml for each app. 

In short, I'd like role app_snippet to be completely data-driven and called as a dependency of each app, and executed once for each app, using vars from the calling app role.

Am I missing something fundamental with Variable scoping? Or is this an anti-pattern in Ansible? The allow_duplicates: yes boolean seems to only be in the scope of the calling app role, not global across roles.


PLAYBOOK: apps.yml

- name: install apps
  hosts: webservers
  roles:
    - { role: app1 }
    - { role: app2 }


ROLES:
roles/app1/
roles/app2/
roles/app_snippet/


ROLE appN: (i.e. app1 and app2 are mostly the same, for the purposes of role dependency)

roles/appN/vars/main.yml
---
app: appN
install_dir: /opt/{{app}}


roles/appN/meta/main.yml
---
allow_duplicates: yes
dependencies:
  - { role: app_snippet }


roles/app_snippet/tasks/main.yml
---
- name: HAProxy Backend Snippet {{app}}
  template: src=haproxy_backend.cfg.j2 dest=/etc/haproxy/conf/backend.d/{{app}}.cfg


I only see the task app_snippet | HAProxy Backend Snippet app1 execute. I don't see it execute for app2

The same is true (runs once) if I assign a static parameter to the app_snippet role dependency for each app:

roles/app1/meta/main.yml
---
allow_duplicates: yes
dependencies:
  - { role: app_snippet, app: app1 }

roles/app2/meta/main.yml
---
allow_duplicates: yes
dependencies:
  - { role: app_snippet, app: app2 }

This suggests that it is not a variable scoping issue. But it does suggest that late interpolation of variables causes the app_snippet tasks in both calls to be considered "the same", and subsequently executed once.

Any help/pointers are appreciated! Thanks!

Robb

James Morgan

unread,
Mar 10, 2015, 4:28:00 AM3/10/15
to ansible...@googlegroups.com
Hi,

I'm seeing the same problem and have a very similar use case.

allow_duplicates = yes doesn't seem to be working as I would expect. Unsure if its a bug or I have misunderstood how it works

James
Reply all
Reply to author
Forward
0 new messages