register with with_together not working

344 views
Skip to first unread message

AnsiNoob

unread,
Sep 5, 2016, 10:10:30 PM9/5/16
to Ansible Project
I want to use register with loops (http://docs.ansible.com/ansible/playbooks_loops.html#using-register-with-a-loop) to accomplish below tasks:

-- Surf through couple of variables arrays in with_together format and save result to a variable
-- now surf through that result set variable and perform specific action.


My register works fine with usual playbooks but the moment I use register with with_together then no value is saved in variable? :(

My playbook:
- name: check for port on remote host
  hosts: localhost
  connection: local
  vars_files:
    - vars1.yml

  tasks:
    - name: check for ports
      debug: msg="x is {{ item.0 }}, y is {{ item.1 }} and z is {{ item.2 }}"
      with_together:
        - "{{ x }}"
        - "{{ y }}"
        - "{{ z }}"
      register: result

    - debug: var=result.stdout_lines

My Variables File:
---
x: ['1','1','1','1','1','2','2','2','2','2','3','3']
y: ['0','0','0','0','0','0','0','0','0','0','0','0']
z: ['1','2','3','4','5','1','2','3','4','5','1','2']

My output (as you can see I get variable not defined at the end. I have tested that if I did not have "with_together" then register works just fine):
ansible-playbook pb.yml
 [WARNING]: provided hosts list is empty, only localhost is available


PLAY [check for port on remote host] *******************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [check for ports] *********************************************************
ok: [localhost] => (item=[u'1', u'0', u'1']) => {
    "item": [
        "1",
        "0",
        "1"
    ],
    "msg": "x is 1, y is 0 and z is 1"
}
ok: [localhost] => (item=[u'1', u'0', u'2']) => {
    "item": [
        "1",
        "0",
        "2"
    ],
    "msg": "x is 1, y is 0 and z is 2"
}
ok: [localhost] => (item=[u'1', u'0', u'3']) => {
    "item": [
        "1",
        "0",
        "3"
    ],
    "msg": "x is 1, y is 0 and z is 3"
}
ok: [localhost] => (item=[u'1', u'0', u'4']) => {
    "item": [
        "1",
        "0",
        "4"
    ],
    "msg": "x is 1, y is 0 and z is 4"
}
ok: [localhost] => (item=[u'1', u'0', u'5']) => {
    "item": [
        "1",
        "0",
        "5"
    ],
    "msg": "x is 1, y is 0 and z is 5"
}
ok: [localhost] => (item=[u'2', u'0', u'1']) => {
    "item": [
        "2",
        "0",
        "1"
    ],
    "msg": "x is 2, y is 0 and z is 1"
}
ok: [localhost] => (item=[u'2', u'0', u'2']) => {
    "item": [
        "2",
        "0",
        "2"
    ],
    "msg": "x is 2, y is 0 and z is 2"
}
ok: [localhost] => (item=[u'2', u'0', u'3']) => {
    "item": [
        "2",
        "0",
        "3"
    ],
    "msg": "x is 2, y is 0 and z is 3"
}
ok: [localhost] => (item=[u'2', u'0', u'4']) => {
    "item": [
        "2",
        "0",
        "4"
    ],
    "msg": "x is 2, y is 0 and z is 4"
}
ok: [localhost] => (item=[u'2', u'0', u'5']) => {
    "item": [
        "2",
        "0",
        "5"
    ],
    "msg": "x is 2, y is 0 and z is 5"
}
ok: [localhost] => (item=[u'3', u'0', u'1']) => {
    "item": [
        "3",
        "0",
        "1"
    ],
    "msg": "x is 3, y is 0 and z is 1"
}
ok: [localhost] => (item=[u'3', u'0', u'2']) => {
    "item": [
        "3",
        "0",
        "2"
    ],
    "msg": "x is 3, y is 0 and z is 2"
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "result.stdout_lines": "VARIABLE IS NOT DEFINED!"
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0

Kai Stian Olstad

unread,
Sep 6, 2016, 7:57:37 AM9/6/16
to ansible...@googlegroups.com
On 05.09.2016 07:16, AnsiNoob wrote:
> My register works fine with usual playbooks but the moment I use
> register
> with with_together then no value is saved in variable? :(

That's because all the with_ that combines data have a different schema
than ordinary register.


> *My playbook:*
> - name: check for port on remote host
> hosts: localhost
> connection: local
> vars_files:
> - vars1.yml
>
> tasks:
> - name: check for ports
> debug: msg="x is {{ item.0 }}, y is {{ item.1 }} and z is {{
> item.2
> }}"
> with_together:
> - "{{ x }}"
> - "{{ y }}"
> - "{{ z }}"
> register: result
>
> - debug: var=result.stdout_lines

Try
- debug: var=result
instead and you will see that the schema is different for the with_'s


--
Kai Stian Olstad

Brian Coca

unread,
Sep 9, 2016, 8:18:06 PM9/9/16
to ansible...@googlegroups.com
Any time you use with_ (loops) you'll get a 'results' key with a list of the normal task results per item of the loop.


----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages