How should one go about defining a pretask for role dependencies. I currently have an apache
role that has a user variable so in my own role in <role>/meta/main.yml
I do something like:
--- dependencies: - { role: apache, user: proxy }
The problem at this point is that I still don't have the user I specify and when the role tries to start apache
server under a non existent user, I get an error.
I tried creating a task in <role>/tasks/main.yml
like:
--- - user: name=proxy
But the user gets created only after running the apache
task in dependencies (which is to be expected). So, is there a way to create a task that would create a user before running roles in dependencies?
What is pre_tasks in Ansible? pre_tasks is a task which Ansible executes before executing any tasks mentioned in . yml file. Consider this scenario. You provisioned a new instance on Amazon EC2 cloud or Google Cloud .
Defining pre_tasks in a playbook will cause those tasks to run before all other tasks, including roles. Defining post_tasks is the opposite—these tasks will run after all others, including any handlers defined by other tasks.
Roles provide a framework for fully independent, or interdependent collections of variables, tasks, files, templates, and modules. In Ansible, the role is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse.
Roles allow you to call a set of variables, tasks, and handlers by simply specifying a defined role. Roles require the use of a defined file structure in order to work. Per the Ansible documentation, that structure looks like this… Roles are really just a way to split up your playbook into smaller reusable parts.
I use the pre_tasks to do some tasks before roles, thanks for Kashyap.
#!/usr/bin/env ansible-playbook --- - hosts: all become: true pre_tasks: - name: start tasks and sent notifiaction to HipChat hipchat: color: purple token: "{{ hipchat_token }}" room: "{{ hipchat_room }}" msg: "[Start] Run 'foo/setup.yml' playbook on {{ ansible_nodename }}." roles: - chusiang.vim-and-vi-mode vars: ... tasks: - name: include main task include: tasks/main.yml post_tasks: - name: finish tasks and sent notifiaction to HipChat hipchat: color: green token: "{{ hipchat_token }}" room: "{{ hipchat_room }}" msg: "[Finish] Run 'foo/setup.yml' playbook on {{ ansible_nodename }}." # vim:ft=ansible :
As of Ansible 2.2, you can use include_role
. https://docs.ansible.com/ansible/include_role_module.html
- user: name=proxy - include_role: name: apache vars: user: proxy
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With