0Pricing
Ansible Academy · Lesson

Privilege Escalation with become

Run tasks as root using sudo.

Privilege Escalation with become is a free Ansible Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Some Tasks Need Root

Installing packages or restarting services usually needs root. Ansible handles that with privilege escalation, not a separate root login. 🔐

Enable It with become

Add the --become flag, often shortened to -b, to run your ad-hoc command with elevated privileges on the remote host.

ansible all -b -m package -a "name=git state=present"

sudo Is the Default Method

By default become uses sudo to escalate. That works on most Linux systems without any extra configuration.

ansible web -b -m service -a "name=nginx state=restarted"

Become root by Default

When you escalate, Ansible becomes the root user unless you say otherwise. That is the right target for most admin tasks.

Switch User with become_user

Use --become-user to become someone other than root, like deploying files as the postgres or deploy account.

ansible db -b --become-user postgres -m command -a "psql -c '\\l'"

Choose the Method

The --become-method flag picks how to escalate, such as sudo, su or doas, depending on what your hosts allow.

ansible all -b --become-method su -m ping

Prompt for the sudo Password

If sudo asks for a password, add --ask-become-pass, often -K, and Ansible will prompt you once before running.

ansible all -b -K -m package -a "name=vim state=present"

Three Pieces to Remember

Escalation has three knobs: become turns it on, become_user sets who, become_method sets how. Together they cover any host.

Same Keys in Playbooks

These flags map to directives: become: true, become_user and become_method, which you will reuse inside playbooks later.

become: true

Connect, Then Escalate

Ansible first logs in as your SSH user, then escalates on the host. The two users are separate steps, so do not confuse them.

ansible web -u deploy -b -m ping

Escalate Only When Needed

Skip become for read-only tasks like ping or gathering facts. Reserve it for actions that truly require elevated rights.

Quick Check

What is the default escalation method when you use become?

Recap

You can now go root safely: become enables escalation, become_user sets the target, and -K prompts for the password. 🔐

Frequently asked questions

Is the “Privilege Escalation with become” lesson free?

Yes — the full text of “Privilege Escalation with become” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.

What will I learn in “Privilege Escalation with become”?

Run tasks as root using sudo. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ansible Academy?

No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Privilege Escalation with become” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ansible Academy lesson?

Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Anatomy of the ansible Command
  2. Patterns: Targeting all, web & host1
  3. Run Shell Commands with command & shell
  4. Privilege Escalation with become
← Back to Ansible Academy