<h5 dir="auto">ISSUE TYPE</h5>
<p dir="auto">Feature Idea</p>
<h5 dir="auto">COMPONENT NAME</h5>
<p dir="auto">shell module</p>
<h5 dir="auto">ANSIBLE VERSION</h5>
<p dir="auto">2.3</p>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">From <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/lordbaco/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/lordbaco">@lordbaco</a> on 2015-05-22T09:32:33Z<br>
Countless of times, I need to create two blocks of command or shell actions.<br>
One to check the status of the system (and register it to an ansible variable) and<br>
One to execute the command if really needed.</p>
<p dir="auto">My idea is to add a check= option to both command and shell core modules<br>
so you can combine both actions in one. This will make playbook/task shorter.</p>
<p dir="auto">In bash, I like to do things like<br>
test -f /etc/libvirt/qemu/autostart/test.xml || virsh autostart test<br>
rsync --dry-run -av a/ b/ &amp;&amp; rsync -av a/ b/<br>
patch --dry-run -p1 -R &lt; test.patch &amp;&amp; patch -p1 -R &lt; test.patch<br>
grep -q 'test3' /root/file || perl -p -i -e 's/test2/test3/g' /root/file<br>
grep -q 'test4' /root/file || perl -p -i -e '$_ = undef if(/test4/)' /root/file</p>
<p dir="auto">To come back to Ansible this will translate to<br>
command: rsync -av a/ b/ check="rsync --dry-run -av a/ b/"<br>
when: check.rc == 0<br>
command: virsh autostart test check="test -f /etc/libvirt/qemu/autostart/test.xml"<br>
when: check.rc == 0<br>
shell: patch -p1 -R &lt; test.patch check="patch --dry-run -p1 -R &lt; test.patch"<br>
when: check.rc == 0<br>
shell: perl -p -i -e 's/test2/test3/g' /root/file check="grep -q 'test3' /root/file"<br>
when: check.rc != 0<br>
shell: perl -p -i -e '$_ = undef if(/test4/)' /root/file check="grep -q 'test4' /root/file"<br>
when: check.rc != 0<br>
...</p>
<p dir="auto">In certain cases, you need to do execute the command when the check= based<br>
on more complex condition(s) so the idea is to use it in conjuction with<br>
"when:" and evaluate check.rc, check.stdout, check.stderr to anything you want</p>
<p dir="auto">-name: check if ssh host keys need to be regenerated<br>
shell: rm -f /etc/ssh/ssh_host_* verify="cat /etc/ssh/ssh_host_rsa_key.pub"<br>
when: check.stdout.find('root@template-') != -1<br>
notify:<br>
reconfigure ssh<br>
tags: ssh</p>
<p dir="auto">Copied from original issue: <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="79376621" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible-modules-core/issues/1396" data-hovercard-type="issue" data-hovercard-url="/ansible/ansible-modules-core/issues/1396/hovercard" href="https://github.com/ansible/ansible-modules-core/issues/1396">ansible/ansible-modules-core#1396</a></p>
<h5 dir="auto">ISSUE TYPE</h5>
<p dir="auto">Feature Idea</p>
<h5 dir="auto">COMPONENT NAME</h5>
<p dir="auto">core</p>
<h5 dir="auto">ANSIBLE VERSION</h5>
<p dir="auto">2.2</p>
<h5 dir="auto">CONFIGURATION</h5>
<h5 dir="auto">OS / ENVIRONMENT</h5>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">Currently modules that do not support check mode (e.g. the <code class="notranslate">command</code> module) report that the "remote module does not support check mode", however if they use <code class="notranslate">creates=</code> or <code class="notranslate">removes=</code> Ansible could perform a check-mode, or at least evaluate whether it would not run.</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<h5 dir="auto">EXPECTED RESULTS</h5>
<h5 dir="auto">ACTUAL RESULTS</h5>