Answers: <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>
  
Answers: CC BY-SA 4.0
Answers: 2024-06-05T05:18:10.993Z
Answers: 2024-07-19T06:45:50.687Z
Answers: 2024-07-19T06:45:50.687Z
Answers: Vladimir Botka
Body: <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>
  
Tags: <ansible>
Title: Take shell cmd stdout_lines and create dict from output
UserDisplayName: OldManWaterfall
ThreadText: # 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
  
  ```yaml
  - set_fact:
      needrestart: "{{ command_results.stdout | from_yaml }}"
  
  ```
  
  Details: Given the below file for testing
  
  ```yaml
  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
  
  ```yaml
      - command: cat needrestart.txt
        register: command_results
  
  ```
  
  You can see that next to the YAML list in the attribute *`stdout_lines`*
  
  ```yaml
    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`*
  
  ```yaml
    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](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/from_yaml_filter.html) to (quote):
  
  > Converts a YAML string representation into an equivalent structured Ansible variable.
  
  For example,
  
  ```yaml
    needrestart: "{{ command_results.stdout | from_yaml }}"
  
  ```
  
  gives what you want
  
  ```yaml
    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
  
  ```yaml
    n3: "{{ command_results.stdout_lines | map('from_yaml') | combine }}"
  
  ```
  
  gives the same result
  
  ```yaml
    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
  
  ```yaml
      n4: "{{ dict(command_results.stdout_lines | map('split', ':')) }}"
  
  ```
  
  * Example of a complete playbook for testing
  
  ```yaml
  - 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']
  
  ```
  
  
  ---
  
  