<p>A: The value of
<em><code>command_results.stdout_lines</code></em> is the YAML list. Nothing happens to the list when you filter it <em><code>from_yaml</code></em>. Use <em><code>command_results.stdout</code></em> instead</p>
<pre class="lang-yaml prettyprint-override"><code>- set_fact:
    needrestart: &quot;{{ command_results.stdout | from_yaml }}&quot;
</code></pre>
<p>Details: Given the below file for testing</p>
<pre class="lang-yaml prettyprint-override"><code>shell&gt; cat needrestart.txt 
NEEDRESTART-VER: 3.6
NEEDRESTART-KCUR: 5.14.0-362.24.1.el9_3.0.1.x86_64
NEEDRESTART-KEXP: 5.14.0-362.24.1.el9_3.0.1.x86_64
NEEDRESTART-KSTA: 2
NEEDRESTART-UCSTA: 0
</code></pre>
<p>Please read it</p>
<pre class="lang-yaml prettyprint-override"><code>    - command: cat needrestart.txt
      register: command_results
</code></pre>
<p>You can see that next to the YAML list in the attribute <em><code>stdout_lines</code></em></p>
<pre class="lang-yaml prettyprint-override"><code>  command_results.stdout_lines:
  - 'NEEDRESTART-VER: 3.6'
  - 'NEEDRESTART-KCUR: 5.14.0-362.24.1.el9_3.0.1.x86_64'
  - 'NEEDRESTART-KEXP: 5.14.0-362.24.1.el9_3.0.1.x86_64'
  - 'NEEDRESTART-KSTA: 2'
  - 'NEEDRESTART-UCSTA: 0'
</code></pre>
<p>there is also the YAML text bloc in the attribute <em><code>stdout</code></em></p>
<pre class="lang-yaml prettyprint-override"><code>  command_results.stdout: |-
    NEEDRESTART-VER: 3.6
    NEEDRESTART-KCUR: 5.14.0-362.24.1.el9_3.0.1.x86_64
    NEEDRESTART-KEXP: 5.14.0-362.24.1.el9_3.0.1.x86_64
    NEEDRESTART-KSTA: 2
    NEEDRESTART-UCSTA: 0
</code></pre>
<p>Now, you can use <a href="https://docs.ansible.com/ansible/latest/collections/ansible/builtin/from_yaml_filter.html" rel="nofollow noreferrer">from_yaml</a> to (quote):</p>
<blockquote>
<p>Converts a YAML string representation into an equivalent structured Ansible variable.</p>
</blockquote>
<p>For example,</p>
<pre class="lang-yaml prettyprint-override"><code>  needrestart: &quot;{{ command_results.stdout | from_yaml }}&quot;
</code></pre>
<p>gives what you want</p>
<pre class="lang-yaml prettyprint-override"><code>  needrestart:
    NEEDRESTART-KCUR: 5.14.0-362.24.1.el9_3.0.1.x86_64
    NEEDRESTART-KEXP: 5.14.0-362.24.1.el9_3.0.1.x86_64
    NEEDRESTART-KSTA: 2
    NEEDRESTART-UCSTA: 0
    NEEDRESTART-VER: 3.6

  needrestart['NEEDRESTART-VER']: '3.6'
</code></pre>
<hr>
<p>Notes:</p>
<ul>
<li>If you have a list only there are more options. You can convert the items to YAML dictionaries and <em>combine</em> them</li>
</ul>
<pre class="lang-yaml prettyprint-override"><code>  n3: &quot;{{ command_results.stdout_lines | map('from_yaml') | combine }}&quot;
</code></pre>
<p>gives the same result</p>
<pre class="lang-yaml prettyprint-override"><code>  n3:
    NEEDRESTART-KCUR: 5.14.0-362.24.1.el9_3.0.1.x86_64
    NEEDRESTART-KEXP: 5.14.0-362.24.1.el9_3.0.1.x86_64
    NEEDRESTART-KSTA: 2
    NEEDRESTART-UCSTA: 0
    NEEDRESTART-VER: 3.6

  n3['NEEDRESTART-VER']: '3.6'
