# Looping through a register variable

**URL:** <https://forum.ansible.com/t/looping-through-a-register-variable/38793>\
**Category:** Ansible Project\
**Created:** [May 23, 2024, 1:07pm UTC](https://forum.ansible.com/t/looping-through-a-register-variable/38793 "2024-05-23T13:07:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [May 23, 2024, 1:07pm UTC](https://forum.ansible.com/t/looping-through-a-register-variable/38793/1 "2024-05-23T13:07:36Z")

</div>

Good day, all. I hope I’m not wearing out my welcome with too many questions.

In the following playbook, I first find any know\_host file for any user on a particular system. I then copy a file with the list of additions to add to the known\_hosts files. What I want to do is use the output of my register values to make changes to those know\_host files, which my last play does. My iteration is wrong, though.

The playbook:

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [May 23, 2024, 2:50pm UTC](https://forum.ansible.com/t/looping-through-a-register-variable/38793/2 "2024-05-23T14:50:07Z")

</div>

You want stdout\_lines rather than stdout. The former is a list with new-lines removed. The latter is a possibly very long string with the complete output stream intact.

```auto
    - name: Append to all known_host files
      shell: cat /tmp/append >> {{ item }}
      loop:
        - '{{ known.stdout_lines }}'

```

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [May 23, 2024, 3:17pm UTC](https://forum.ansible.com/t/looping-through-a-register-variable/38793/3 "2024-05-23T15:17:15Z")

</div>

Todd, I made the change, but, the last play failed:

**TASK [Append to all known\_host files] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\***  
**Thursday 23 May 2024 11:06:33 -0400 (0:00:01.629) 0:00:06.012 \*\*\*\*\*\*\*\*\*\***  
**Thursday 23 May 2024 11:06:33 -0400 (0:00:01.629) 0:00:06.011 \*\*\*\*\*\*\*\*\*\***  
**failed: [myhost] (item=[‘/root/.ssh/known\_hosts’, ‘/home/user1/.ssh/known\_hosts’, ‘/home/user2/.ssh/known\_hosts’, ‘/home/user3/.ssh/known\_hosts’, ‘/home/user4/.ssh/known\_hosts’]) =\> changed=true**  
**ansible\_loop\_var: item**  
**cmd: cat /tmp/append \>\> [‘/root/.ssh/known\_hosts’, ‘/home/user1/.ssh/known\_hosts’, ‘/home/user2/.ssh/known\_hosts’, ‘/home/user3/.ssh/known\_hosts’, ‘/home/user4/.ssh/known\_hosts’]**  
**delta: ‘0:00:00.012330’**  
**end: ‘2024-05-23 11:06:33.947617’**  
**item:**  
**- /root/.ssh/known\_hosts**  
**- /home/user1/.ssh/known\_hosts**  
**- /home/user2/.ssh/known\_hosts**  
**- /home/user3/.ssh/known\_hosts**  
**- /home/user4/.ssh/known\_hosts**  
**msg: non-zero return code**  
**rc: 1**  
**start: ‘2024-05-23 11:06:33.935287’**  
**stderr: ‘/bin/sh: line 1: [/root/.ssh/known\_hosts,: No such file or directory’**  
**stderr\_lines:**   
**stdout: ‘’**  
**stdout\_lines:**

I wonder if **[** in **[/root/.ssh/known\_hosts** is getting in the way, since **/root/.ssh/known\_host** does exist.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [May 23, 2024, 3:30pm UTC](https://forum.ansible.com/t/looping-through-a-register-variable/38793/4 "2024-05-23T15:30:08Z")

</div>

Gah! Of course. known.stdout\_lines is a list, so

```auto
loop:
  - '{{ known.stdout_lines }}'

```

passes the whole list as a single item. So you need to not put the list in a list:

```auto
loop: '{{ known.stdout_lines }}'

```

should do the trick.

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [May 23, 2024, 9:00pm UTC](https://forum.ansible.com/t/looping-through-a-register-variable/38793/5 "2024-05-23T21:00:36Z")

</div>

Yupper, that worked. And, I appreciate the explanation. As the Farber College motto states, “Learning is Good”.

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [May 24, 2024, 3:09am UTC](https://forum.ansible.com/t/looping-through-a-register-variable/38793/6 "2024-05-24T03:09:53Z")

</div>

> You want stdout\_lines rather than stdout. The former is a list with new-lines removed. The latter is a possibly very long string with the complete output stream intact.
> 
> ```auto
> - name: Append to all known_host files
> shell: cat /tmp/append >> {{ item }}
> loop:
> - '{{ known.stdout_lines }}'
> 
> ```

That should probably just loop over the list itself? So:

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [May 24, 2024, 11:41am UTC](https://forum.ansible.com/t/looping-through-a-register-variable/38793/7 "2024-05-24T11:41:20Z")

</div>

Very subtle but very different. Great catch Todd.

In the first instance you create a list that contains a single item – another list called known.stdout\_lines.

loop:

- ‘{{ known.stdout\_lines }}’

In the second instance you provide a list called known.stdout\_lines as the list for the loop:

loop: ‘{{ known.stdout\_lines }}’

The second instance is the desired behavior. The difference is subtle at first glance but makes a world of difference in execution.

Walter

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [May 24, 2024, 12:43pm UTC](https://forum.ansible.com/t/looping-through-a-register-variable/38793/8 "2024-05-24T12:43:12Z")

</div>

Here is an illustration of the difference for those interested.

- name: test loop constructs

hosts: localhost

gather\_facts: false

become: false

vars:

my\_list: [one, two, three]

tasks:

- name: construct 1

debug: var=item

loop:

- ‘{{ my\_list }}’

- name: construct 2

debug: var=item

loop: ‘{{ my\_list }}’

**% ansible-playbook -i localhost, loop.yml**

PLAY [test loop constructs] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [construct 1] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

ok: [localhost] =\> (item=[‘one’, ‘two’, ‘three’]) =\> {

“ansible\_loop\_var”: “item”,

“item”: [

“one”,

“two”,

“three”

]

}

TASK [construct 2] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

ok: [localhost] =\> (item=one) =\> {

“ansible\_loop\_var”: “item”,

“item”: “one”

}

ok: [localhost] =\> (item=two) =\> {

“ansible\_loop\_var”: “item”,

“item”: “two”

}

ok: [localhost] =\> (item=three) =\> {

“ansible\_loop\_var”: “item”,

“item”: “three”

}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

Walter
