Bug in include_vars or jinja2

190 views
Skip to first unread message

DomaNitro

unread,
Feb 8, 2016, 4:15:03 AM2/8/16
to Ansible Project
For the last month I have been trying convert my v1 action plugin to v2 with no luck.

So Brian suggested to use default include_vars with a loop. So decided to try to use ansible built in modules. but the same issue came up that I had in my custom module.

test.yml
---
- name: Test include_vars template 
  hosts: all
  connection: local
  gather_facts: False
  vars:
    common_var: "my var is defined"
  tasks:
    - name: Debug common_var
      debug: var=common_var
    - name: Include var1.yml
      include_vars: "{{ item }}"
      with_items:
        - "var1.yml"
      register: foo_result
    - name: Debug results
      debug: 
        var=foo_result.results
    - name: Compile a list 
      set_fact: 
        compiled_list="{{ foo_result.results | map(attribute='ansible_facts') | list }}"
    - name: debug compile list
      debug: var=compiled_list

 var1.yml
---
var1:
   mysyste: "1"
   my_var: "{{ common_var }}"


to run 
ansible-playbook -i 127.0.0.1, test.yml



The results
TASK [Debug common_var] ********************************************************
ok: [127.0.0.1] => {
    "common_var": "my var is defined"
}
TASK [Debug results] ***********************************************************
ok: [127.0.0.1] => {
    "foo_result.results": [
        {
            "_ansible_no_log": false,
            "ansible_facts": {
                "var1": {
                    "my_var": "{# common_var #}",
                    "mysyste": "1"
                }
            },
            "invocation": {
                "module_args": {
                    "_raw_params": "var1.yml"
                },
                "module_name": "include_vars"
            },
            "item": "var1.yml"
        }
    ]
}

TASK [debug compile list]
ok: [127.0.0.1] => {
    "compiled_list": [
        {
            "var1": {
                "my_var": "{# common_var #}",
                "mysyste": "1"
            }
        }
    ]
}

 Ansible fails to substitute variable in V2. the above code works with V1, So second level substitution does not work. The variable appears as a Jinja2 comment"{# common_var #}" instead of "my var is defined"

Regards


Brian Coca

unread,
Feb 8, 2016, 3:56:18 PM2/8/16
to Ansible Project
I'm not sure that what you are doing is right, no need to access
through facts, which would prevent it from templating (security
issue).
This example shows that the variable substitution DOES work, as long
as you access it directly.

vars.yml
composed_var: "{{test1}}"

play:
---
- hosts: localhost
gather_facts: false
vars:
test1: [1,2,3]
tasks:

- include_vars: "{{item}}"
with_items:
- vars.yml

- debug: var=composed_var


output:
_____________________
< TASK [include_vars] >
---------------------
ok: [localhost] => (item=vars.yml)
______________
< TASK [debug] >
--------------
ok: [localhost] => {
"composed_var": [
1,
2,
3
]
}



--
Brian Coca
Message has been deleted
Message has been deleted

DomaNitro

unread,
Feb 8, 2016, 5:02:51 PM2/8/16
to Ansible Project

Thank you for your reply.

The objective is to compile a list of variables from files in a directory without using the merge behaviour. In my example I use with_items but it should be with_fileglob

var1.yml
  myvar: "example1"

var2.yml
  myvar: "example2"

var3.yml
  myvar: "example3"

compiled_list:
  - myvar: "example1"
  - myvar: "example2"
  - myvar: "example3"

Since the templating does not happen facts any more. And I dont have the option to enable merge behaviour.
Any other alternative ?
Reply all
Reply to author
Forward
0 new messages