</code></pre>
<ul>
<li>The next option is using the function <em>dict</em> on the split items. The below declaration also gives the same result</li>
</ul>
<pre class="lang-yaml prettyprint-override"><code>    n4: &quot;{{ dict(command_results.stdout_lines | map('split', ':')) }}&quot;
</code></pre>
<ul>
<li>Example of a complete playbook for testing</li>
</ul>
<sup>
<pre class="lang-yaml prettyprint-override"><code>- hosts: localhost

  vars:

    n1: &quot;{{ command_results.stdout_lines | from_yaml }}&quot;
    n2: &quot;{{ command_results.stdout | from_yaml }}&quot;
    n3: &quot;{{ command_results.stdout_lines | map('from_yaml') | combine }}&quot;
    n4: &quot;{{ dict(command_results.stdout_lines | map('split', ':')) }}&quot;

  tasks:

    - command: cat needrestart.txt
      register: command_results

    - debug:
        var: command_results.stdout_lines
    - debug:
        var: command_results.stdout

    - debug:
        var: n1
    - debug:
        var: n2
    - debug:
        var: n2['NEEDRESTART-VER']
    - debug:
        var: n3
    - debug:
        var: n3['NEEDRESTART-VER']
    - debug:
        var: n4
    - debug:
        var: n4['NEEDRESTART-VER']
</code></pre>
</sup>

CC BY-SA 4.0
2024-06-05T05:18:10.993Z
2024-07-19T06:45:50.687Z
2024-07-19T06:45:50.687Z
Vladimir Botka
<p>I am using needrestart -b to get the following example output</p>
<pre><code>NEEDRESTART-VER: 3.6
NEEDRESTART-KCUR: 5.14.0-362.24.1.el9_3.0.1.x86_64
NEEDRESTART-KEXP: 5.14.0-362.24.1.el9_3.0.1.x86_64
NEEDRESTART-KSTA: 2
NEEDRESTART-UCSTA: 0
</code></pre>
<p>How would one convert this into a dictionary for use in my playbook? I've done it before but I can't for the life of me remember how I did it. See my current playbook below. AI recommended the &quot;from_yaml&quot; but it didn't work.</p>
<pre><code>---
- name: Reboot if necessary
  hosts: all
  gather_facts: true  

  tasks:
    - name: Install needrestart RedHat
      become: true
      when: ansible_facts['os_family'] == &quot;RedHat&quot;
      ansible.builtin.dnf:
        name: 'needrestart'
        state: latest # noqa package-latest

    - name: Install needrestart Debian
      become: true
      when: ansible_facts['os_family'] == &quot;Debian&quot;
      ansible.builtin.apt:
        name: 'needrestart'
        state: latest # noqa package-latest

    - name: Check if system needs to be rebooted
      become: true
      ansible.builtin.shell:
        cmd: needrestart -b        
      register: command_results
    
    - name: Convert previous output to dictionary
      set_fact:
        needrestart: &quot;{{command_results.stdout_lines | from_yaml }}&quot;      

    - name: Debug print needrestart results
      ansible.builtin.debug:
        var: needrestart['NEEDRESTART-VER']
</code></pre>

<ansible>
Take shell cmd stdout_lines and create dict from output
OldManWaterfall
# Take shell cmd stdout_lines and create dict from output

(Asked by: username_0 on 2024-06-04)

I am using needrestart -b to get the following example output
NEEDRESTART-VER: 3.6
NEEDRESTART-KCUR: 5.14.0-362.24.1.el9_3.0.1.x86_64
NEEDRESTART-KEXP: 5.14.0-362.24.1.el9_3.0.1.x86_64
NEEDRESTART-KSTA: 2
NEEDRESTART-UCSTA: 0

How would one convert this into a dictionary for use in my playbook? I've done it before but I can't for the life of me remember how I did it. See my current playbook below. AI recommended the "from_yaml" but it didn't work.
---
- name: Reboot if necessary
  hosts: all
  gather_facts: true  

  tasks:
    - name: Install needrestart RedHat
      become: true
      when: ansible_facts['os_family'] == "RedHat"
      ansible.builtin.dnf:
        name: 'needrestart'
        state: latest # noqa package-latest

    - name: Install needrestart Debian
      become: true
      when: ansible_facts['os_family'] == "Debian"
      ansible.builtin.apt:
        name: 'needrestart'
        state: latest # noqa package-latest

    - name: Check if system needs to be rebooted
      become: true
      ansible.builtin.shell:
        cmd: needrestart -b        
      register: command_results
    
    - name: Convert previous output to dictionary
      set_fact:
        needrestart: "{{command_results.stdout_lines | from_yaml }}"      

    - name: Debug print needrestart results
      ansible.builtin.debug:
        var: needrestart['NEEDRESTART-VER']

