New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SOLVED: ansible 2.0: with_items: "{{ variable }}_fixstring" resolve issue #14032

Closed
jardleex opened this Issue Jan 20, 2016 · 4 comments

Comments

Projects
None yet
4 participants
@jardleex

jardleex commented Jan 20, 2016

Hi,
i'm facing an issue with the with_items option in combination with a variable to replace + a fixed string behind the variable name. The issue came up after updating from 1.9.4 to 2.0.0.2.

Environment:

Ansible: 2.0.0.2-1
OS: Ubuntu 14.04
Python: 2.7.5-5ubuntu3

Steps To Reproduce:

site.yml

---
- hosts: localhost
  tags: withitems
  roles:
    - { role: withitems }


roles/withitems/tasks/main.yml

---
- name: debug01
  debug: msg={{ role_name }}

- name: debug02
  debug: msg="{{ role_name }}_anystring"
  with_items: nameofrole_anystring

- name: debug03
  debug: msg={{ item.name }}
  with_items: nameofrole_anystring

- name: debug04
  debug: msg={{ item }}
  with_items: "{{ role_name }}_anystring"

- name: debug05
  debug: msg={{ item.name }}
  with_items:
    - { name: foo }

- name: debug06
  debug: msg={{ item.name }}
  with_items: "{{ role_name }}_anystring"

roles/withitems/vars/main.yml

---
role_name: nameofrole

nameofrole_anystring:
  - { name: foo }

When executing this on Ansible 1.9.4:

:~ ansible --version
ansible 1.9.4
  configured module search path = None

:~ ansible-playbook testsite.yml --tags withitems

PLAY [localhost] **************************************************************

TASK: [withitems | debug01] ***************************************************
ok: [localhost] => {
    "msg": "nameofrole"
}

TASK: [withitems | debug02] ***************************************************
ok: [localhost] => (item={'name': 'foo'}) => {
    "item": {
        "name": "foo"
    },
    "msg": "nameofrole_anystring"
}

TASK: [withitems | debug03] ***************************************************
ok: [localhost] => (item={'name': 'foo'}) => {
    "item": {
        "name": "foo"
    },
    "msg": "foo"
}

TASK: [withitems | debug04] ***************************************************
ok: [localhost] => (item={'name': 'foo'}) => {
    "item": {
        "name": "foo"
    },
    "msg": "{'name':"
}

TASK: [withitems | debug05] ***************************************************
ok: [localhost] => (item={'name': 'foo'}) => {
    "item": {
        "name": "foo"
    },
    "msg": "foo"
}

TASK: [withitems | debug06] ***************************************************
ok: [localhost] => (item={'name': 'foo'}) => {
    "item": {
        "name": "foo"
    },
    "msg": "foo"
}

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

When executing this on Ansible 2.0.0.2:

:~ ansible --version
ansible 2.0.0.2
  config file = /root/ansible-test/ansible.cfg
  configured module search path = Default w/o overrides

:~ ansible-playbook testsite.yml --tags withitems

PLAY ***************************************************************************

TASK [withitems : debug01] *****************************************************
ok: [localhost] => {
    "msg": "withitems"
}

TASK [withitems : debug02] *****************************************************
ok: [localhost] => (item={u'name': u'foo'}) => {
    "item": {
        "name": "foo"
    },
    "msg": "withitems_anystring"
}

TASK [withitems : debug03] *****************************************************
ok: [localhost] => (item={u'name': u'foo'}) => {
    "item": {
        "name": "foo"
    },
    "msg": "foo"
}

TASK [withitems : debug04] *****************************************************
ok: [localhost] => (item=withitems_anystring) => {
    "item": "withitems_anystring",
    "msg": "withitems_anystring"
}

TASK [withitems : debug05] *****************************************************
ok: [localhost] => (item={u'name': u'foo'}) => {
    "item": {
        "name": "foo"
    },
    "msg": "foo"
}

TASK [withitems : debug06] *****************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "ERROR! 'unicode object' has no attribute 'name'"}

PLAY RECAP *********************************************************************
localhost                  : ok=5    changed=0    unreachable=0    failed=1

Actual Results:

Using with_items: "{{ variable_name }}_fixedstring" the playbook fails to find attributes on the item.

Expected Results:

Usage of above combination is able to find attributes like it was in version <2.0.

