jinja2 templated variables do not expand

23 views
Skip to first unread message

eray

unread,
Oct 18, 2017, 1:28:42 PM10/18/17
to Ansible Project
I am not sure if its my misuse of jinja within ansible, but I am seeing strange behavior from what would seem to be a typical use case for managing dynamic variables. As you can see from the code below, I am using the mysql_os variable to determine the list to select items from.  This may be the intended behavior, but I swear I have done this in the past.  I tested on 2.3, and 2.2 with the same results.

---
  - hosts: localhost
    connection: local
    vars:
      mysql_os: "el6"

      mysql_el6_base:
        - "MySQL-server-{{ mysql_os }}"

      mysql_el7_base:
        - "mysql-community-server-{{ mysql_os }}"
    tasks:
      - debug: msg="{{ item }}.x86_64.rpm"
        with_items:
          - "{{vars['mysql_' + mysql_os + '_base']}}"

Expected Result
TASK [debug] ******************************************************************************************************************
******
ok: [localhost] => (item=MySQL-server-el6) => {
    "item": "MySQL-server-el6",
    "msg": "MySQL-server-el6.x86_64.rpm"
}



Actual Result
TASK [debug] ******************************************************************************************************************
******
ok: [localhost] => (item=MySQL-server-{{ mysql_os }}) => {
    "item": "MySQL-server-{{ mysql_os }}",
    "msg": "MySQL-server-{{ mysql_os }}.x86_64.rpm"
}


Thanks

mcso...@redhat.com

unread,
Oct 19, 2017, 4:19:16 AM10/19/17
to Ansible Project
If I remember correctly, it does not work, when you define a variable and try to use it in same "block" in this case vars:

Both mysql_os and mysql_el6_base are defined in vars, so when expanding mysql_el6_base the mysql_os is not yet defined.

And the solution I used was to separate them into multiple vars_files.
Reply all
Reply to author
Forward
0 new messages