skip to Main Content

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

3

Answers


  1. Chosen as BEST ANSWER

    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
    

  2. Use shell module instead of yum.

    - name: Install Memcached
    shell: sudo yum install -y {{ your_package_here }}

    Not as cool as using a module, but it will get the job done.

    Your become_user is ok. If you don’t use it, you’ll end up trying to run the commands in the playbook, by using the user used to stablish the ssh connection (ansible_user or remote_user or the user used to execute the playbook).

    Login or Signup to reply.
  3. You can specify become: yes a few places. Often it is used at the task level, sometimes it is used as command line parameter (–become, -b run operations with become). It can be also set at the play level:

    - hosts: servers
      become: yes
      become_method: enable
      tasks:
        - name: Hello
      ...
    

    You can also enable it in group_vars:

    group_vars/exmaple.yml

    ansible_become: yes
    

    For your example, using it for installing software I would set it at the task level. I think in your case the import is the problem. You should set it in the file you are importing.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search