Re: [ansible-project] Error with jinja2 templating for a conditional

2018-09-18 Thread Matt Martz
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:

https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#using-register-with-a-loop


On Tue, Sep 18, 2018 at 4:59 PM Chris  wrote:

> 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
>
> --
> 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-project+unsubscr...@googlegroups.com.
> To post to this group, send email to ansible-project@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

-- 
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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAD8N0v8KHKThMg1PfkB9%3D6t6EZQ5MdzA0nrOePbyQYzh-JqyrA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Error with jinja2 templating for a conditional

2018-09-18 Thread Chris
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

-- 
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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@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.