This is a text-only version of the following page on https://raymii.org: --- Title : Ansible - Sudo sometimes Author : Remy van Elst Date : 21-12-2013 URL : https://raymii.org/s/tutorials/Ansible_-_Better_sudo_control.html Format : Markdown/HTML --- This Ansible tutorial shows you how run some actions via sudo and some not. It also shows you how to run an entire role via sudo or not.

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 $200 credit for 60 days. Spend $25 after your credit expires and I'll get $25!

Ansible has the option to run playbooks via sudo. You can setup passwordless sudo, but also execute a playbook with the extra `--ask-sudo-pass` / `-K` option so that Ansible asks you for the sudo password. However, you can also have very specific control over how and when sudo is used in a playbook. I have a playbook with a few roles which I use to bootstrap a new debian server. It installs software, sets up ssh, sets up sudo and places a few config files. It is organized in roles, the main playbook looks like this: --- - hosts: new-servers user: username connection: ssh # or paramiko roles: - { role: basic-debian-setup, sudo: yes } - { role: git-setup } - { role: vim } - { role: bash } - { role: screen } - { role: openssh, sudo: yes } - { role: sudo, sudo: yes } - { role: postfix, sudo: yes } - { role: vnstat, sudo: yes } As you can see, I have a few playbooks run with sudo on, and a few with sudo off. The `git-setup`, `vim`, `screen` and `bash` playbooks all do basically the same, they install software and place a configuration file. However, if the entire playbook is run as root, the configuration files placed would be owned by root. If the playbook is not run via sudo, the software cannot be installed. Note that in the first case Ansible also supports setting file permissions on files. This however is not the case when configuration files are cloned from a git repository. The git module does not support setting permissions, and I don't like recursive `chmod`'s. Here is the `vim` playbook: - name: install packages vim and git apt: pkg: {{ item }} state: present update_cache:"yes with_items: - vim-tiny - git sudo: yes - name: clone git repository git: repo: https://github.com/RaymiiOrg/df.git dest: /home/{{user}}/conf version: master sudo: no - name: create symmlink for vim config file: path: /home/{{ user }}/.vimrc src: /home/{{ user }}/conf/vimrc state: link owner: {{ user }} sudo: no This playbook makes sure both `vim` and `git` are installed. It uses sudo for that action. It then clones the git repository with my personal dotfiles, without using sudo. If this action would use sudo, the git repository in my home folder would me owned by root and I could not update it later on without using sudo. The last action symlinks the `.vimrc` file from the repo to the correct location. If that would be done with sudo I could not remove the file without root access. If you define a role with sudo, like in the above example the `postfix` role, then you can use the `sudo: no` option in that playbook to make sure one or more actions are not executed with sudo. [Ansible documentation regarding sudo][2] [1]: https://www.digitalocean.com/?refcode=7435ae6b8212 [2]: http://www.ansibleworks.com/docs/playbooks.html --- 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.