Running simple ad hoc commands via script module not working

I have a playbook and a script file which contains a couple of simple ad hoc commands, but it is not working.

I have 2 VMs on my desktop which are linked up via custom host-only 10.10.x.x network. Connection is working, other playbooks work fine etc.

Playbook -


  • hosts: 10.10.0.4

become: yes

tasks:

  • name: Run a script

script: /etc/ansible/playbooks/script.sh

Script.sh

[ansible@localhost playbooks]$ vi script.sh
#!/bin/bash

ansible 10.10.0.4 -m file -a “path=/home/ansible/newfile2.txt state=touch”

Output -

TASK [Run a script] ****************************************************************************************************

fatal: [10.10.0.4]: FAILED! => {“changed”: true, “failed”: true, “rc”: 127, “stderr”: “Shared connection to 10.10.0.4 closed.\r\n”, “stdout”: “/home/ansible/.ansible/tmp/ansible-tmp-1504887533.6-234169887879143/script.sh: line 3: ansible: command not found\r\n”, “stdout_lines”: [“/home/ansible/.ansible/tmp/ansible-tmp-1504887533.6-234169887879143/script.sh: line 3: ansible: command not found”]}

Any idea? Am i missing something? Thanks

Don’t really understand what you’re going to do… but the script works, it justs does’nt find ansible installed on your remote server Perhaps you should explain what you want to do before mixing playbook and had hoc command Regards, JYL

No specific requirement. I just wanted to bundle up some ad hoc commands and run them via script module.

You are correct. I just recently setup that 2nd vm and didnt have ansible installed. Fixed that. Now the command runs but doesnt actually create the file, even though the output reports changed…

Any idea why that would be? Doesnt seem to do anything when pointed to tmp either

[ansible@localhost playbooks]$ ansible-playbook scriptcall.yml

PLAY [10.10.0.4] *******************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [10.10.0.4]

TASK [Run a script] ****************************************************************************************************
changed: [10.10.0.4]

PLAY RECAP *************************************************************************************************************
10.10.0.4 : ok=2 changed=1 unreachable=0 failed=0

Go to the host and run the script to see what’s really happening… :slight_smile:
I tryed from my box the following:

ansible 10.10.0.4 -m command -a ‘echo Hello’
And the result, with Ansible version 2.3.2.0 was:
[WARNING]: No hosts matched, nothing to do
Just after that, I issue:
echo $?
And guess what? The result is: 0
I.e. there’s no reel error on your call, but the module is not ran (i.e. the file is not created) because the host isn’t in the inventory :wink:
Again what are you trying to do and why are you mixing things so ?

The issue here was ssh keys werent setup on the server I was trying to run the script on (the agent). ssh key exchange was setup between the agent and the master, but because the agent itself didnt have ssh keys setup and copied to itself the part of the script running the ad hoc command didnt succeed. When i did that it ran properly…

Also I had to add an inventory file on the agent so that it would recognize itself when the ad hoc command was run

Thanks for your responses