How to configure sudo for Ansible?

QuestionsQuestions8 SkillsProAnsible ConfigurationOct, 06 2025
0226

To configure sudo for Ansible, you need to ensure that the user running Ansible has the necessary permissions to execute commands with sudo without a password prompt. Here’s how to do it:

  1. Edit the sudoers file:
    Use the visudo command to safely edit the sudoers file:

    sudo visudo
  2. Add the user:
    Add a line for the user that will run Ansible, allowing them to execute commands without a password. Replace username with the actual username:

    username ALL=(ALL) NOPASSWD: ALL

    This line allows username to run any command as any user without being prompted for a password.

  3. Use the become directive in your playbook:
    In your Ansible playbook, specify that you want to use sudo by adding the become directive:

    - hosts: all
      tasks:
        - name: Install a package
          apt:
            name: package_name
            state: present
          become: yes
  4. Run your playbook:
    When you run your playbook, Ansible will use sudo to execute tasks that require elevated privileges.

If you have any further questions or need clarification, feel free to ask!

0 Comments

no data
Be the first to share your comment!