# loop with\_together and register

**URL:** <https://forum.ansible.com/t/loop-with-together-and-register/14169>\
**Category:** Ansible Project\
**Created:** [December 17, 2013, 4:48pm UTC](https://forum.ansible.com/t/loop-with-together-and-register/14169 "2013-12-17T16:48:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pablo\_Hinojosa\_Nava](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pablo\_Hinojosa\_Nava](https://forum.ansible.com/u/Pablo_Hinojosa_Nava)\
**Post date:** [December 17, 2013, 4:48pm UTC](https://forum.ansible.com/t/loop-with-together-and-register/14169/1 "2013-12-17T16:48:52Z")

</div>

Hi all,

I am trying to set a loop on with\_together way but I am using nested variables with lists, and I do not have clear how the is code .

My first variable is the next:

- vmStorage: { vcpu: 1,  
ram: 1024,  
storage: 10,  
vms: [{name: storage1-dev02,ip: 10.199.3.87},  
{name: storage2-dev02,ip: 10.199.3.88},  
{name: storage3-dev02,ip: 10.199.3.89}]  
}  
My second variable is a register created on this way:

- name: getting tty  
shell: virsh dumpxml {{ item.name }} | grep “tty=” | cut -d= -f3 | cut -d/ -f4 | cut -d' -f1  
with\_items: vmHash.vms  
register: ntty

Then I am using ntty variable as this without problem:

> - name: entering on the shell inside each tty  
> shell: “echo ‘6’ \> /dev/pts/{{ item.stdout }}”  
> with\_items: ntty.results

and also vmHash.vms variables, for example with:

> - name: Do not worry about the previous error, checking if the vms are up  
> wait\_for: host={{ item.ip }}  
> port=22  
> timeout=200  
> delegate\_to: localhost  
> with\_items: vmHash.vms

**but the problem is when I want to use the with\_together loop with these variables.**

The command is the next:

> shell: “echo ‘sed -i ‘’ ‘s/{{ goldenip }}/{{ first list variables }}/g’ /cf/conf/config.xml’ \> /dev/pts/{{ second list variables }}”

I have tried a lot of ways without success, for example:

> - name: send stuff2 to all ttys  
> shell: “echo ‘sed -i ‘’ ‘s/{{ goldenip }}/{{ item.0.ip }}/g’ /cf/conf/config.xml’ \> /dev/pts/{{ item.1.stdout }}”  
> with\_together:
> - vmHash.vms
> - ntty.results  
> with the next error TASK: [send stuff2 to all ttys] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> fatal: [x.x] =\> One or more undefined variables: ‘str object’ has no attribute ‘ip’

or

> - name: send stuff2 to all ttys  
> shell: “echo ‘sed -i ‘’ ‘s/{{ goldenip }}/{{ item.0 }}/g’ /cf/conf/config.xml’ \> /dev/pts/{{ item.1 }}”  
> with\_together:
> - vmHash.vms.ip
> - ntty.results.ip

with the next error:

> TASK: [send stuff2 to all ttys] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> failed: [s04.lab] =\> (item=[‘v’, ‘n’]) =\> {“changed”: true, “cmd”: "echo ‘sed -i ‘’ ‘s/10.199.3.82/v/g’ /cf/conf/config.xml’ \> /dev/pts/n ", “delta”: “0:00:00.005936”, “end”: “2013-12-17 16:04:15.616932”, “item”: [“v”, “n”], “rc”: 1, “start”: “2013-12-17 16:04:15.610996”}  
> stderr: /bin/sh: /dev/pts/n: Permission denied  
> failed: [s04.lab] =\> (item=[‘m’, ‘t’]) =\> {“changed”: true, “cmd”: "echo ‘sed -i ‘’ ‘s/10.199.3.82/m/g’ /cf/conf/config.xml’ \> /dev/pts/t ", “delta”: “0:00:00.005735”, “end”: “2013-12-17 16:04:17.796956”, “item”: [“m”, “t”], “rc”: 1, “start”: “2013-12-17 16:04:17.791221”}  
> stderr: /bin/sh: /dev/pts/t: Permission denied

(It is getting the name of the variable)

How could I run the command using the ip and the number of tty?

Cheers,

Pablo Hinojosa

---

<div class="post-metadata">

**Author:** ![tannerjc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tannerjc/32/985_2.png) [@tannerjc](https://forum.ansible.com/u/tannerjc)\
**Post date:** [December 17, 2013, 5:03pm UTC](https://forum.ansible.com/t/loop-with-together-and-register/14169/2 "2013-12-17T17:03:40Z")

</div>

Could you copy your vars and the playbook to a gist(gist.github.com) ? The formatting is a bit hard to read in this email.

It would also be nice to see the output of -debug: var=ntty after the "getting tty" task so that we understand what the datastructure looks like.

---

<div class="post-metadata">

**Author:** ![Pablo\_Hinojosa\_Nava](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pablo\_Hinojosa\_Nava](https://forum.ansible.com/u/Pablo_Hinojosa_Nava)\
**Post date:** [December 20, 2013, 9:12am UTC](https://forum.ansible.com/t/loop-with-together-and-register/14169/3 "2013-12-20T09:12:21Z")

</div>

Hi James,

here you are the gist [https://gist.github.com/Pablohn26/88b7770d0bc6e5d7da06](https://gist.github.com/Pablohn26/88b7770d0bc6e5d7da06)

I have added the output of ntty after and before getting tty. It is weird that the value is a Hello World after the “getting tty” (also the results):

TASK: [debug var=ntty] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [s04.lab] =\> {“item”: “”, “msg”: “Hello world!”}

TASK: [debug var=ntty.results] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [s04.lab] =\> {“item”: “”, “msg”: “Hello world!”}

There it might be the problem.

Then, when I run the command it run:  
echo ‘sed -i ‘’ ‘s/10.199.3.82/v/g’ /cf/conf/config.xml’ \> **/dev/pts/n**

echo ‘sed -i ‘’ ‘s/10.199.3.82/v/g’ /cf/conf/config.xml’ \> **/dev/pts/t**  
echo ‘sed -i ‘’ ‘s/10.199.3.82/v/g’ /cf/conf/config.xml’ \> **/dev/pts/t**  
echo ‘sed -i ‘’ ‘s/10.199.3.82/v/g’ /cf/conf/config.xml’ \> **/dev/pts/y**

echo ‘sed -i ‘’ ‘s/10.199.3.82/v/g’ /cf/conf/config.xml’ \> **/dev/pts/.**

echo ‘sed -i ‘’ ‘s/10.199.3.82/v/g’ /cf/conf/config.xml’ \> **/dev/pts/r**  
…

The main question is: how to mix registers, nested variables and with\_together loops?

Cheers

---

<div class="post-metadata">

**Author:** ![Pablo\_Hinojosa\_Nava](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pablo\_Hinojosa\_Nava](https://forum.ansible.com/u/Pablo_Hinojosa_Nava)\
**Post date:** [January 7, 2014, 1:47pm UTC](https://forum.ansible.com/t/loop-with-together-and-register/14169/4 "2014-01-07T13:47:10Z")

</div>

If I use nested variables it takes the name of the variable as a string and loop over it. Is it the actual use? is it a bug? or I am doing something wrong.

Cheers

---

<div class="post-metadata">

**Author:** ![Pablo\_Hinojosa\_Nava](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pablo\_Hinojosa\_Nava](https://forum.ansible.com/u/Pablo_Hinojosa_Nava)\
**Post date:** [January 17, 2014, 9:09pm UTC](https://forum.ansible.com/t/loop-with-together-and-register/14169/5 "2014-01-17T21:09:29Z")

</div>

I share the answer because maybe somedoby will need it, the answer is the next:

- name: send stuff2 to all ttys  
shell: “echo ‘sed -i ‘’ ‘s/{{ goldenip }}/{{ item.0.ip }}/g’ /cf/conf/config.xml’ \> /dev/pts/{{ item.1.stdout }}”  
with\_together:
- vmHash.vms
- ntty.results

I do not why the solution is the same than 1 month ago gave me problems as I said in the gist [https://gist.github.com/Pablohn26/88b7770d0bc6e5d7da06#file-ansible-loop-with\_together-L44](https://gist.github.com/Pablohn26/88b7770d0bc6e5d7da06#file-ansible-loop-with_together-L44)

Maybe was an evil typo or maybe the new version of ansible. Never mind. Is a shame to have my task blocked 1 month (and due to that, lost my job) because this (my mind was completely blocked), but that is part of this wonderful game.

Cheers
