# Conditionals in sudo

**URL:** <https://forum.ansible.com/t/conditionals-in-sudo/17235>\
**Category:** Ansible Developer\
**Created:** [January 10, 2015, 7:49pm UTC](https://forum.ansible.com/t/conditionals-in-sudo/17235 "2015-01-10T19:49:31Z")\
**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 10, 2015, 7:49pm UTC](https://forum.ansible.com/t/conditionals-in-sudo/17235/1 "2015-01-10T19:49:31Z")

</div>

Hi,  
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 ?

Thanks  
Raja

---

<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 10, 2015, 8:20pm UTC](https://forum.ansible.com/t/conditionals-in-sudo/17235/2 "2015-01-10T20:20:58Z")

</div>

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:** ![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:25am UTC](https://forum.ansible.com/t/conditionals-in-sudo/17235/3 "2015-01-11T03:25:21Z")

</div>

Sorry, should have posted in Ansible General mailing list. Apologize for the spam. Ill post there.
