I can't get Ansible to recognize sudo

I’m starting with Ansible but I’m having trouble using sudo. The configuration files look like this:
ansible.cfg

[defaults]
ask_sudo_pass=True
editor=/usr/bin/micro
force-color=True
home=.
inventory=inventory.yml
nocolor=False
remote_port=2222
remote_user=eniocsj

[privilege_escalation]
become_ask_pass=True
become_exe=/usr/bin/sudo
become_user=eniocsj
become_method=sudo

playbook.yml

---
- name: Criação de um ambiente de desenvolvimento em background
  hosts: lab
  gather_facts: no
  become: yes
  remote_user: eniocsj
  become_method: sudo
  tasks:
    - name: Atualizando o cache do gerenciador de pacotes
      become: yes
      remote_user: eniocsj
      become_method: sudo
      ansible.builtin.apt:
        update_cache: yes

When calling the playbook, the following error is given:, When calling the playbook, the following error appears:

fatal: [182.168.1.2]: FAILED! => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3.10"
    },
    "changed": false,
    "invocation": {
        "module_args": {
            "allow_change_held_packages": false,
            "allow_downgrade": false,
            "allow_unauthenticated": false,
            "autoclean": false,
            "autoremove": false,
            "cache_valid_time": 0,
            "clean": false,
            "deb": null,
            "default_release": null,
            "dpkg_options": "force-confdef,force-confold",
            "fail_on_autoremove": false,
            "force": false,
            "force_apt_get": false,
            "install_recommends": null,
            "lock_timeout": 60,
            "only_upgrade": false,
            "package": null,
            "policy_rc_d": null,
            "purge": false,
            "state": "present",
            "update_cache": true,
            "update_cache_retries": 5,
            "update_cache_retry_max_delay": 12,
            "upgrade": null
        }
    },
    "msg": "Failed to lock apt for exclusive operation: Failed to lock directory /var/lib/apt/lists/: E:Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)"
}

It’s probably not calling sudo., Probably not calling sudo. Where am I going wrong?

You need to remove that configuration. That config tells ansible which user you want to become. It’s default value is root. If this is set to the same user as the remote_user then become is effectively ignored, since you are already the user that you want to become.

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.