# Conditionals in sudo

**URL:** <https://forum.ansible.com/t/conditionals-in-sudo/17236>\
**Category:** Ansible Project\
**Created:** [January 11, 2015, 3:33am UTC](https://forum.ansible.com/t/conditionals-in-sudo/17236 "2015-01-11T03:33:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raja1](https://avatars.discourse-cdn.com/v4/letter/r/85f322/32.png) [@Raja1](https://forum.ansible.com/u/Raja1)\
**Post date:** [January 11, 2015, 3:33am UTC](https://forum.ansible.com/t/conditionals-in-sudo/17236/1 "2015-01-11T03:33:25Z")

</div>

Hi,  
(Originally posted to Ansible Dev by mistake)

Im a newbie trying to automate a few commands across boxes using Ansible.

I would like the sudo command on certain tasks to be conditional, so depending on the node, I’d like to turn on sudo or not. For e.g. I have a development machine (my laptop) and a stage server where I need certain sets of commands to be run. On the dev machine, I dont need any of the commands to be run as sudo but need them to run as sudo on the stage server.

I thought something like

- hosts: all  
tasks:
- name: Execute Command X  
command: Command X  
sudo: inventory\_hostname != ‘localhost’

My Hosts file contains entries like  
localhost  
demo7 ansible\_ssh\_host=… ansible\_ssh\_port=…

would make the task not run as sudo on “localhost” (my dev machine) but would run with sudo on the other nodes. Unfortunately, this doesnt happen and it always treats this as sudo: False.

Is it possible to have conditionals in sudo ?

UPDATE: Looking through the source, this doesn’t seem to be the case. The Expression doesnt seem to be evaluated and sent directly to utils.boolean.  
Is there any other way of achieving this goal other than running every task twice – once with sudo and once without.

Thanks  
Raja

---

<div class="post-metadata">

**Author:** ![Tom\_Bamford](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@Tom\_Bamford](https://forum.ansible.com/u/Tom_Bamford)\
**Post date:** [January 13, 2015, 6:18pm UTC](https://forum.ansible.com/t/conditionals-in-sudo/17236/2 "2015-01-13T18:18:02Z")

</div>

Hi Raja

I struggled with this, and the approach I settled with is to duplicate the task, having one with the ‘sudo:’ parameter and one without, and each having a ‘when:’ conditional.

- command: something  
sudo: yes  
when: env == ‘development’

- command: something  
when: env != ‘development’

Hope this helps

Tom

---

<div class="post-metadata">

**Author:** ![Greg\_Andrews](https://avatars.discourse-cdn.com/v4/letter/g/6bbea6/32.png) [@Greg\_Andrews](https://forum.ansible.com/u/Greg_Andrews)\
**Post date:** [January 14, 2015, 4:29am UTC](https://forum.ansible.com/t/conditionals-in-sudo/17236/3 "2015-01-14T04:29:53Z")

</div>

I wonder if inventory variables might do what you need. Have a look at the Ansible docs page for them [here](http://docs.ansible.com/intro_inventory.html), especially the part of the page starting with “Host Variables”, but also the descriptions of files in host\_vars and group\_vars directories starting with “Splitting Out Host and Group Specific Data”. One thing that’s not mentioned in the page is that the “host\_vars/all” file will set variables for all hosts.

I haven’t checked to see if the “sudo: yes” flag can be controlled in these ways. If it can, then this approach could be better than running tasks twice.

-Greg
