<p>There are a couple of things here:</p>
<ol>
<li><p>The indent of &quot;with_items&quot; on the debug module is wrong, it should the same level as the &quot;debug&quot; word.</p>
</li>
<li><p>More importantly, in this case the &quot;with_items&quot; variable on the debug is wrong. When you added &quot;register&quot; to the cli_command module, you actually said &quot;collect the output from every iteration and return the whole lot&quot;. Therefore, you need to iterate over the variable you created with &quot;register&quot;, not the variable you passed into the command itself. Something like below (I'm not at a computer atm so this will need to be checked):</p>
</li>
</ol>
<pre><code>    - debug:
        msg: &quot;{{ item.stdout_lines }}&quot;
      with_items: &quot;{{ showcmds }}&quot;
</code></pre>
<p>edit: OK, sounds like this specific module doesn't return stdout_lines. Try running the debug with just &quot;{{ showcmds }}&quot; and without the &quot;with_items&quot; to get the actual structure of the variable.</p>

CC BY-SA 4.0
2021-07-26T13:43:18.090Z
with_items:
        - "show configuration protocols bgp group Level3 | display set |  match export"
        - "show configuration protocols bgp group Level3-1 | display set |  match export"

    - debug:
            msg: "{{ item.stdout_lines }}"
       with_items:  "{{ showcmds }}"
chester
CC BY-SA 4.0
2021-07-26T13:44:31.760Z
REsults in an error.  TASK [debug] **********************************************************************************************************
fatal: [stn4-llpb-01]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'stdout_lines'\n\nThe error appears to be in '/home/aa78103/ansible/show.yaml': line 51, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - debug:\n      ^ here\n"}
chester
CC BY-SA 4.0
2021-07-23T21:22:35.143Z
2021-07-26T20:59:34.020Z
2021-07-26T20:59:34.020Z
Brett Levene
<p>I can't seem to get the register to works with cli_command and looping of multiples commands.  Any suggestions?</p>
<p>-</p>
<pre><code>name:  Run show commands
  hosts:
    - all
  connection: network_cli
  gather_facts: no

  vars:
     shcmd:
       - show bgp sum | match 3356
       - show config protocols bgp group Level3

  tasks:

    - name: create report directory if needed
      set_fact:
        report_dir: &quot;{{ user_data_path }}/reports&quot;

    - name: confirm report directory
      file:
        path: &quot;{{ report_dir }}&quot;
        state: directory

    - name: Run commands
      cli_command:
          command: &quot;{{item}}&quot;
      register: &quot;showcmds&quot;
      with_items: &quot;{{shcmd}}&quot;

    - debug:
        msg: &quot;{{ showcmds.stdout_lines }}&quot;
        with_items: &quot;{{shcmd}}&quot;
</code></pre>

<ansible><networking>
Ansible cli_command loop register issues
chester
# Ansible cli_command loop register issues

(Asked by: username_0 on 2021-07-20)

I can't seem to get the register to works with cli_command and looping of multiples commands.  Any suggestions?
-
name:  Run show commands
  hosts:
    - all
  connection: network_cli
  gather_facts: no

  vars:
     shcmd:
       - show bgp sum | match 3356
       - show config protocols bgp group Level3

  tasks:

    - name: create report directory if needed
      set_fact:
        report_dir: "{{ user_data_path }}/reports"

    - name: confirm report directory
      file:
        path: "{{ report_dir }}"
        state: directory

    - name: Run commands
      cli_command:
          command: "{{item}}"
      register: "showcmds"
      with_items: "{{shcmd}}"

    - debug:
        msg: "{{ showcmds.stdout_lines }}"
        with_items: "{{shcmd}}"

**Tags:** ansible, networking


---

## 1 answers

### Answer by username_1 on 2021-07-23. Score: 2

There are a couple of things here:

The indent of "with_items" on the debug module is wrong, it should the same level as the "debug" word.

More importantly, in this case the "with_items" variable on the debug is wrong. When you added "register" to the cli_command module, you actually said "collect the output from every iteration and return the whole lot". Therefore, you need to iterate over the variable you created with "register", not the variable you passed into the command itself. Something like below (I'm not at a computer atm so this will need to be checked):


    - debug:
        msg: "{{ item.stdout_lines }}"
      with_items: "{{ showcmds }}"

edit: OK, sounds like this specific module doesn't return stdout_lines. Try running the debug with just "{{ showcmds }}" and without the "with_items" to get the actual structure of the variable.

> **Comments:**
> **username_0**: with_items:         - "show configuration protocols bgp group Level3 | display set |  match export"         - "show configuration protocols bgp group Level3-1 | display set |  match export"      - debug:             msg: "{{ item.stdout_lines }}"        with_items:  "{{ showcmds }}"  
> **username_0**: REsults in an error.  TASK [debug] ********************************************************************************************************** fatal: [stn4-llpb-01]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'stdout_lines'\n\nThe error appears to be in '/home/aa78103/ansible/show.yaml': line 51, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - debug:\n      ^ here\n"}  

---

