Ansible Become Root,True,Sudo,User-DecodingDevOps

Ansible Become Root,True,Sudo,User-DecodingDevOps

Ansible become is used for  privilege escalation. ansible become true or become set to yes to activate privilege escalation. ansible become user defines the user which is being used to execute the tasks.

Ansible Become Root OR Ansible Become True

- name: Ensure the httpd service is running
  service:
    name: httpd
    state: started
  become: yes

this task will be executed as the root user. If you mention become: yes in the playbook tasks, the tasks will be executed as the default root user. Because root is the default user for privilege escalation.

Ansible Become User

- name: Run a command as the apache user
  command: somecommand
  become: yes
  become_user: apache

This task will be executed as user apache because the user is explicitly set. So if you want to run the task as different sudo user mention become yes and become_user. If you mention only become_user in the task, it will not do anything with become_user, because become is not set and become default value is false/no.

Connect with different user and run the tasks as a root user

For example, if you want to run all tasks as root on a server named webserver, but you can only connect as the manager user, for this we can mention in inventory file entry like below

webserver ansible_user=manager ansible_become=yes

if you mention like above in inventory file. all the tasks that are going to run on webserverwill run as the root user, but ansible master(control) machine will connect to webserver machine via a user called manager.

 

 

1 Response

Leave a Reply

Your email address will not be published. Required fields are marked *