Hello. I would like to reuse some ansible code in roles tasks. For example, installation of packages for multiple destination operation systems from local files:
roles/includes_common/pkg_from_files.yaml:
`
Hello. I would like to reuse some ansible code in roles tasks. For example, installation of packages for multiple destination operation systems from local files:
roles/includes_common/pkg_from_files.yaml:
`
A few pedantic things: Roles don’t actually hold code, and this seems to be more of a question about how to split up things that vary between OS than about reuse.
Attaching a “when” to an include is fine if you’d like to do this, or you could keep the tasks inline. This is fine.
The “pkg_from_files.yaml” stuff looks pretty darn terrible (no offense), so I’m not sure what you are trying to do there.
It looks like you’re trying to build up some kind of package abstraction - by advice to you on this is STOP.
Why?
Not just package names differ between systems, the apps are packaged differently. It’s important to know how your package differs between platforms (think about Apache between Ubuntu and CentOS) and deal with those things, and you shouldn’t try to bury them.
Trying to use roles to hide this defeats the purpose of not hiding this, in other words.
Otherwise we’d just have a “package” module, but we explicitly do not, for the reason that we want folks to think about what OSes things run on.
Is it possible to use roles from other roles in ansible? I’m unable to found way how.
Hi Dmitry,
It is not supported directly from the syntax however you can use role
dependency to achieve almost the same result
http://docs.ansible.com/playbooks_roles.html#role-dependencies
--Best, Igor