Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

---
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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading