Ansible 'This command has to be run under the root user' Error: Fix with become — Video Tutorial
Fix the Ansible error 'This command has to be run under the root user'. Enable privilege escalation with become, become_method, and sudoers configuration.
Watch Video
Watch "Ansible 'This command has to be run under the root user' Error: Fix with become" on YouTube
What You'll Learn
- Introduction
- Playbook
- error code
- error execution
- fix code
- fix execution
- Conclusion
- Fix: Enable become
- Task level
- Play level
Full Tutorial Content
Introduction
Today we're going to talk about Ansible troubleshooting, specifically about the "This command has to be run under the root user" message.
This fatal error message happens when we are trying to execute a module that requires more privilege during module execution.
These circumstances are usually related to Ansible Playbook or Ansible configuration.
I'm Luca Berton and welcome to today's episode of Ansible Pilot.
Playbook
The best way of talking about Ansible troubleshooting is to jump in a live Playbook to show you practically the "This command has to be run under the root user" and how to solve it!
This Playbook is going to try to install the "rsync" package on our target system.
error code
```yaml
---
- name: Troubleshooting under the root user
hosts: all
become: false
tasks:
- name: rsync installed
ansible.builtin.package:
name: rsync
state: present
```
error execution
```bash
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory troubleshooting/under_root_user_error.yml
PLAY [Troubleshooting under the root user] ********************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [rsync installed] ****************************************************************************
fatal: [demo.example.com]: FAILED! => {"changed": false, "msg": "This command has to be run under the root user.", "results": []}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
ansible-pilot $
```
fix code
```yaml
---
- name: Troubleshooting under the root user
hosts: all
become: true
tasks:
- name: rsync installed
ansible.builtin.package:
name: rsync
state: present
```
fix execution
```bash
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory troubleshooting/under_root_user_fix.yml
PLAY [Troubleshooting under the root user] ********************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [rsync installed] ****************************************************************************
ok: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
```
[code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/tree/master/troubleshooting)
Conclusion
Now you know better how to troubleshoot the Ansible "This command has to be run under the root user." message.
Fix: Enable become
Task level
```yaml
- name: Install package
ansible.builtin.apt:
name: nginx
state
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 3 min
- Category: installation
Read the full written article: Ansible 'This command has to be run under the root user' Error: Fix with become
Topics Covered
Related Video Tutorials
- Ansible troubleshooting - Destination does not exist rc 257 — Troubleshoot the "Destination does not exist" error (return code 257) in Ansible with Luca Berton on Ansible Pilot! Learn to fix file path issues effectively.
- Ansible 'Failed to Connect via SSH localhost:22': Fix Guide — Fix Ansible 'failed to connect to the host via ssh localhost port 22' error. Resolve SSH config, connection type, host key, and authentication issues.
- Ansible Permission Denied (Errno 13): Fix File Access Errors — Fix Ansible Permission denied [Errno 13] errors. Resolve file permission, become/sudo, SELinux, and directory access issues with troubleshooting steps.
- Ansible Syntax Error: Debug & Fix YAML Playbook Errors (Guide) — Fix Ansible syntax errors in playbooks. Debug YAML indentation, quoting, colons, and common YAML gotchas with step-by-step troubleshooting examples.
- Ansible 'chgrp failed' Error: Fix Group Ownership Permission Issues — Fix Ansible chgrp failed error when setting file group ownership. Resolve permission denied, missing groups, and mounted filesystem issues with troubleshooting steps.
- Ansible 'Destination Does Not Exist' Error: Fix Path Issues — Fix Ansible destination does not exist error. Resolve missing parent directories, wrong paths, and permission issues for copy, template, and file modules.