Check the intended execution of Ansible become settings

Make the target user and the scope of user switching explicit.

Description

become_user names the account to switch to. The effective become value can come from the play, task, configuration or variables.

Potential impact

Running as the wrong account can cause a task to fail or change files and settings with unintended privileges.

Remediation

Set become: true and become_user only on plays or tasks that need to switch users. Confirm that the target account exists and that the connection user has permission to switch to it.

Examples

The first excerpt omits an explicit switch. The play in the second excerpt requests the postgres account. Check the account actually running the command with whoami.

Before

yaml
- hosts: localhost
  name: become_user without become
  become_user: bar
  tasks:
    - name: Simple hello
      ansible.builtin.debug:
        msg: hello

- hosts: localhost
  tasks:
    - name: become_user without become
      ansible.builtin.command: whoami
      become_user: mysql
      changed_when: false

After

yaml
- hosts: localhost
  become_user: postgres
  become: true
  tasks:
    - name: some task
      ansible.builtin.command: whoami
      changed_when: false

References