# Need to execute complex unix commands

**URL:** <https://forum.ansible.com/t/need-to-execute-complex-unix-commands/27222>\
**Category:** Ansible Developer\
**Created:** [April 9, 2018, 6:41am UTC](https://forum.ansible.com/t/need-to-execute-complex-unix-commands/27222 "2018-04-09T06:41:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akash\_Agarwal](https://avatars.discourse-cdn.com/v4/letter/a/b2d939/32.png) [@Akash\_Agarwal](https://forum.ansible.com/u/Akash_Agarwal)\
**Post date:** [April 9, 2018, 6:41am UTC](https://forum.ansible.com/t/need-to-execute-complex-unix-commands/27222/1 "2018-04-09T06:41:02Z")

</div>

Hi All,

I need to execute complex shell commands on remote or local hosts. What is the best way to this?

I tried multiple ways. Either it is not passing Ansible syntax, or shell script syntax

Example:

Suppose I want to execute this command

echo “ps -ef|grep data”|sed ‘s~[\[1\]/]+~~g’|sed “s/$/$(date +”%d\_%m\_%Y\_%H\_%M\_%S")/"

It gives following output on shell :  
psefgrepdata09\_04\_2018\_12\_08\_55

But somehow I am not able to execute. Urgent help please ?

Thanks,  
Akash

* * *

1. :alnum:

---

<div class="post-metadata">

**Author:** ![Suren\_Baskaran](https://avatars.discourse-cdn.com/v4/letter/s/3be4f8/32.png) [@Suren\_Baskaran](https://forum.ansible.com/u/Suren_Baskaran)\
**Post date:** [April 9, 2018, 6:47am UTC](https://forum.ansible.com/t/need-to-execute-complex-unix-commands/27222/2 "2018-04-09T06:47:02Z")

</div>

How about

1. a creating a script with commands ,
2. execute
3. and remove

Thanks,  
Suren VB

---

<div class="post-metadata">

**Author:** ![sdoran](https://avatars.discourse-cdn.com/v4/letter/s/47e85d/32.png) [@sdoran](https://forum.ansible.com/u/sdoran)\
**Post date:** [April 9, 2018, 7:59pm UTC](https://forum.ansible.com/t/need-to-execute-complex-unix-commands/27222/3 "2018-04-09T19:59:34Z")

</div>

If you need to execute complex shell command, a script file run by the script module is your best bet.

This particular examples look like you’re analyzing running processes and getting a string back, not making changes to the system. This type of thing is better done by a facts plugin. Look at [service\_facts](https://github.com/ansible/ansible/blob/08a1d47427d26c1640a2dd41a431ca9c8e255873/lib/ansible/modules/system/service_facts.py) for an example.

---

<div class="post-metadata">

**Author:** ![Marcin\_Dulak](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@Marcin\_Dulak](https://forum.ansible.com/u/Marcin_Dulak)\
**Post date:** [April 15, 2018, 10:54pm UTC](https://forum.ansible.com/t/need-to-execute-complex-unix-commands/27222/4 "2018-04-15T22:54:07Z")

</div>

`

- hosts: localhost

tasks:

- name: fact command  
set\_fact:  
fact\_command: echo “ps -ef|grep data”|sed ‘s~[\[1\]/]+~~g’|sed “s/$/$(date +”%d\_%m\_%Y\_%H\_%M\_%S")/"

- name: run command  
shell: “{{ fact\_command }}”  
`

`  
ansible-playbook test.yml -v  
Using /etc/ansible/ansible.cfg as config file  
[WARNING]: provided hosts list is empty, only localhost is available

PLAY \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [setup] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [fact command] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {“ansible\_facts”: {“fact\_command”: “echo "ps -ef|grep data"|sed ‘s~[\[2\]/]\+~~g’|sed "s/$/$(date +"%d\_%m\_%Y\_%H\_%M\_%S")/"”}, “changed”: false}

TASK [run command] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [localhost] =\> {“changed”: true, “cmd”: “echo "ps -ef|grep data"|sed ‘s~[\[3\]/]\+~~g’|sed "s/$/$(date +"%d\_%m\_%Y\_%H\_%M\_%S")/"”, “delta”: “0:00:00.002489”, “end”: “2018-04-16 00:47:44.957419”, “rc”: 0, “start”: “2018-04-16 00:47:44.954930”, “stderr”: “”, “stdout”: “psefgrepdata16\_04\_2018\_00\_47\_44”, “stdout\_lines”: [“psefgrepdata16\_04\_2018\_00\_47\_44”], “warnings”: }

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=3 changed=1 unreachable=0 failed=0  
`

* * *

1. :alnum: 

2. :alnum: 

3. :alnum:

---

<div class="post-metadata">

**Author:** ![Akash\_Agarwal](https://avatars.discourse-cdn.com/v4/letter/a/b2d939/32.png) [@Akash\_Agarwal](https://forum.ansible.com/u/Akash_Agarwal)\
**Post date:** [April 16, 2018, 2:12am UTC](https://forum.ansible.com/t/need-to-execute-complex-unix-commands/27222/5 "2018-04-16T02:12:29Z")

</div>

Thanks!

Is it possible to run a list of commands using this. I was able to read the commands from a file; paas it to a custom bash module and execute it.  
Which is the better way ?

Thanks,  
Akash

---

<div class="post-metadata">

**Author:** ![Marcin\_Dulak](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@Marcin\_Dulak](https://forum.ansible.com/u/Marcin_Dulak)\
**Post date:** [April 16, 2018, 7:38am UTC](https://forum.ansible.com/t/need-to-execute-complex-unix-commands/27222/6 "2018-04-16T07:38:20Z")

</div>

`

- name: fact command  
set\_fact:  
fact\_command: \>  
echo “ps -ef|grep data”|sed ‘s~[\[1\]/]+~~g’|sed “s/$/$(date +”%d\_%m\_%Y\_%H\_%M\_%S")/"  
;  
echo something else

`

* * *

1. :alnum:
