This sounds like you're re-discovering why OOP is such a pain in the arse. Sorry to be glib but I see this a lot and it's usually easier for everyone if you just avoid the whole meta dependency thing altogether, it's always a maintenance hassle. If you get this to work it's just another maintenance headache for you or the next guy who maintains this playbook.
On 10 April 2017 at 13:58, <[email protected]> wrote: > Hi, > > I have a playbook with many roles plus a common role that is used by each > other roles. > Basically, this common role is used because I have some tasks that are > applicable for all roles, obviously, I don't want to rewrite these tasks as > many time as I have a role. > Recently I encountered a case where I need a task to be played for all roles > except only one. Therefore, I'm looking for a way to detect which role > issued the call to the common role due to dependency so I could filter when > to skip the task or not with a simple `when` statement. > > > > ├── roles > │ ├── common > │ │ ├── defaults > │ │ │ └── main.yml > │ │ └── tasks > │ │ ├── main.yml > │ │ └── test.yml > │ ├── first_role > │ │ ├── meta > │ │ │ └── main.yml > │ │ └── tasks > │ │ └── main.yml > │ └── second_role > │ ├── meta > │ │ └── main.yml > │ └── tasks > │ └── main.yml > └── site.yml > > > > The only way I see at the moment to achieve that would be something like > following: > > > # ./hosts[group1] > laptop ansible_host=127.0.0.1 > > > > # ./site.yml > --- > - hosts: group1 > roles: > - first_role > > > > # ./roles/first_role/meta/main.yml > --- > dependencies: > - { role: common, parent_role: "first_role" } > > > > # ./roles/common/defaults/main.yml > --- > parent_role: > > > > # ./roles/common/tasks/main.yml > --- > - name: common task > debug: > msg: "I'm a common task" > > > - name: common task but not for all roles > debug: > msg: "I'm not a common task for all roles" > when: parent_role != 'first_role' > > > > > 1/ The fact that I need to hardcode the rolename is not something really > clean I think. > > 2/ I'm obliged to declare a default variable for `parent_role` to avoid an > error message : > > "The error was: 'parent_role' is undefined" > > Indeed, when my common role is played and was not called by my role that > doesn't need to exclude the task from being played, I'll get this error > since I do not pass the `parent_role` variable from the other roles. > > > Maybe someone know a nicer way to do this ? > > -- > You received this message because you are subscribed to the Google Groups > "Ansible Project" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/ansible-project/7cd49c8c-99bf-4758-bb1c-129c80306abd%40googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAK5eLPT4SOeMysn_ZUh8aLvhH3EF2%3DvSTT5QM6eiEeKzuohhow%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
