Configuring Default Escalation Method
To configure the default privilege escalation method in Ansible, you can use the become
and become_method
options in your Ansible configuration file (typically ansible.cfg
) or in your playbook.
Configuring in ansible.cfg
In the ansible.cfg
file, you can set the default privilege escalation method using the following configuration:
[privilege_escalation]
become=yes
become_method=sudo
This configuration sets the default privilege escalation method to sudo
. You can change the become_method
value to other supported methods, such as su
, pbrun
, pfexec
, doas
, or runas
, depending on the target system's requirements.
Configuring in Playbook
Alternatively, you can configure the default privilege escalation method directly in your Ansible playbook using the become
and become_method
options at the play or task level. For example:
- hosts: webservers
become: yes
become_method: sudo
tasks:
- name: Install Apache web server
apt:
name: apache2
state: present
In this example, the become
and become_method
options are set at the play level, which means that all tasks in the playbook will use the sudo
privilege escalation method by default.
You can also set the become
and become_method
options at the task level if you need to use a different escalation method for specific tasks:
- hosts: webservers
tasks:
- name: Install Apache web server
apt:
name: apache2
state: present
become: yes
become_method: sudo
By configuring the default privilege escalation method, you can ensure that your Ansible playbooks execute tasks with the appropriate permissions, without the need to specify the escalation method for each individual task.