Search code examples
ansiblesudo

How to force Ansible to use sudo to install packages?


I have a playbook than run roles, and logs in the server with a user that has the sudo privileges. The problem is that, when switching to this user, I still need to use sudo to, say, install packages.

ie:

sudo yum install httpd

However, Ansible seems to ignore that and will try to install packages without sudo, which will result as a fail.

Ansible will run the following:

yum install httpd

This is the role that I use:

tasks:
  - name: Import du role 'memcacheExtension' 
    import_role:
      name: memcacheExtension
    become: yes
    become_method: sudo
    become_user: "{{become_user}}"
    become_flags: '-i'
    tags:
      - never
      - memcached

And this is the tasks that fails in my context:

- name: Install Memcached
  yum:
    name: memcached.x86_64
    state: present

Am I setting the sudo parameter at the wrong place? Or am I doing something wrong?

Thank you in advance


Solution

  • I ended up specifying Ansible to become root for some of the tasks that were failing (my example wasn't the only one failing, and it worked well. The tweak in my environment is that I can't login as root, but I can "become" root once logged in as someone else.

    Here is how my tasks looks like now:

    - name: Install Memcached
      yum:
        name: memcached.x86_64
        state: present
      become_user: root