Let me know if i can provide further details. Thank you for your help.

@jardleex jardleex changed the title from ansible 2.0: with_items variable resolve issue when using with statix suffix to ansible 2.0: with_items: "{{ variable }}_fixstring" resolve issue Jan 21, 2016

@ktaragorn

This comment has been minimized.

Contributor

ktaragorn commented Jan 22, 2016

@jardleex This looks like a duplicate of #13584, take a look if this is so and if the solution there works for you.

@abadger

This comment has been minimized.

Member

abadger commented Jan 22, 2016

Yeah, closing as a duplicate of #13584

@abadger abadger closed this Jan 22, 2016

@jardleex

This comment has been minimized.

jardleex commented Jan 26, 2016

Hi,
thanks for the link. It took me some more days to find a working solution on this:

File to test
withitems.yml
---
- hosts: localhost
  vars:
    role_name: nameofrole
    nameofrole_anystring:
      - { name: foo }
      - { name: bar }
  tasks:
  - name: debug01
    debug: msg={{ role_name }}

  - name: debug02
    debug: msg="{{ role_name }}_anystring"
    with_items: nameofrole_anystring

  - name: debug03
    debug: msg={{ item.name }}
    with_items: nameofrole_anystring

  - name: debug04
    debug: msg={{ item }}
    with_items: "{{ role_name }}_anystring"

  - name: debug05
    debug: msg={{ item.name }}
    with_items:
      - { name: foo }

  - name: debug06
    debug: msg={{ item }}
    with_items: "{{ role_name + '_anystring'}}"

  - name: debug07
    debug: msg={{ item.name }}
    with_items: "{{ vars [role_name + '_anystring'] }}"

Command output
ansible-playbook withitems.yml

PLAY ***************************************************************************

TASK [debug01] *****************************************************************
ok: [localhost] => {
    "msg": "nameofrole"
}

TASK [debug02] *****************************************************************
ok: [localhost] => (item={u'name': u'foo'}) => {
    "item": {
        "name": "foo"
    },
    "msg": "nameofrole_anystring"
}
ok: [localhost] => (item={u'name': u'bar'}) => {
    "item": {
        "name": "bar"
    },
    "msg": "nameofrole_anystring"
}

TASK [debug03] *****************************************************************
ok: [localhost] => (item={u'name': u'foo'}) => {
    "item": {
        "name": "foo"
    },
    "msg": "foo"
}
ok: [localhost] => (item={u'name': u'bar'}) => {
    "item": {
        "name": "bar"
    },
    "msg": "bar"
}

TASK [debug04] *****************************************************************
ok: [localhost] => (item=nameofrole_anystring) => {
    "item": "nameofrole_anystring",
    "msg": "nameofrole_anystring"
}

TASK [debug05] *****************************************************************
ok: [localhost] => (item={u'name': u'foo'}) => {
    "item": {
        "name": "foo"
    },
    "msg": "foo"
}

TASK [debug06] *****************************************************************
ok: [localhost] => (item=nameofrole_anystring) => {
    "item": "nameofrole_anystring",
    "msg": "nameofrole_anystring"
}

TASK [debug07] *****************************************************************
ok: [localhost] => (item={u'name': u'foo'}) => {
    "item": {
        "name": "foo"
    },
    "msg": "foo"
}
ok: [localhost] => (item={u'name': u'bar'}) => {
    "item": {
        "name": "bar"
    },
    "msg": "bar"
}

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

Especially take a look at debug07 how to access attributes of keys.
It would be nice if this to find this in to documentation somehow. There i only found the "{{ name_of_my_var }}" syntax without additional strings or how to acces attributes on the variable.

@jardleex jardleex changed the title from ansible 2.0: with_items: "{{ variable }}_fixstring" resolve issue to SOLVED: ansible 2.0: with_items: "{{ variable }}_fixstring" resolve issue Jan 26, 2016

@pkelleratwork

This comment has been minimized.

pkelleratwork commented Oct 18, 2018

this helped a lot - thanks. i did a similar thing using your example. i have a file with different groups i want to use. set it up like this -

  vars:
     SUBDOMAIN: '{{ LIST }}'
...
  - name: Run yada.sh
    command: yada.sh
    with_items: "{{ vars [SUBDOMAIN] }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment