Running simple ad hoc commands via script module not working

747 views
Skip to first unread message

Anfield

unread,
Sep 8, 2017, 12:22:35 PM9/8/17
to Ansible Project
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 
 

Jean-Yves LENHOF

unread,
Sep 8, 2017, 12:29:23 PM9/8/17
to ansible...@googlegroups.com
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

Anfield

unread,
Sep 8, 2017, 12:41:12 PM9/8/17
to Ansible Project
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




Gildas Cotomale

unread,
Sep 11, 2017, 10:04:02 AM9/11/17
to Ansible Project
Go to the host and run the script to see what's really happening... :)
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 ;-)
Again what are you trying to do and why are you mixing things so ?

Gareth Hasson

unread,
Sep 11, 2017, 11:13:21 AM9/11/17
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/wYPngKNucPc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/3e3a2cb5-3699-407f-9552-a3e3ad4c0dfa%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages