This is a text-only version of the following page on https://raymii.org:
---
Title : Ansible - sudoers safety and sanity checking in playbook
Author : Remy van Elst
Date : 23-03-2013
URL : https://raymii.org/s/tutorials/Ansible_-_Sudo_Safety_and_Sanity_Checks.html
Format : Markdown/HTML
---
Using Ansible to manage the /etc/sudoers file is fine, except when you have a
syntax error in your template. This method helps you to only deploy a correct
sudoers file.
Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:
I'm developing an open source monitoring app called Leaf Node Monitoring, for windows, linux & android. Go check it out!
Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.
You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days.
I manage the `sudo` config (`/etc/sudoers/`) via Ansible. My sudo playbook
creates an admin group, adds me to that admin group, and sets some variables in
`/etc/sudoers/`. I do not have a sudoers template file, because I created the
playbook at a client which has various different sudoers files, which they do
not want to have changed ,because of different nagios checks that needed sudo on
different hosts. However, if you start of clean, then a template file for
`/etc/sudoers` is the best choice.
This is the playbook:
---
- hosts: all
sudo: True
user: remy
connection: ssh # or paramiko
vars:
distro: {{ ansible_distribution }}
pkg_mgr: {{ ansible_pkg_mgr }}
pbname: {{ inventory_hostname }}
tasks:
- name: Copy sudoers file for safety
command: cp -f /etc/sudoers /etc/sudoers.tmp
- name: Create sudoers file backup
command: cp -f /etc/sudoers /etc/sudoers.bak
- name: Create admins group
group: name=admins system=yes state=present
- name: make sure we can sudo as admin group
lineinfile: dest=/etc/sudoers.tmp state=present regexp='^%admin' line='%admin ALL=(ALL) ALL'
- name: also make sure ssh-agent works via sudo
lineinfile: dest=/etc/sudoers.tmp state=present regexp='^Defaults env_keep\+\=SSH_AUTH_SOCK' line='Defaults env_keep+=SSH_AUTH_SOCK'
- name: Final sudoers file check
shell: visudo -q -c -f /etc/sudoers.tmp && cp -f /etc/sudoers.tmp /etc/sudoers
* We create the `admins` group, to which all users that need sudo are added by other playbooks.
* We copy the remote sudoers file to a temp one and perform all actions on the temp sudoers file. We also back up the sudoers file.
* We enable the `admins` group to sudo
* We make sure `ssh-agent` works via sudo. This was used for a git repository on the root user account, to show our own names in the commits.
* Finally we use `visudo` to check if the file is correct, and if so we copy the file over the "original" sudos file.
By using the temp file we make sure we don't have any syntax errors and lock
ourselves out of machines, needing to use ILO/DRAC to reset passwords and such.
Been there, done that, not funny at all.
[1]: https://www.digitalocean.com/?refcode=7435ae6b8212
---
License:
All the text on this website is free as in freedom unless stated otherwise.
This means you can use it in any way you want, you can copy it, change it
the way you like and republish it, as long as you release the (modified)
content under the same license to give others the same freedoms you've got
and place my name and a link to this site with the article as source.
This site uses Google Analytics for statistics and Google Adwords for
advertisements. You are tracked and Google knows everything about you.
Use an adblocker like ublock-origin if you don't want it.
All the code on this website is licensed under the GNU GPL v3 license
unless already licensed under a license which does not allows this form
of licensing or if another license is stated on that page / in that software:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
Just to be clear, the information on this website is for meant for educational
purposes and you use it at your own risk. I do not take responsibility if you
screw something up. Use common sense, do not 'rm -rf /' as root for example.
If you have any questions then do not hesitate to contact me.
See https://raymii.org/s/static/About.html for details.