Run a unix script on ansible server that connect to remote servers.

22 views
Skip to first unread message

R-JRI

unread,
Oct 22, 2019, 4:32:21 PM10/22/19
to Ansible Project
Hi, all

I have an existing unix shell script that does ssh to connect to remote servers to search FAILED key word through logs on the remote server. If the logs contain FAILED key word, sftp to the servers to bring the logs to a central server and analyzes them.
Now we want to launch the unix script from Ansible or in the future from Ansible Tower.
Ansible shell, script and command modules are to copy or run UNIX script on remote servers not on Ansible server. In my case, I don't want to copy the script to run on remote servers. I want to run the script on Ansible server. Is there a way to accomplish this in Ansible? Thanks.

Kai Stian Olstad

unread,
Oct 22, 2019, 4:39:27 PM10/22/19
to ansible...@googlegroups.com
On 22.10.2019 22:32, R-JRI wrote:
> Ansible shell, script and command modules are to copy or run UNIX script on
> remote servers not on Ansible server. In my case, I don't want to copy the
> script to run on remote servers. I want to run the script on Ansible
> server. Is there a way to accomplish this in Ansible? Thanks.

command module does that.
The "hosts:" keyword in a play says which hosts the play is run on, localhost aka Ansible controller is a valid host.

Alternative you have
delegate_to: localhost


--
Kai Stian Olstad

R-JRI

unread,
Oct 24, 2019, 1:22:08 PM10/24/19
to Ansible Project
Thanks for the reply, Kai

After I changed the host to local host name in hosts field of yml file. I ran it without error but I don't see it get logs from remote servers to local /tmp directory. If I ran UNIX script, it takes logs to local /tmp/ directory from remote server.

Here is my yml file content:

---
- name: grab log
  hosts: ansidev (this is the host name of ansible server)
 
   tasks:
      - name: get log
        command: /usr/local/bin/failed_log_analysys.sh

Kai Stian Olstad

unread,
Oct 24, 2019, 1:54:44 PM10/24/19
to ansible...@googlegroups.com
On 24.10.2019 19:22, R-JRI wrote:
> Thanks for the reply, Kai
>
> After I changed the host to local host name in hosts field of yml file. I
> ran it without error but I don't see it get logs from remote servers to
> local /tmp directory. If I ran UNIX script, it takes logs to local /tmp/
> directory from remote server.
>
> Here is my yml file content:
>
> ---
> - name: grab log
> hosts: ansidev (this is the host name of ansible server)
>
> tasks:
> - name: get log
> command: /usr/local/bin/failed_log_analysys.sh

That is most likely because your script assume some environment variables is set that is not set when using ssh with non-interactive shell.

Since you are using ansidev instead of localhost Ansible most likely is login in to the server with ssh(depending on you inventory).
If using ssh then you are running it under an non-interactive shell that has very few environment variables set.


--
Kai Stian Olstad

R-JRI

unread,
Oct 28, 2019, 11:48:52 AM10/28/19
to Ansible Project
Thanks, Kai

Once I changed to localhost instead host name. Ansible is able to grab logs from remote hosts.

On Tuesday, October 22, 2019 at 4:32:21 PM UTC-4, R-JRI wrote:
Reply all
Reply to author
Forward
0 new messages