shell module with multiple arguments using variables

66 views
Skip to first unread message

Veera

unread,
Aug 1, 2019, 5:05:15 PM8/1/19
to Ansible Project
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..

Matt Martz

unread,
Aug 1, 2019, 5:09:01 PM8/1/19
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/ece5e2ff-7e5f-4d37-b7c4-64b6394be17c%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net

Kai Stian Olstad

unread,
Aug 1, 2019, 5:09:59 PM8/1/19
to ansible...@googlegroups.com
Variables in with_items need {{ }} around them.

--
Kai Stian Olstad

Veera Mani

unread,
Aug 1, 2019, 5:45:50 PM8/1/19
to ansible...@googlegroups.com

When running pup_stat alone, it works.

Adding {{ }} around items did not help



--
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/8ad-GDtPa6E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e744297c-9e97-56fa-2459-e7b505a4dc63%40olstad.com.

Veera

unread,
Aug 2, 2019, 6:45:29 AM8/2/19
to Ansible Project
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'"
}








To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Veera

unread,
Aug 2, 2019, 11:11:31 AM8/2/19
to Ansible Project

Now .. I am able to print the output of items using  debug module with items.. 
 .. but  "stdout_lines"  is  not working ...  

-
 name: List the   status of the puppet services
      ignore_errors
: yes
      shell
: "{{ item }}"
      with_items
:
       
- "{{ pp_stat }}"
       
- "{{ pr_date }}"
     
register: pupstat
   
- debug:

       
var: "{{ item.stdout_lines}}"
      with_items
:
       
- "{{ pupstat.results }}"





the output is as below:

TASK [debug] *************************************************************************************************************************
ok
: [myhost] => (item={'_ansible_parsed': True, 'stderr_lines': [], u'changed': True, u'stdout': u'puppetd (pid  5881) is running...', '_ansible_item_result': True, u'delta': u'0:00:00.027518', 'stdout_lines': [u'puppetd (pid  5881) is running...'], '_ansible_item_label': u'/etc/init.d/puppet status', u'end': u'2019-08-02 19:05:43.608320', '_ansible_no_log': False, 'failed': False, u'cmd': u'/etc/init.d/puppet status', 'item': u'/etc/init.d/puppet status', u'stderr': u'', u'rc': 0, u'invocation': {u'module_args': {u'warn': True, u'executable': None, u'_uses_shell': True, u'_raw_params': u'/etc/init.d/puppet status', u'removes': None, u'argv': None, u'creates': None, u'chdir': None, u'stdin': None}}, u'start': u'2019-08-02 19:05:43.580802', '_ansible_ignore_errors': True}) => {
   
"<type 'list'>": "VARIABLE IS NOT DEFINED!",
   
"item": {

       
"changed": true,
       
"cmd": "/etc/init.d/puppet status",

       
"delta": "0:00:00.027518",
       
"end": "2019-08-02 19:05:43.608320",

       
"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 19:05:43.580802",

       
"stderr": "",
       
"stderr_lines": [],
       
"stdout": "puppetd (pid  5881) is running...",
       
"stdout_lines": [
           
"puppetd (pid  5881) is running..."
       
]
   
}
}

ok
: [myhost] => (item={'_ansible_parsed': True, 'stderr_lines': [], u'changed': True, u'stdout': u'========= as on  date of ===========\nFri Aug  2 19:05:48 +04 2019', '_ansible_item_result': True, u'delta': u'0:00:00.005896', 'stdout_lines': [u'========= as on  date of ===========', u'Fri Aug  2 19:05:48 +04 2019'], '_ansible_item_label': u"echo '========= as on  date of ==========='; date", u'end': u'2019-08-02 19:05:48.695987', '_ansible_no_log': False, 'failed': False, u'cmd': u"echo '========= as on  date of ==========='; date", 'item': u"echo '========= as on  date of ==========='; date", u'stderr': u'', u'rc': 0, u'invocation': {u'module_args': {u'warn': True, u'executable': None, u'_uses_shell': True, u'_raw_params': u"echo '========= as on  date of ==========='; date", u'removes': None, u'argv': None, u'creates': None, u'chdir': None, u'stdin': None}}, u'start': u'2019-08-02 19:05:48.690091', '_ansible_ignore_errors': True}) => {
   
"<type 'list'>": "VARIABLE IS NOT DEFINED!",
   
"item": {

       
"changed": true,
       
"cmd": "echo '========= as on  date of ==========='; date",

       
"delta": "0:00:00.005896",
       
"end": "2019-08-02 19:05:48.695987",

       
"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 19:05:48.690091",
       
"stderr": "",
       
"stderr_lines": [],
       
"stdout": "========= as on  date of ===========\nFri Aug  2 19:05:48 +04 2019",

       
"stdout_lines": [
           
"========= as on  date of ===========",

           
"Fri Aug  2 19:05:48 +04 2019"
       
]
   
}
}



replacing "msg"  with  "var"  also prints a similar output..

I am looking for a  output like this 


"puppetd (pid  5881) is running..."


 
"========= as on  date of ===========",
 
"Fri Aug  2 19:05:48 +04 2019"
Message has been deleted

Veera

unread,
Aug 3, 2019, 4:42:16 PM8/3/19
to Ansible Project
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 }}"

Reply all
Reply to author
Forward
0 new messages