# with\_items to get values from a shell cmd output

**URL:** <https://forum.ansible.com/t/with-items-to-get-values-from-a-shell-cmd-output/32640>\
**Category:** Ansible Project\
**Created:** [April 9, 2020, 7:06am UTC](https://forum.ansible.com/t/with-items-to-get-values-from-a-shell-cmd-output/32640 "2020-04-09T07:06:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kiran\_Kumar](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kiran\_Kumar](https://forum.ansible.com/u/Kiran_Kumar)\
**Post date:** [April 9, 2020, 7:06am UTC](https://forum.ansible.com/t/with-items-to-get-values-from-a-shell-cmd-output/32640/1 "2020-04-09T07:06:26Z")

</div>

I got this error for below code - FAILED! =\> {“msg”: “template error while templating string: expected token ‘end of print statement’, got ‘=’. String: {{ var=oom.stdout }}”}

Please suggest , i am trying to get pid from shell cmd & then update it with echo

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [April 9, 2020, 7:30am UTC](https://forum.ansible.com/t/with-items-to-get-values-from-a-shell-cmd-output/32640/2 "2020-04-09T07:30:35Z")

</div>

The result from your grep/awk shell task consists of multiple lines, each with one PID.

So instead of stdout you should use stdout\_lines: [https://docs.ansible.com/ansible/latest/reference\_appendices/common\_return\_values.html#stdout-lines](https://docs.ansible.com/ansible/latest/reference_appendices/common_return_values.html#stdout-lines)

Also, you are iterating over a list of 1 item, which is incorrect as well.

Also, you’re trying to establish something called “os\_type” using a shell task, but that is not necessary - this is already available in the ansible facts (which you’ve explicitly gathered).  
So just use ‘ansible\_system’ instead.

Also, you declare “become: yes”, but also “remote\_user: root”. That’s double. Either connect as non-root user and only declare “become: yes” (which implies become\_user = root). Or just only declare “remote\_user: root”.

Try this:

- hosts: temp  
gather\_facts: yes  
remote\_user: root  
tasks:
- name: Adjust OOM to negative so that OOM killer does not kill below processes  
shell: ‘ps -ef|egrep “sssd|wdmd|portreserve|autofs|automount|ypbind|rpcbind|rpc.statd|rpc.mountd|rpc.idampd|ntpd|lmgrd|Xvnc|vncconfig|irqblance|rpc.rquotad|metric|nscd|crond|snpslmd|[getpwname.pl](http://getpwname.pl)|mysqld|rsyslogd|xinetd|sendmail|lsf|tigervnc|tightvnc|cfadm” |egrep -ve “ps|egrep” |awk “{print $2}”’  
register: oom  
when: ansible\_system == ‘Linux’
- debug: var=oom.stdout
- name: update the pid  
raw: echo -17 \> /proc/{{ item }}/oom\_adj  
loop: “{{ oom.stdout\_lines }}”

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [April 9, 2020, 12:50pm UTC](https://forum.ansible.com/t/with-items-to-get-values-from-a-shell-cmd-output/32640/3 "2020-04-09T12:50:13Z")

</div>

> The result from your grep/awk shell task consists of multiple lines, each with one PID.  
> So instead of stdout you should use  
> stdout\_lines: [https://docs.ansible.com/ansible/latest/reference\_appendices/common\_return\_values.html#stdout-lines](https://docs.ansible.com/ansible/latest/reference_appendices/common_return_values.html#stdout-lines)
> 
> Also, you are iterating over a list of 1 item, which is incorrect as well.
> 
> Also, you're trying to establish something called "os\_type" using a shell task, but that is not necessary - this is  
> already available in the ansible facts (which you've explicitly gathered).  
> So just use 'ansible\_system' instead.
> 
> Also, you declare "become: yes", but also "remote\_user: root". That's double. Either connect as non-root user and only  
> declare "become: yes" (which implies become\_user = root). Or just only declare "remote\_user: root".
> 
> Try this:
> 
> - hosts: temp  
> gather\_facts: yes  
> remote\_user: root  
> tasks:  
> - name: Adjust OOM to negative so that OOM killer does not kill below processes  
> shell: 'ps -ef|egrep  
> "sssd|wdmd|portreserve|autofs|automount|ypbind|rpcbind|rpc.statd|rpc.mountd|rpc.idampd|ntpd|lmgrd|Xvnc|vncconfig|irqblance|rpc.rquotad|metric|nscd|crond|snpslmd|getpwname.pl  
> \<[http://getpwname.pl](http://getpwname.pl)\>|mysqld|rsyslogd|xinetd|sendmail|lsf|tigervnc|tightvnc|cfadm" |egrep -ve "ps|egrep" |awk "{print \$2}"'  
> register: oom  
> when: ansible\_system == 'Linux'  
> - debug: var=oom.stdout  
> - name: update the pid  
> raw: echo -17 \> /proc/{{ item }}/oom\_adj  
> loop: "{{ oom.stdout\_lines }}"

This looks like a bad hack to me. Why don't you adjust OOMScoreAdjust in the system unit files of these services (or  
corresponding setting in init scripts) ??

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@Kiran\_Kumar](https://forum.ansible.com/u/Kiran_Kumar)\
**Post date:** [April 9, 2020, 5:16pm UTC](https://forum.ansible.com/t/with-items-to-get-values-from-a-shell-cmd-output/32640/4 "2020-04-09T17:16:51Z")

</div>

@Dick Visser

**Wow, that helped, thanks a lot !**

**Thanks for the valuable suggestions & nice explanation as well**
