T
T
thematt2016-03-16 19:51:18
Ansible
thematt, 2016-03-16 19:51:18

How to tell Ansible to get permissions via sudo?

Hello. I understand with Ansible, I decided to start by creating a service user, which he will use to go to the servers. Because doing it manually is tedious and illogical, wrote the playbook:

---
- hosts: myservers                                                                                                                                                 
  remote_user: myuser
  become: yes 
  become_user: myuser
  become_method: sudo
  tasks:
  - name: create ansible user
    user: 
      name: ansible 
      shell: /bin/bash 
      home: /home/ansible 

  - name: copy ssh pubkey to home directory
    authorized_key: 
      user: ansible 
      key: "{{ lookup('file', './ansible_pubkey') }}"
  
  - name: adding ansible user to sudoers
    lineinfile: 
      dest: /etc/sudoers 
      line: "ansible ALL=(ALL)       NOPASSWD: ALL" 
      validate: 'visudo -cf %s'

The idea is to run the execution as a private user with password access. All servers have my admin account myuser, which has sudo privileges.
I start the execution of the playbook'a like this:
ansible-playbook -i hosts add-ansible-user.yml -k
To which I get the answer:
TASK [create ansible user] *****************************************************
fatal: [myserver]: FAILED! => {"changed": false, "cmd": "/sbin/useradd -d /home/ansible -s /bin/bash -m ansible", "failed": true, "msg": "[Errno 13] Отказано в доступе", "rc": 13}

Those. Ansible gets to the server via ssh, but cannot execute the task due to insufficient rights. Locally sudo works correctly. Ansible version 2.0.1.0.
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
thematt, 2016-03-16
@thematt

Solved, helped on another resource. From off documentation:

become_user set to user with desired privileges, the user you 'become', NOT the user you login as. Does NOT imply become: yes, to allow it to be set at host level.
become_user specifies the user whose rights you want to get through sudo, respectively, you had to specify root'a, not myuser.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question