**Tags:** ansible


---

## 1 answers

### Answer by username_1 on 2024-06-05. Score: 2 | Accepted answer

A: The value of
command_results.stdout_lines is the YAML list. Nothing happens to the list when you filter it from_yaml. Use command_results.stdout instead
- set_fact:
    needrestart: "{{ command_results.stdout | from_yaml }}"

Details: Given the below file for testing
shell> cat needrestart.txt 
NEEDRESTART-VER: 3.6
NEEDRESTART-KCUR: 5.14.0-362.24.1.el9_3.0.1.x86_64
NEEDRESTART-KEXP: 5.14.0-362.24.1.el9_3.0.1.x86_64
NEEDRESTART-KSTA: 2
NEEDRESTART-UCSTA: 0

Please read it
    - command: cat needrestart.txt
      register: command_results

You can see that next to the YAML list in the attribute stdout_lines
  command_results.stdout_lines:
  - 'NEEDRESTART-VER: 3.6'
  - 'NEEDRESTART-KCUR: 5.14.0-362.24.1.el9_3.0.1.x86_64'
  - 'NEEDRESTART-KEXP: 5.14.0-362.24.1.el9_3.0.1.x86_64'
  - 'NEEDRESTART-KSTA: 2'
  - 'NEEDRESTART-UCSTA: 0'

there is also the YAML text bloc in the attribute stdout
  command_results.stdout: |-
    NEEDRESTART-VER: 3.6
    NEEDRESTART-KCUR: 5.14.0-362.24.1.el9_3.0.1.x86_64
    NEEDRESTART-KEXP: 5.14.0-362.24.1.el9_3.0.1.x86_64
    NEEDRESTART-KSTA: 2
    NEEDRESTART-UCSTA: 0

Now, you can use from_yaml to (quote):

Converts a YAML string representation into an equivalent structured Ansible variable.

For example,
  needrestart: "{{ command_results.stdout | from_yaml }}"

gives what you want
  needrestart:
    NEEDRESTART-KCUR: 5.14.0-362.24.1.el9_3.0.1.x86_64
    NEEDRESTART-KEXP: 5.14.0-362.24.1.el9_3.0.1.x86_64
    NEEDRESTART-KSTA: 2
    NEEDRESTART-UCSTA: 0
    NEEDRESTART-VER: 3.6

  needrestart['NEEDRESTART-VER']: '3.6'


Notes:

If you have a list only there are more options. You can convert the items to YAML dictionaries and combine them

  n3: "{{ command_results.stdout_lines | map('from_yaml') | combine }}"

gives the same result
  n3:
    NEEDRESTART-KCUR: 5.14.0-362.24.1.el9_3.0.1.x86_64
    NEEDRESTART-KEXP: 5.14.0-362.24.1.el9_3.0.1.x86_64
    NEEDRESTART-KSTA: 2
    NEEDRESTART-UCSTA: 0
    NEEDRESTART-VER: 3.6

  n3['NEEDRESTART-VER']: '3.6'


The next option is using the function dict on the split items. The below declaration also gives the same result

    n4: "{{ dict(command_results.stdout_lines | map('split', ':')) }}"


Example of a complete playbook for testing


- hosts: localhost

  vars:

    n1: "{{ command_results.stdout_lines | from_yaml }}"
    n2: "{{ command_results.stdout | from_yaml }}"
    n3: "{{ command_results.stdout_lines | map('from_yaml') | combine }}"
    n4: "{{ dict(command_results.stdout_lines | map('split', ':')) }}"

  tasks:

    - command: cat needrestart.txt
      register: command_results

    - debug:
        var: command_results.stdout_lines
    - debug:
        var: command_results.stdout

    - debug:
        var: n1
    - debug:
        var: n2
    - debug:
        var: n2['NEEDRESTART-VER']
    - debug:
        var: n3
    - debug:
        var: n3['NEEDRESTART-VER']
    - debug:
        var: n4
    - debug:
        var: n4['NEEDRESTART-VER']


---

