# Ansible become root and run other commands

**URL:** <https://forum.ansible.com/t/ansible-become-root-and-run-other-commands/40643>\
**Category:** Get Help\
**Created:** [February 26, 2025, 4:01pm UTC](https://forum.ansible.com/t/ansible-become-root-and-run-other-commands/40643 "2025-02-26T16:01:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alessandro\_Giannini](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alessandro_giannini/32/7737_2.png) [@Alessandro\_Giannini](https://forum.ansible.com/u/Alessandro_Giannini)\
**Post date:** [February 26, 2025, 4:01pm UTC](https://forum.ansible.com/t/ansible-become-root-and-run-other-commands/40643/1 "2025-02-26T16:01:36Z")

</div>

Hi guys, I have a problem with running commands on the remote host with the root account. The manual procedure involves logging in to the server with the user ‘user1’ after which I have to run the command ‘sudo su -’ (without password and without other parameters) to become ‘root’ and execute other commands (e.g. chown userx:userx /path/file). Is there any ‘good soul’ who can share a playbook to achieve this result? Thank you for your support

---

<div class="post-metadata">

**Author:** ![kristianheljas](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kristianheljas/32/7702_2.png) [@kristianheljas](https://forum.ansible.com/u/kristianheljas)\
**Post date:** [February 26, 2025, 4:07pm UTC](https://forum.ansible.com/t/ansible-become-root-and-run-other-commands/40643/2 "2025-02-26T16:07:12Z")

</div>

“Become” is the magic keyword here, here are the docs:  
[https://docs.ansible.com/ansible/latest/playbook\_guide/playbooks\_privilege\_escalation.html](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_privilege_escalation.html)

Within playbooks, you can set `become: true` on play or task level and such tasks will run with `sudo` by default.

For example, there’s [file module](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html) that allows to modify file permissions.

```yaml
- hosts: targethost
  become: true
  tasks:
    - name: Change file ownership, group and permissions
      ansible.builtin.file:
        path: /etc/foo.conf
        owner: foo
        group: foo
        mode: '0644'

```

If theres no suitable module for your needs, you can use [command](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/command_module.html) or [shell](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/shell_module.html) module to execute arbitrary commands on the target.

---

<div class="post-metadata">

**Author:** ![Kevinwincott](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@Kevinwincott](https://forum.ansible.com/u/Kevinwincott)\
**Post date:** [February 26, 2025, 5:56pm UTC](https://forum.ansible.com/t/ansible-become-root-and-run-other-commands/40643/3 "2025-02-26T17:56:33Z")

</div>

couldnt have answered it better myself. you can specify a become user too if you need to
