Error with jinja2 templating for a conditional

14 views
Skip to first unread message

Chris

unread,
Sep 18, 2018, 5:59:28 PM9/18/18
to Ansible Project
Hello,

Will someone please help me understand why this is not work?

Thanks!

---
- hosts: all
  become: yes
  become_method: sudo
  become_user: root

  vars:

    stat_files:
      - name: stat_var
        stat_path: "/etc/resolv.conf"

  tasks:
    - name: "Verify file copy destination"
      stat:
        path: "{{ item.stat_path }}"
      register: "{{ item.name }}"
      with_items:
        - "{{ stat_files }}"
    - debug:
        var: "Found it!"
      when: stat_var.stat.exists

--------------------------------------------------------------------------------------------------------------------


k: [hostname] => (item={u'stat_path': u'/etc/resolv.conf', u'name': u'stat_var'}) => {
    "changed": false,
    "item": {
        "name": "stat_var",
        "stat_path": "/etc/resolv.conf"
    },
    "stat": {
        "atime": 1537249786.7223341,
        "attr_flags": "",
        "attributes": [],
        "block_size": 4096,
        "blocks": 8,
        "charset": "us-ascii",
        "checksum": "2f01daec7b7a510f0b791c94ee345845f211be18",
        "ctime": 1536990466.4755569,
        "dev": 64768,
        "device_type": 0,
        "executable": false,
        "exists": true,

--------------------------------------------------------------------------------------------------------------------

MSG:

The conditional check 'stat_var.stat.exists' failed. The error was: error while evaluating conditional (stat_var.stat.exists): 'stat_var' is undefined

Matt Martz

unread,
Sep 18, 2018, 6:57:08 PM9/18/18
to ansible...@googlegroups.com
You cannot use a variable as the name of the var to be registered. It must be an explicit static string.

See the following info on the structure of registered data when using a loop:


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/42e82cf6-54e1-41c2-9743-4fa493149bee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Matt Martz
@sivel
sivel.net
Reply all
Reply to author
Forward
0 new messages