Ansible Privilege Escalation
Many system tasks need administrator rights. Installing software, editing files in /etc, and restarting services all require them. Ansible logs in as a normal user and climbs to a higher level for specific tasks. The feature is called privilege escalation, and the keyword is become. Picture a hotel guest with a room key who borrows the master key from the front desk for one job, such as fixing a pipe, and returns it afterward.
The Become Keywords
| Keyword | Purpose | Default |
|---|---|---|
| become | Turns escalation on or off | false |
| become_user | Account that runs the task | root |
| become_method | Tool used to escalate | sudo |
| become_flags | Extra options for the tool | none |
Diagram: How Escalation Flows
Control Node Managed Node
┌──────────┐ SSH as ┌─────────────────────────┐
│ Ansible │──── deploy ──▶│ user: deploy │
└──────────┘ │ │ │
│ │ become: true │
│ ▼ │
│ sudo ──▶ user: root │
│ │ │
│ ▼ │
│ install nginx (allowed) │
└─────────────────────────┘
Enable Escalation in a Playbook
Place become at the play level to cover every task, or at the task level to cover a single step.
- name: Configure web server
hosts: webservers
become: true
tasks:
- name: Install nginx
ansible.builtin.package:
name: nginx
state: present
- name: Create a log summary as the app user
ansible.builtin.shell: wc -l /var/log/app.log > /tmp/count.txt
become: true
become_user: appuser
Task level settings override play level settings. A single play can mix tasks that run as root, as appuser, and as the plain login user.
Enable Escalation on the Command Line
Ad-hoc commands and playbook runs accept short flags.
ansible webservers -m ansible.builtin.package -a "name=nginx state=present" --become
ansible-playbook site.yml --become --ask-become-pass
The flag --ask-become-pass (short form -K) prompts for the sudo password. Teams that automate runs prefer passwordless sudo for a dedicated automation account, or an encrypted password stored with Ansible Vault.
Passwordless Sudo for an Automation Account
Create a file on the managed node with the visudo tool so a syntax error cannot lock the system.
sudo visudo -f /etc/sudoers.d/deploy
deploy ALL=(ALL) NOPASSWD: ALL
Restrict the rule to specific commands when security policy demands tighter control.
Other Escalation Methods
Sudo is the most common method. Ansible supports others through become_method.
- su switches users with a target account password
- doas serves systems that use the OpenBSD tool
- pbrun and pfexec serve enterprise and Solaris environments
- runas serves Windows hosts
Set Defaults in ansible.cfg
[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False
Troubleshooting
- The message "Missing sudo password" means the account needs a password or a sudoers rule.
- The message "Failed to set permissions on the temporary files" appears when becoming an unprivileged user. Install the
aclpackage on the managed node or enable pipelining. - A task that works manually but fails in Ansible often depends on environment variables. Add
become_flags: '-i'to load a full login environment.
Security Habits
- Escalate only the tasks that need it
- Use a dedicated automation account rather than a personal one
- Limit sudo rules to the commands the account needs
- Store passwords in Ansible Vault instead of plain text files
Try This
Run ansible localhost -m ansible.builtin.command -a "whoami" and note the output. Run the same command with --become. The result changes from your username to root, which confirms escalation works.
Summary
Privilege escalation lets Ansible log in as a regular user and run chosen tasks with higher rights. The become keyword family controls the switch at play, task, command line, or configuration file level. Sudo serves most Linux environments, and other methods cover special systems. A dedicated automation account with narrow sudo rules keeps the setup safe and repeatable.
