Skip to content

Wojciech Błaszkowski

  • Start
  • CQ-DX
  • Linux
  • O mnie
  • my Linux spell book
  • GitHub
  • RSS
Ansible logo

Ansible – add user to sudo

2019-09-27 by Wojciech Błaszkowski

I’m using Ansible for some time. Great tool! Now for some example; allow user to use sudo.

Many tasks samples look’s like this (tabs/spaces cut by dummy WP editor! sorry!):

- name: Ensuring that wheel is able to use sudo without password
lineinfile:
path: /etc/sudoers
regexp: '^%wheel'
line: '%wheel ALL=(ALL) NOPASSWD: ALL'
validate: 'visudo -cf %s'

If it it’s not, they hack the /etc/sudoers file. Then they add a shell user as a member of wheel group:

- name: Setup Ansible User
user:
name: ansible
comment: Ansible Management User
group: wheel

But we wish that new user had’s only it’s own group. Or they add a shell user to the main /etc/sudoers file:

- name: Add user to sudoers file
lineinfile:
path: /etc/sudoers
regexp: '^ansibleuser'
line: 'ansibleuser ALL=(ALL) NOPASSWD: ALL'
validate: 'visudo -cf %s'

It is true, that the lineinfile module’s validate option will NOT write any changes to the path file if the result of validation command will not be positive. But why to hack the /etc/sudoers every time?

I prefare to create single file for each user, like so:

- name: Add user "ansibleuser" to sudo
lineinfile:
path: /etc/sudoers.d/ansibleuser
line: 'ansibleuser ALL=(ALL) NOPASSWD: ALL'
state: present
mode: 0440
create: yes
validate: 'visudo -cf %s'

That’s all. We can make sure, that the includedir in sudoers is in /etc/sudoers file (this is one time hack if not present):

- name: Set includedir in sudoers
lineinfile:
dest: /etc/sudoers
line: "#includedir /etc/sudoers.d"
state: present
validate: "/usr/sbin/visudo -cf %s"

That is easy to manage; both by hand and via automation tools like Ansible. Feel free to comment! :)

Post navigation

Previous Post:

wycieczki 2.1 – rodzina rozwija skrzydła

Next Post:

Fizyka – Halliday Resnick

9 Commments

  1. fredmj pisze:
    2019-11-28 o 19:17

    Thanks Wojciech !
    Nice example I will inspired from to config my own sudoers groups.

  2. Paweł pisze:
    2019-12-28 o 19:31

    Thanks !

  3. Andu pisze:
    2020-02-05 o 00:31

    Thanks!!!

  4. Patrick pisze:
    2020-07-11 o 05:20

    thank you nice examples!

  5. Perfecto pisze:
    2020-11-29 o 12:56

    Very nice example. It has saved me a lot of time

  6. None pisze:
    2021-06-03 o 12:31

    There is a small mistake in the last task, you mean

    line: „includedir /etc/sudoers.d”

    and not

    line: „#includedir /etc/sudoers.d”

    (remove the '#’ before included)

  7. mimmus pisze:
    2022-02-10 o 16:13

    No, you need # in „#includedir /etc/sudoers.d”

  8. 123 pisze:
    2022-11-22 o 15:31

    whata fuck is %s ??

  9. Pingback: Ansible Create User With Sudo? All Answers - Brandiscrafts.com

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Wojciech Błaszkowski

Wojciech Błaszkowski

Cześć, jestem Wojciech.

Oprócz mojej codzienności nastawionej na bycie przygotowanym (prepper) znajdziesz tu kilka informacji jakie mogą przydać się administratorowi Linux, a także trochę prywatnych informacji o mnie którymi chciałbym się podzielić.

Zapraszam :)

Kto zabezpieczył Twój serwer?

Linux Admin

Co u mnie się dzieje:

  • W moim domu mówi się Walkiewiczem
  • Ferie 2023
  • Gwiazdka 2022
  • Antena W3DZZ
  • Zwalniamy :)

administracja serwerami linux

  • Administrator Linux Administrator Linux

Najnowsze komentarze

  • Ansible Create User With Sudo? All Answers - Brandiscrafts.com - Ansible – add user to sudo
  • Wojciech Błaszkowski - my linux spell book
  • virooz - Dostrajanie routera WiFi Netgear R7000
  • 123 - Ansible – add user to sudo
  • pauluZ - WRT160NL – router, którego używam w domu

Archiwa

  • kwiecień 2024
  • luty 2023
  • grudzień 2022
  • listopad 2020
  • lipiec 2020
  • marzec 2020
  • luty 2020
  • listopad 2019
  • lipiec 2019
  • maj 2019
  • wrzesień 2018
  • maj 2018
  • grudzień 2017
  • sierpień 2017
  • lipiec 2017
  • czerwiec 2017
  • luty 2017
  • grudzień 2016
  • wrzesień 2016
  • sierpień 2016
  • czerwiec 2016
  • marzec 2016
  • luty 2016
  • styczeń 2016
  • grudzień 2015
  • maj 2015
  • luty 2015
  • styczeń 2015
  • sierpień 2014
  • lipiec 2014
  • czerwiec 2014
  • maj 2014
  • styczeń 2014
  • grudzień 2013
  • listopad 2013
  • lipiec 2013
  • czerwiec 2013
  • maj 2013
  • marzec 2013
  • luty 2013
  • styczeń 2013
  • grudzień 2012
  • październik 2012
  • sierpień 2012
  • lipiec 2012
  • czerwiec 2012
  • kwiecień 2012
  • luty 2012
  • styczeń 2012
  • grudzień 2011
  • wrzesień 2011
  • sierpień 2011
  • maj 2011
  • marzec 2011
  • wrzesień 2010
  • maj 2010
  • kwiecień 2010
  • luty 2010
  • październik 2009
  • wrzesień 2009
  • sierpień 2009
  • lipiec 2009
  • luty 2009
  • grudzień 2008
  • październik 2008
  • wrzesień 2008
  • czerwiec 2008
  • kwiecień 2008
  • marzec 2008
  • luty 2008
  • grudzień 2007
  • październik 2007
  • wrzesień 2007
  • sierpień 2007
  • lipiec 2007
  • czerwiec 2007
  • maj 2007
  • kwiecień 2007
  • marzec 2007
  • luty 2007
  • styczeń 2007
  • grudzień 2006
  • listopad 2006
  • październik 2006
  • wrzesień 2006
  • sierpień 2006
  • lipiec 2006
  • czerwiec 2006
  • maj 2006
  • kwiecień 2006
  • marzec 2006
  • luty 2006

Administrator Linux

Jeśli szukasz opieki nad serwerem Linux, zapraszam do zapoznania się z tym czym zajmuję się na codzień. Administracja serwerami Linux
© 2025 Wojciech Błaszkowski | Built using WordPress and SuperbThemes