Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify ansible pretasks for a role?

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?

like image 256
Kęstutis Avatar asked Mar 25 '15 14:03

Kęstutis


People also ask

What is pre tasks in Ansible?

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 .

What is pre Task & POST task in Ansible?

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.

How do you define a role in Ansible playbook?

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.

How can variables be defined within a role in Ansible?

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.


2 Answers

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 : 
like image 127
Chu-Siang Lai Avatar answered Sep 26 '22 23:09

Chu-Siang Lai


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 
like image 32
kimamula Avatar answered Sep 23 '22 23:09

kimamula