# shell  module with  multiple  arguments using variables

**URL:** <https://forum.ansible.com/t/shell-module-with-multiple-arguments-using-variables/30933>\
**Category:** Ansible Project\
**Created:** [August 1, 2019, 9:05pm UTC](https://forum.ansible.com/t/shell-module-with-multiple-arguments-using-variables/30933 "2019-08-01T21:05:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [August 1, 2019, 9:05pm UTC](https://forum.ansible.com/t/shell-module-with-multiple-arguments-using-variables/30933/1 "2019-08-01T21:05:15Z")

</div>

Hi,

I am trying to call variables to the shell module but failing …

`

- name: status of services  
hosts: myhost  
remote\_user: root  
gather\_facts: no  
vars:
- pp\_stat: “/etc/init.d/puppet status”
- pr\_date: “echo ‘========= as on date of ===========’; date”

strategy: free  
tasks:

- name: List the status of the puppet services  
ignore\_errors: yes  
shell: “{{ item }}”  
with\_items:
- pp\_stat
- pr\_date  
register: pupstat
- debug:  
var: pupstat.stdout\_lines  
`

error reads:

`  
TASK [List the status of the puppet services] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
failed: [wf-omsal1-01-01] (item=pp\_stat) =\> {“changed”: true, “cmd”: “pp\_stat”, “delta”: “0:00:00.004792”, “end”: “2019-08-02 01:02:19.344642”, “item”: “pp\_stat”, “msg”: “non-zero return code”, “rc”: 127, “start”: “2019-08-02 01:02:19.339850”, “stderr”: “/bin/sh: pp\_stat: command not found”, “stderr\_lines”: [“/bin/sh: pp\_stat: command not found”], “stdout”: “”, “stdout\_lines”: }  
failed: [wf-omsal1-01-01] (item=pr\_date) =\> {“changed”: true, “cmd”: “pr\_date”, “delta”: “0:00:00.004765”, “end”: “2019-08-02 01:02:24.356002”, “item”: “pr\_date”, “msg”: “non-zero return code”, “rc”: 127, “start”: “2019-08-02 01:02:24.351237”, “stderr”: “/bin/sh: pr\_date: command not found”, “stderr\_lines”: [“/bin/sh: pr\_date: command not found”], “stdout”: “”, “stdout\_lines”: }  
…ignoring

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [wf-omsal1-01-01] =\> {  
“pupstat.stdout\_lines”: “VARIABLE IS NOT DEFINED!”  
}  
`

what I am missing in calling the variables?  
using {{ }} to items also failed…

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [August 1, 2019, 9:08pm UTC](https://forum.ansible.com/t/shell-module-with-multiple-arguments-using-variables/30933/2 "2019-08-01T21:08:10Z")

</div>

When registering a variable with a loop the structure of the data is different.

Try doing a debug on just `pupstat` to see the data structure. See this documentation for more information: [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_loops.html#registering-variables-with-a-loop](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#registering-variables-with-a-loop)

---

<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:** [August 1, 2019, 9:09pm UTC](https://forum.ansible.com/t/shell-module-with-multiple-arguments-using-variables/30933/3 "2019-08-01T21:09:47Z")

</div>

Variables in with\_items need {{ }} around them.

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [August 1, 2019, 9:45pm UTC](https://forum.ansible.com/t/shell-module-with-multiple-arguments-using-variables/30933/4 "2019-08-01T21:45:23Z")

</div>

When running pup\_stat alone, it works.

Adding {{ }} around items did not help

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [August 2, 2019, 10:45am UTC](https://forum.ansible.com/t/shell-module-with-multiple-arguments-using-variables/30933/5 "2019-08-02T10:45:29Z")

</div>

Hi Martz,

I tried with a couple of options as per the link you have shared including the "shell: echo “{{ item }}” . but still I am unable to combine the right combination.

my current snippet:

`  
tasks:

- name: List the status of the puppet services  
ignore\_errors: yes  
shell: “{{ item }}”  
register: pupstat  
with\_items:
- “{{ pp\_stat }}”
- “{{ pr\_date }}”
- debug:  
var: pupstat  
`

and the output of debug is fine.

`  
TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [myhost] =\> {  
“pupstat”: {  
“changed”: true,  
“msg”: “All items completed”,  
“results”: [  
{  
“\_ansible\_ignore\_errors”: true,  
“\_ansible\_item\_label”: “/etc/init.d/puppet status”,  
“\_ansible\_item\_result”: true,  
“\_ansible\_no\_log”: false,  
“\_ansible\_parsed”: true,  
“changed”: true,  
“cmd”: “/etc/init.d/puppet status”,  
“delta”: “0:00:00.030987”,  
“end”: “2019-08-02 14:35:42.933291”,  
“failed”: false,  
“invocation”: {  
“module\_args”: {  
“\_raw\_params”: “/etc/init.d/puppet status”,  
“\_uses\_shell”: true,  
“argv”: null,  
“chdir”: null,  
“creates”: null,  
“executable”: null,  
“removes”: null,  
“stdin”: null,  
“warn”: true  
}  
},  
“item”: “/etc/init.d/puppet status”,  
“rc”: 0,  
“start”: “2019-08-02 14:35:42.902304”,  
“stderr”: “”,  
“stderr\_lines”: ,  
“stdout”: “puppetd (pid 5881) is running…”,  
“stdout\_lines”: [  
“puppetd (pid 5881) is running…”  
]  
},  
{  
“\_ansible\_ignore\_errors”: true,  
“\_ansible\_item\_label”: “echo ‘========= as on date of ===========’; date”,  
“\_ansible\_item\_result”: true,  
“\_ansible\_no\_log”: false,  
“\_ansible\_parsed”: true,  
“changed”: true,  
“cmd”: “echo ‘========= as on date of ===========’; date”,  
“delta”: “0:00:00.006032”,  
“end”: “2019-08-02 14:35:47.994833”,  
“failed”: false,  
“invocation”: {  
“module\_args”: {  
“\_raw\_params”: “echo ‘========= as on date of ===========’; date”,  
“\_uses\_shell”: true,  
“argv”: null,  
“chdir”: null,  
“creates”: null,  
“executable”: null,  
“removes”: null,  
“stdin”: null,  
“warn”: true  
}  
},  
“item”: “echo ‘========= as on date of ===========’; date”,  
“rc”: 0,  
“start”: “2019-08-02 14:35:47.988801”,  
“stderr”: “”,  
“stderr\_lines”: ,  
“stdout”: “========= as on date of ===========\nFri Aug 2 14:35:47 +04 2019”,  
“stdout\_lines”: [  
“========= as on date of ===========”,  
“Fri Aug 2 14:35:47 +04 2019”  
]  
}  
]  
}  
}

`

But debug with “stdout.lines” is giving an error

`  
TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [myhost] =\> {  
“pupstat.stdout\_lines”: “VARIABLE IS NOT DEFINED!: ‘dict object’ has no attribute ‘stdout\_lines’”  
}

`

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [August 2, 2019, 3:11pm UTC](https://forum.ansible.com/t/shell-module-with-multiple-arguments-using-variables/30933/6 "2019-08-02T15:11:31Z")

</div>

Now … I am able to print the output of items using debug module with items…  
… but “stdout\_lines” is not working …

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [August 3, 2019, 8:42pm UTC](https://forum.ansible.com/t/shell-module-with-multiple-arguments-using-variables/30933/7 "2019-08-03T20:42:15Z")

</div>

Finally , I got the right combination … Thaks for all …

`  
name: List the status of the puppet services  
ignore\_errors: yes  
shell: “{{ item }}”  
with\_items:

- pp\_stat
- pr\_date  
register: pupstat
- debug:  
msg: “{{ pupstat.results|map(attribute=‘stdout\_lines’)|list }}”

`
