# Using "shell" with a switch

**URL:** <https://forum.ansible.com/t/using-shell-with-a-switch/29180>\
**Category:** Ansible Project\
**Created:** [December 3, 2018, 4:44pm UTC](https://forum.ansible.com/t/using-shell-with-a-switch/29180 "2018-12-03T16:44:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [December 3, 2018, 4:44pm UTC](https://forum.ansible.com/t/using-shell-with-a-switch/29180/1 "2018-12-03T16:44:08Z")

</div>

Hey, all.

I need to run a script residing on the remote machine with a switch, as in “/scripts/myscript -f” (it must be executed by root). I’m using the shell module, but can find no way to add the switch without the play failing. Help would be appreciated.

Dimitri

---

<div class="post-metadata">

**Author:** ![powcarz](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@powcarz](https://forum.ansible.com/u/powcarz)\
**Post date:** [December 3, 2018, 9:25pm UTC](https://forum.ansible.com/t/using-shell-with-a-switch/29180/2 "2018-12-03T21:25:19Z")

</div>

Hey Dimitri,

Why don't you send us your code and the output you are receiving?

Piotr

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [December 3, 2018, 10:22pm UTC](https://forum.ansible.com/t/using-shell-with-a-switch/29180/3 "2018-12-03T22:22:03Z")

</div>

OK:

> * * *
> 
> - hosts: all  
> gather\_facts: false  
> become: yes
> 
> # become\_user: root
> 
> # become\_method: su
> 
> tasks:
> 
> - shell: “su - root”  
> args:  
> warn: false
> - shell: ‘/scripts/dnscopy.pl -f’  
> register: (result)
> - debug:  
> var: (result)  
> verbosity: 2

\*become\_user and become\_method are commented out in favor of shell: “su - root” because using the former causes a timeout error, But that’s another issue.

Output of play:

TASK [command] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [admin1]: FAILED! =\> {“changed”: true, “cmd”: “/scripts/dnscopy.pl -f”, “delta”: “0:00:01.425695”, “end”: “2018-12-03 17:13:56.113927”, “failed”: true, “rc”: 2, “start”: “2018-12-03 17:13:54.688232”, “stderr”: “Can’t exec "rndc": No such file or directory at /scripts/dnscopy.pl line 296.\nrndc -s dns1 reload failed: No such file or directory at /scripts/dnscopy.pl line 296.”, “stderr\_lines”: [“Can’t exec "rndc": No such file or directory at /scripts/dnscopy.pl line 296.”, “rndc -s dns1 reload failed: No such file or directory at /scripts/dnscopy.pl line 296.”], “stdout”: “”, “stdout\_lines”: }

without the -f switch, the play completes successfully.

---

<div class="post-metadata">

**Author:** ![powcarz](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@powcarz](https://forum.ansible.com/u/powcarz)\
**Post date:** [December 3, 2018, 11:37pm UTC](https://forum.ansible.com/t/using-shell-with-a-switch/29180/4 "2018-12-03T23:37:12Z")

</div>

Dimitri,

&nbsp;&nbsp;&nbsp;&nbsp;- shell: "su - root"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;args:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;warn: false  
&nbsp;&nbsp;&nbsp;&nbsp;- shell: '/scripts/dnscopy.pl -f'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: (result)

You are running 2 separate shells. First shell runs su - root then exits -  
this is not doining anything. Then secon task runs in a new shell, you are  
not leveraging the previous "su - root". You are using become: yes, and  
that should be sufficient to elevate task's rights, so the second task  
should work anyway. Try this:  
Remove  
- shell: "su - root" task  
replace it with:  
- shell: "whoami"  
&nbsp;&nbsp;&nbsp;register: who  
- debug: var=who  
to check under which user is your script executing. If it's not 'root'  
then you messed up, if it is 'root' but your script still fails, you have a  
problem with your script.

Piotr

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [December 4, 2018, 1:34pm UTC](https://forum.ansible.com/t/using-shell-with-a-switch/29180/5 "2018-12-04T13:34:43Z")

</div>

Thank you, Piotr. All good, now. You’re right, and this is a mistake I shouldn’t have made, given my long history with Ansible. But, hey, never too late to learn. Thanks, again.

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [December 4, 2018, 2:38pm UTC](https://forum.ansible.com/t/using-shell-with-a-switch/29180/6 "2018-12-04T14:38:03Z")

</div>

Piotr,

Before I consider this solved, let me push the envelope a bit, and ask about the switch. If I run the play like this:

Shell: /scripts/myscript

it works.

If I run it like this:

Shell: /scripts/myscript -f

it fails with:

fatal: [admin1]: FAILED! =\> {“changed”: true, “cmd”: “/scripts/dnscopy.pl -f”, “delta”: “0:00:02.025735”, “end”: “2018-12-04 09:34:11.123312”, “failed”: true, “rc”: 2, “start”: “2018-12-04 09:34:09.097577”, “stderr”: “Can’t exec "rndc": No such file or directory at /scripts/dnscopy.pl line 296.\nrndc -s dns1 reload failed: No such file or directory at /scripts/dnscopy.pl line 296.”, “stderr\_lines”: [“Can’t exec "rndc": No such file or directory at /scripts/dnscopy.pl line 296.”, “rndc -s dns1 reload failed: No such file or directory at /scripts/dnscopy.pl line 296.”], “stdout”: “”, “stdout\_lines”: }

Your continued help (or anybody else’s) would be appreciated.

Dimitri

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 4, 2018, 2:56pm UTC](https://forum.ansible.com/t/using-shell-with-a-switch/29180/7 "2018-12-04T14:56:11Z")

</div>

You script does something else with -f than without.  
As the error message say it can't find rndc, so I guess it's a missing path issue.

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [December 4, 2018, 3:10pm UTC](https://forum.ansible.com/t/using-shell-with-a-switch/29180/8 "2018-12-04T15:10:48Z")

</div>

Thanks, Kai.

Now, I’m completely turned around.

In order for /scripts/dnscopy.pl -f to work in the remote host itself, I must su - root first. I thought that become: yes took care of that. Here’s the playbook as it stands (and fails) now:

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 4, 2018, 3:24pm UTC](https://forum.ansible.com/t/using-shell-with-a-switch/29180/9 "2018-12-04T15:24:53Z")

</div>

Become uses sudo by default unless you set become\_method: su

Default I think it only will do "su root -c /scripts/dnscopy.pl -f" so if you need the - aka --login option you might need become\_flags: '-' as well

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [December 4, 2018, 3:35pm UTC](https://forum.ansible.com/t/using-shell-with-a-switch/29180/10 "2018-12-04T15:35:09Z")

</div>

Sorry, Kai, I’m not understanding what the playbook should look like. I’m a bit slow on the uptake.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 4, 2018, 3:37pm UTC](https://forum.ansible.com/t/using-shell-with-a-switch/29180/11 "2018-12-04T15:37:45Z")

</div>

- hosts: all  
&nbsp;&nbsp;gather\_facts: false  
&nbsp;&nbsp;become: yes  
&nbsp;&nbsp;become\_method: su  
&nbsp;&nbsp;become\_flags: '-'

&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- shell: /scripts/dnscopy.pl -f  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: script\_output  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: script\_output.stdout\_lines

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [December 4, 2018, 4:10pm UTC](https://forum.ansible.com/t/using-shell-with-a-switch/29180/12 "2018-12-04T16:10:37Z")

</div>

I thought that’s what I should do. I made those changes, but then the playbook is stuck/runs without completion (until it ultimately times out, I’m guessing).

---

<div class="post-metadata">

**Author:** ![powcarz](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@powcarz](https://forum.ansible.com/u/powcarz)\
**Post date:** [December 4, 2018, 5:51pm UTC](https://forum.ansible.com/t/using-shell-with-a-switch/29180/13 "2018-12-04T17:51:54Z")

</div>

Piotr

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [December 4, 2018, 6:49pm UTC](https://forum.ansible.com/t/using-shell-with-a-switch/29180/14 "2018-12-04T18:49:53Z")

</div>

Piotr,

After putting in the directives you mentioned, if I leave become\_method: su and become\_flags: ‘-’ in the playbook, then the playbook is stuck/runs without completion. If I take those out, leaving only become: yes, then I get the following output:

ok: [admin1] =\> {  
“who”: {  
“changed”: true,  
“cmd”: “whoami”,  
“delta”: “0:00:00.006895”,  
“end”: “2018-12-04 13:32:06.805245”,  
“rc”: 0,  
“start”: “2018-12-04 13:32:06.798350”,  
“stderr”: “”,  
“stderr\_lines”: ,  
“stdout”: “root”,  
“stdout\_lines”: [  
“root”  
]  
}  
}

If I run the script on the remote host **without** su - root, I get:

Can’t exec “rndc”: No such file or directory at ./dnscopy.pl line 296.  
rndc -s dns1 reload failed: No such file or directory at ./dnscopy.pl line 296.

If I run the script on the remote host with su - root, I get:

server reload successful

---

<div class="post-metadata">

**Author:** ![powcarz](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@powcarz](https://forum.ansible.com/u/powcarz)\
**Post date:** [December 4, 2018, 7:27pm UTC](https://forum.ansible.com/t/using-shell-with-a-switch/29180/15 "2018-12-04T19:27:53Z")

</div>

Piotr

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [December 4, 2018, 8:07pm UTC](https://forum.ansible.com/t/using-shell-with-a-switch/29180/16 "2018-12-04T20:07:10Z")

</div>

Piotr,

The thing of it is, the script (which I didn’t write) runs the rndc (BIND Remote Name Daemon Control - we use it to reload zone files) command on our dns servers (which it locates), not on the host in which the script is located. Thus, the failure. Here’s a snippet from the script:

> # rndc -s dns1 reload
> 
> @args = (“rndc”, “-s”, “dns1”, “reload”);  
> system(@args) == 0  
> or die “rndc -s dns1 reload failed: $!”;

Changes to the script are not an option.

That said, is what I’m trying to do not doable?

Dimitri

---

<div class="post-metadata">

**Author:** ![powcarz](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@powcarz](https://forum.ansible.com/u/powcarz)\
**Post date:** [December 4, 2018, 8:30pm UTC](https://forum.ansible.com/t/using-shell-with-a-switch/29180/17 "2018-12-04T20:30:46Z")

</div>

Dimitri  
Your script is depending on rndc residing in the host's $PATH. It might  
happen, that the root's profile is being set differently when you become  
root in Ansible playbook, than the situation when are switching to root in  
a regular way in SSH or terminal. This is why I asked you for more  
information in my previous post.  
A possible workaround would be to forcibly set PATH env variable when  
calling dnscopy.pl. but I can't give you a ready recipe because I don't  
have enough informaton about your system

Piotr

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [December 4, 2018, 8:48pm UTC](https://forum.ansible.com/t/using-shell-with-a-switch/29180/18 "2018-12-04T20:48:17Z")

</div>

Piotr,

Fair enough. And, I do appreciate you sticking with this.

Output of “echo $PATH”:

/usr/kerberos/sbin:/usr/kerberos/bin:/usr/bin:/bin

Output of “which rndc”:

/usr/bin/which: no rndc in (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/bin:/bin) ← not a surprise; BIND isn’t installed on this machine

Output after adding - shell: “‘echo $PATH’”, etc. to playbook:

ok: [admin1] =\> {  
“path”: {  
“changed”: true,  
“cmd”: “echo $PATH”,  
“delta”: “0:00:00.006128”,  
“end”: “2018-12-04 14:36:50.766764”,  
“rc”: 0,  
“start”: “2018-12-04 14:36:50.760636”,  
“stderr”: “”,  
“stderr\_lines”: ,  
“stdout”: “/usr/bin:/bin”,  
“stdout\_lines”: [  
“/usr/bin:/bin”  
]  
}  
}

Remote machine OS os CentOS 5.11 ← yes, I know

---

<div class="post-metadata">

**Author:** ![powcarz](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@powcarz](https://forum.ansible.com/u/powcarz)\
**Post date:** [December 4, 2018, 9:21pm UTC](https://forum.ansible.com/t/using-shell-with-a-switch/29180/19 "2018-12-04T21:21:19Z")

</div>

> /usr/bin/which: no rndc in (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/bin:/bin) ← not a surprise; BIND isn’t installed on this machine

So, do you already know what the problem is? Are you executing rndc on remote host but you dont have BIND installed on it?

---

<div class="post-metadata">

**Author:** ![sateeshazure](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@sateeshazure](https://forum.ansible.com/u/sateeshazure)\
**Post date:** [December 4, 2018, 9:24pm UTC](https://forum.ansible.com/t/using-shell-with-a-switch/29180/20 "2018-12-04T21:24:21Z")

</div>

Hi,

I am trying to setup communication between ansible and a windows server which is hosted in virtual machine manager. Do any one know about it.

I am getting error like  
Bad HTTP response from server . Code 404

Thanks in advance

[Next page](https://forum.ansible.com/t/using-shell-with-a-switch/29180.md?page=2)
