Search code examples
ansiblesudoansible-inventory

Simplest task to confirm sudo access with Ansible


I am trying to validate I have correctly set up my inventories/hosts.yml to remotely provision a BeagleBone Black.

What is a very simple playbook task one can create to confirm one can run a command with sudo (thinking something like sudo apt update).


I have ansible [core 2.11.12] with python version = 3.7.14, and here is my current inventories/hosts.yml:

---
all:
  hosts:
    testbeagle:
      ansible_host: 10.4.8.120
  vars:
    ansible_user: debian
    ansible_password: temppwd
    ansible_python_interpreter: /usr/bin/python3
    ansible_become_pass: temppwd

Solution

  • You don't generally use sudo directly when working with Ansible. You configure privilege escalation using the become, become_method, and become_user variables. Out of the box, become is false, become_method is sudo and become_user is root. You can test things like this:

    - hosts: all
      tasks:
        - become: true
          command: id -u
          register: id_output
    
        - assert:
            that: id_output.stdout == '0'
    

    If that playbook runs, it means that Ansible was successfully able to obtain root privileges for the task on which we set become: true.

    Read more about privilege escalation in the documentation.