DevOps/Ansible/Configure user to be able to use sudo with no password

From Wikiversity
Jump to navigation Jump to search
#!/usr/bin/env ansible-playbook --ask-become-pass

- hosts: YOUR_HOSTNAME
  become: yes
  become_method: sudo
  gather_facts: no
  tasks:
    - lineinfile:
        path: /etc/sudoers
        state: present
        line: 'YOUR_USERNAME ALL=(ALL) NOPASSWD: ALL'
        validate: 'visudo -cf %s'

Example with insertafter

#!/usr/bin/env ansible-playbook --ask-become-pass

- hosts: YOUR_HOSTNAME
  become: yes
  become_method: sudo
  gather_facts: no
  tasks:
    - lineinfile:
        path: /etc/sudoers
        state: present
        insertafter: '^%sudo'
        line: 'YOUR_USERNAME ALL=(ALL) NOPASSWD: ALL'
        validate: 'visudo -cf %s'


See also[edit | edit source]

  • Sudo/passwordless sudo