loop with_together and register

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

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.

Hi James,

here you are the gist 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

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

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

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