Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define `become=yes' per role with Ansible

Tags:

ansible

In my system provisioning with Ansible, I don't want to specify become=yes in every task, so I created the following ansible.cfg in the project main directory, and Ansible automatically runs everything as root:

[privilege_escalation] become = True 

But as the project kept growing, some new roles should not be run as root. I would like to know if it is possible to have some instruction inside the role that all tasks whithin that role should be run as root (eg. something in vars/), instead of the global ansible.cfg solution above!

like image 533
Teresa e Junior Avatar asked Aug 27 '16 16:08

Teresa e Junior


People also ask

What is become yes in Ansible?

Adding become: yes and become_method: enable instructs Ansible to enter enable mode before executing the task, play, or playbook where those parameters are set.

How do you define roles in Ansible?

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.

What does become Yes mean in Ansible playbooks Mcq?

What does 'become: yes' mean in Ansible playbooks? command must be retried until it succeeds. service needs to be started once installed. we would run all commands as root. worker node should become a manager node.


1 Answers

I have found a solution, although I think a better solution should be implemented by the Ansible team. Rename main.yml to tasks.yml, and then write the following to main.yml:

--- - { include: tasks.yml, become: yes } 

Another solution is to pass the parameter directly in site.yml, but the main idea of the question was reusing the role in other projects without forgetting it needs root:

--- - hosts: localhost   roles:     - { role: name, become: yes } 
like image 192
Teresa e Junior Avatar answered Oct 08 '22 03:10

Teresa e Junior