On Mon, 10 Apr 2017 05:58:41 -0700 (PDT) [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.
For start, I would suggest looking at this not from perspective of what role depends on the common role, but instead what task in the common role needs to be optional. The optional tasks performed could be viewed as optional "features" of a role. I.e. when you pass in the parameter to the "common" role, it should be more along the lines of "enable_BLAH", "operation_mode" or something similar. You want to put emphasis on what it is that will get run or not. > 1/ The fact that I need to hardcode the rolename is not something > really clean I think. As mentioned above, use a more logical name for the parameter. In this specific case you probably want to have a boolean (True/False) value for it. > 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. Declaring defaults for roles is a perfectly ok thing to do - just keep in mind what I wrote above apropos naming the parameter and its syntax. As a side-note, there are some other ways to handle defaults for undefined variables, like (although, I would say it is better to declare values in defaults/main.yml instead): http://docs.ansible.com/ansible/playbooks_filters.html#defaulting-undefined-variables There is also the "when: foo is defined" syntax (which can be combined with logical and/or as well). But you probably don't need it for your use-case :) > Maybe someone know a nicer way to do this ? For an alternative to meta dependencies (although I have not tried this myself, so take it with a grain of salt), have a look at: http://docs.ansible.com/ansible/include_role_module.html I would find the include_role module more useful in cases where you want to control ordering in more complex way than just the "meta deps first" -> "then this role". Finally, you had two replies related to avoid dependencies via meta, something you should consider as well (I thought it'd be nice to provide at least one more way to handle things :) Best regards -- Branko Majic XMPP: [email protected] Please use only Free formats when sending attachments to me. Бранко Мајић XMPP: [email protected] Молим вас да додатке шаљете искључиво у слободним форматима. -- 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/20170418203417.2aa4e097%40majic.rs. For more options, visit https://groups.google.com/d/optout.
