--
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 ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Roles are tasks.From a roles/foo/tasks/main.yml you can include other task files.
Further, you can include a loose task section with roles just fine.
Hi,
I'm looking into using Ansible to manage my systems and I'm wondering about how to make my roles/tasks composable.
As far as I understand roles are Ansibles version of reusable components but it seems they do not really support any sort of ordering/dependencies. Let me give an example of what I'm trying to accomplish:
- I download a role "mysql-server" from github that installs the mysql server package and does some initial configuration, etc.
- When setting up a system I also need
a) install a repo before the mysql-server role
b) do some customization to mysql after the mysql-server role (add users, etc.)ou'll
c) apply the "register-server" role which performs various tasks to mark the server as ready for work
From what I understand there is no way to mix roles and tasks which makes me wonder how to implement such self-contained roles that can be used as if they were regular tasks? I guess what I'm asking for is some sort of "apply_role" action that would make it possible to build more complex playbooks out of self-contained components.
Is this already possible and I just missed it or is there something wrong with what I'm trying to do?
The problem is that this only works in very specific ways and is no general solution to making things truly reusable.
I understand that using this approach can be used for coarse grained:
- task
- role(s)
- task
but what if I have more complex compositions like:
- task
- role
- task
- role
- task
- ...
For this the pre/post approach is not usable.
What I'm really getting at is that roles should be first class citizens and be usable anywhere an action can be used.
--
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 ansible-project+unsubscribe@googlegroups.com.
And you can still do "../../other_role/tasks/foo.yml" ... out of tasks, out of the role, relative.
--
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 ansible-proje...@googlegroups.com.
Hi,
One way of realizing such tasks/roles interleaving could be to separate it into multiple plays inside one playbook. For example I used to process everything in 4 plays:
- Bootstrap play - only bootstraps Ansible (installs python and python-apt with raw module)
- Basic play - configures basic system settings, such as repositories
- Infrastructure play - installs and does basic configuration of services (eg. MySQL, Nginx...)
- Application play - installs and configures the application
But after thinking about it and refactoring I only use Bootstrap, Basic/Infrastructure (only does what Basic used to do) and Application (only installs roles of applications that now contain requirements for services (what used to be in Infrastructure)). This is simply possible with the use of "meta: flush_handlers". But real dependencies between roles would be much better.
Greetings,
gw
On Wed, 1 May 2013 06:23:43 -0700 (PDT), Dennis Jacobfeuerborn wrote:
But there are not four tasks but two tasks and two roles interleaved and the roles "mysql_server" and "register_db_server" are not written by me but by somebody else and git-pulled into the roles directory. I only want to change these for bug fixes/features I want to contribute to upstream and pull updated versions but not for customizations. These customizations would happen in a higher level role/playbook like the one in my example.
Later I might want put this higher level role on github for other users to use and extend in the same way.
If I understand the things explained to me so far correctly about syntax and features in Ansible than what comes closest to this is the following version of the example above:
- name: configure repo for patched mysql server
copy: src=my-repo.repo dest=/etc/yum.repos.d/my-repo.repo
- include: ../../mysql-server/tasks/main.yml root_password=test
- name: add additional mysql user
mysql_user: name=admin host='%' ...
- include: ../../register_db_server/tasks/main.yml
This should work but but has two issues:
1. Having to specify the fill path to the main.yml of the role is somewhat ugly when I really want to say "apply role X". This is not that big a deal but so far I find Ansibles "language" rather elegant and concise and it would be more explicit if I could declare that I want to apply a role rather than a specific playbook in a directory that just happens to be a role.
2. When a dependency is missing (e.g. I forgot to download the "mysql_server" role) I get a "File not found" error for the main.yml file. If the inclusion of a role could be specified more explicitly than this could be turned into a "Role or File X not found" message.
So basically this is not so much about features but about improving the syntax for this.
Here are three ways this could potentially be solved. All of them would probably require the addition of a "roles_path = (::...)" option in ansible.cfg:
1. Make include not only look for specified files but also look if a role with the name exists and include "//tasks/main.yml" in that case.
The result would look like this:
- include: mysql_server key=value
Ansible would see that a role "mysql_server" exists in and include "/mysql_server/tasks/main.yml". If no role or file with that name exists the error would be "Role or file mysq_server not found".
The downside of this is that technically a file that is literally called "mysql_server" could exist in the current directory so the reference could be ambiguous. Not sure how likely that is though.
2. Add the syntax "- role: mysql_server key=value" to the language that would internally simply call include "/mysql_server/tasks/main.yml".
This would be more explicit and disambiguate the inclusion of playbook files vs. the inclusion of roles but it would also introduce a new element to the language which might not be desired.
3. With the following approach I'm not sure if this is even possible as it basically turns role inclusion into a module call and I'm not sure about whether "treat roles like modules" is even possible because of how playbooks and modules interact (I think Ansible calls certain hook function of the module which would not be possible with a role). So just ignore the following if it is too crazy.
Make a module invocation not only look for a module with the specified name but also for a role (similar to case 1. above):
- name: setup mysql server
mysql_server: key=value
In this case Ansible would not find a module "mysql_server" but instead of aborting with an error would first look for a role called "mysql_server" and if that exists include that instead similar to the cases above. This basically turn roles into modules except that you don't have to write an actual module in python/ruby/erlang.
On Wednesday, May 1, 2013 5:56:34 AM UTC+2, Michael DeHaan wrote:
In your above case, everything in those four tasks should be a role "mysql-server".And the role could be broken up into smaller task files to keep it organized.Roles can of course still be parameterized and you can have more than one instance of a role applied.
On Tue, Apr 30, 2013 at 11:52 PM, Dennis Jacobfeuerborn <djacobf...@gmail.com> wrote:
On Wednesday, May 1, 2013 3:04:38 AM UTC+2, Michael DeHaan wrote:And you can still do "../../other_role/tasks/foo.yml" ... out of tasks, out of the role, relative.
What I am hoping for is that roles are treated in a similar way to modules. For modules there is a search path and I can reference a module without having to specify where they live no matter from where I reference them. When I use "action: " then Ansible finds in the module library. What I would like to be able to do is to use something like "role: mysql_server" and then have Ansible look up the module in the "role library" and include /mysql_server/tasks/main.yaml.
That way people could create roles on github and users could git-clone these into the roles directory and then reference these no matter where the playbook they execute lives relative to that role.
The resulting playbook for my example would then look like this:
- name: configure repo for patched mysql server
copy: src=my-repo.repo dest=/etc/yum.repos.d/my-repo.repo
- name: install and prepare mysql
role: mysql-server root_password=test
- name: add additional mysql user
mysql_user: name=admin host='%' ...
- name: register this database server with several external services
role: register_db_server
Here roles are basically used like modules and that is what I meant when I said they should become first class citizens.
--
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 ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
Hi,
One way of realizing such tasks/roles interleaving could be to separate it into multiple plays inside one playbook. For example I used to process everything in 4 plays:
- Bootstrap play - only bootstraps Ansible (installs python and python-apt with raw module)
- Basic play - configures basic system settings, such as repositories
- Infrastructure play - installs and does basic configuration of services (eg. MySQL, Nginx...)
- Application play - installs and configures the application
But after thinking about it and refactoring I only use Bootstrap, Basic/Infrastructure (only does what Basic used to do) and Application (only installs roles of applications that now contain requirements for services (what used to be in Infrastructure)). This is simply possible with the use of "meta: flush_handlers". But real dependencies between roles would be much better.
Greetings,
gw
I should add that Michael is doing an excellent good job improving these issues.I'm explicitly glad to see defaults/main.yml added to roles. This was my largest issue with sharing roles.
--
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 ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/626b051f-fb47-4cfd-9d98-be31184a2156%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/9bghHncUQCM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CA%2BnsWgy_FJe__7Ei0Ub8QQVKZfF0V3o6U4e6XPGmDPSOuB3Vww%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CA%2BQ8zTy4tFwg4j%2BXpYwqqt712g%3DL%3DWuhTwizwnm5ow9BGe1NSg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CA%2BQ8zTzs_rS_8YnPME5er6XZWXXda2Ue%3DqoiQwnnSaSVRtN68w%40mail.gmail.com.
There will not be a task that applies roles, the "role" directive is for this.