# Conditionally execute command

**URL:** <https://forum.ansible.com/t/conditionally-execute-command/15960>\
**Category:** Ansible Project\
**Created:** [August 15, 2014, 1:57pm UTC](https://forum.ansible.com/t/conditionally-execute-command/15960 "2014-08-15T13:57:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tipytop](https://avatars.discourse-cdn.com/v4/letter/t/a6a055/32.png) [@tipytop](https://forum.ansible.com/u/tipytop)\
**Post date:** [August 15, 2014, 1:57pm UTC](https://forum.ansible.com/t/conditionally-execute-command/15960/1 "2014-08-15T13:57:26Z")

</div>

Hi,

i have a following problem. I am building OpenStack which uses tool _openstack-config_ for setting value in configuration files.

To get value:

openstack-config --get /etc/keystone/keystone.conf sql connection

To set value:

openstack-config --set /etc/keystone/keystone.conf sql connection {{ VALUE }}

So now I want to check if previous value matches the value I provide. If they do match, than it should report OK, otherwise CHANGED. Current solution is to register shell result of _GET_ command and conditionally execute _SET_ command, but I don’t like that very much because I have to do that 50-100 times; therefore code would be quite unreadable.

Does ansible provide easy mechanism to merge these two tasks or is copying ansible shell module and extending it the only solution?

PS: sorry for bad title

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 15, 2014, 7:45pm UTC](https://forum.ansible.com/t/conditionally-execute-command/15960/2 "2014-08-15T19:45:55Z")

</div>

I would use the shell module to register to check the first value.

Then key off the saved\_value.stdout or saved\_value.rc on the conditional of the second.

It may make sense to have an openstack\_config module, but I suspect many folks _might_ just template the config files directly. Depends where they are stored.
