How do I access "results" when original ansible task uses with_subelements - debugging

I am trying to generate a list of "views" and the "domains" in each view.
i.e.:
internal/baseserver1
internal/baseserver2
external/baseserver1
...
For each of the above, I want to check for the existence of a "regular" file on the remote host under a known directory inserting the above values into the list of files to be checked. I am attempting to check the value of isreg for each view/host in the above list.
roles/default/main.yml
---
zone_dir: /var/named
zone_domains:
- domain:
name: "jlhimpel.net"
hostmaster_email: "john"
views:
- view:
name: internal
hosts:
- host:
name: baseserver1
ip: 192.168.1.20
- host:
name: baseserver2
ip: 192.168.1.30
- view:
name: external
hosts:
- host:
name: baseserver1
ip: 343.22.56.237
...
roles/tasks/main.yml
---
- name: Debug fqn zone path
stat:
path: /var/named/internal/jlhimpel.net.zone
register: f
become: true
- name: Display f
debug:
var: f
- name: Display f.stat.isreg
debug:
var: f.stat.isreg
- name: Debug symbolic zone path
stat:
path: "{{ zone_dir }}/{{ item[1].name }}/{{ item[0].name }}.zone"
loop: "{{ zone_domains|subelements('views') }}"
register: p
become: true
- name: Display p
debug:
var: p
- name: Display p.results.stat.isreg
debug:
var: item.isreg
with_items:
p.results.stat
...
Output:
PLAY [dnsServiceGroup] *********************************************************
TASK [Gathering Facts] *********************************************************
ok: [testFedora.jlhimpel.net]
TASK [pocDnsService : Debug fqn zone path] *************************************
ok: [testFedora.jlhimpel.net]
TASK [pocDnsService : Display f] ***********************************************
ok: [testFedora.jlhimpel.net] => {
"f": {
"changed": false,
"failed": false,
"stat": {
"atime": 1582308757.2357316,
"attr_flags": "",
"attributes": [],
"block_size": 4096,
"blocks": 8,
"charset": "us-ascii",
"checksum": "b8e6ecf9a617314ed1e040b4cd72dbde3dc9753c",
"ctime": 1581784999.833953,
"dev": 64768,
"device_type": 0,
"executable": false,
"exists": true,
"gid": 25,
"gr_name": "named",
"inode": 18788348,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mimetype": "text/plain",
"mode": "0640",
"mtime": 1581784999.1509356,
"nlink": 1,
"path": "/var/named/internal/jlhimpel.net.zone",
"pw_name": "root",
"readable": true,
"rgrp": true,
"roth": false,
"rusr": true,
"size": 406,
"uid": 0,
"version": "1161797691",
"wgrp": false,
"woth": false,
"writeable": true,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
}
}
}
TASK [pocDnsService : Display f.stat.isreg] ************************************
ok: [testFedora.jlhimpel.net] => {
"f.stat.isreg": true
}
TASK [pocDnsService : Debug symbolic zone path] ********************************
ok: [testFedora.jlhimpel.net] => (item=[{'domain': None, 'name': 'jlhimpel.net', 'hostmaster_email': 'john', 'views': [{'view': None, 'name': 'internal', 'hosts': [{'host': None, 'name': 'baseserver1', 'ip': '192.168.1.20'}, {'host': None, 'name': 'baseserver2', 'ip': '192.168.1.30'}]}, {'view': None, 'name': 'external', 'hosts': [{'host': None, 'name': 'baseserver1', 'ip': '343.22.56.237'}]}]}, {'view': None, 'name': 'internal', 'hosts': [{'host': None, 'name': 'baseserver1', 'ip': '192.168.1.20'}, {'host': None, 'name': 'baseserver2', 'ip': '192.168.1.30'}]}])
ok: [testFedora.jlhimpel.net] => (item=[{'domain': None, 'name': 'jlhimpel.net', 'hostmaster_email': 'john', 'views': [{'view': None, 'name': 'internal', 'hosts': [{'host': None, 'name': 'baseserver1', 'ip': '192.168.1.20'}, {'host': None, 'name': 'baseserver2', 'ip': '192.168.1.30'}]}, {'view': None, 'name': 'external', 'hosts': [{'host': None, 'name': 'baseserver1', 'ip': '343.22.56.237'}]}]}, {'view': None, 'name': 'external', 'hosts': [{'host': None, 'name': 'baseserver1', 'ip': '343.22.56.237'}]}])
TASK [pocDnsService : Display p] ***********************************************
ok: [testFedora.jlhimpel.net] => {
"p": {
"changed": false,
"msg": "All items completed",
"results": [
{
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"invocation": {
"module_args": {
"checksum_algorithm": "sha1",
"follow": false,
"get_attributes": true,
"get_checksum": true,
"get_md5": false,
"get_mime": true,
"path": "/var/named/internal/jlhimpel.net.zone"
}
},
"item": [
{
"domain": null,
"hostmaster_email": "john",
"name": "jlhimpel.net",
"views": [
{
"hosts": [
{
"host": null,
"ip": "192.168.1.20",
"name": "baseserver1"
},
{
"host": null,
"ip": "192.168.1.30",
"name": "baseserver2"
}
],
"name": "internal",
"view": null
},
{
"hosts": [
{
"host": null,
"ip": "343.22.56.237",
"name": "baseserver1"
}
],
"name": "external",
"view": null
}
]
},
{
"hosts": [
{
"host": null,
"ip": "192.168.1.20",
"name": "baseserver1"
},
{
"host": null,
"ip": "192.168.1.30",
"name": "baseserver2"
}
],
"name": "internal",
"view": null
}
],
"stat": {
"atime": 1582308757.2357316,
"attr_flags": "",
"attributes": [],
"block_size": 4096,
"blocks": 8,
"charset": "us-ascii",
"checksum": "b8e6ecf9a617314ed1e040b4cd72dbde3dc9753c",
"ctime": 1581784999.833953,
"dev": 64768,
"device_type": 0,
"executable": false,
"exists": true,
"gid": 25,
"gr_name": "named",
"inode": 18788348,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mimetype": "text/plain",
"mode": "0640",
"mtime": 1581784999.1509356,
"nlink": 1,
"path": "/var/named/internal/jlhimpel.net.zone",
"pw_name": "root",
"readable": true,
"rgrp": true,
"roth": false,
"rusr": true,
"size": 406,
"uid": 0,
"version": "1161797691",
"wgrp": false,
"woth": false,
"writeable": true,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
}
},
{
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"invocation": {
"module_args": {
"checksum_algorithm": "sha1",
"follow": false,
"get_attributes": true,
"get_checksum": true,
"get_md5": false,
"get_mime": true,
"path": "/var/named/external/jlhimpel.net.zone"
}
},
"item": [
{
"domain": null,
"hostmaster_email": "john",
"name": "jlhimpel.net",
"views": [
{
"hosts": [
{
"host": null,
"ip": "192.168.1.20",
"name": "baseserver1"
},
{
"host": null,
"ip": "192.168.1.30",
"name": "baseserver2"
}
],
"name": "internal",
"view": null
},
{
"hosts": [
{
"host": null,
"ip": "343.22.56.237",
"name": "baseserver1"
}
],
"name": "external",
"view": null
}
]
},
{
"hosts": [
{
"host": null,
"ip": "343.22.56.237",
"name": "baseserver1"
}
],
"name": "external",
"view": null
}
],
"stat": {
"atime": 1582326121.0318115,
"attr_flags": "",
"attributes": [],
"block_size": 4096,
"blocks": 8,
"charset": "us-ascii",
"checksum": "5deeb57dfd80c5b264ab5ebff8af728b2a551860",
"ctime": 1582079607.5288289,
"dev": 64768,
"device_type": 0,
"executable": false,
"exists": true,
"gid": 0,
"gr_name": "root",
"inode": 25378368,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mimetype": "text/plain",
"mode": "0640",
"mtime": 1582079607.4818277,
"nlink": 1,
"path": "/var/named/external/jlhimpel.net.zone",
"pw_name": "root",
"readable": true,
"rgrp": true,
"roth": false,
"rusr": true,
"size": 406,
"uid": 0,
"version": "2850301314",
"wgrp": false,
"woth": false,
"writeable": true,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
}
}
]
}
}
TASK [pocDnsService : Display f.stat[].isreg] **********************************
ok: [testFedora.jlhimpel.net] => (item=p.results.stat) => {
"ansible_loop_var": "item",
"item": "p.results.stat",
"item.isreg": "VARIABLE IS NOT DEFINED!"
}
Any advise would be greatly appreciated.
Sorry for pasting such verbose output.

The task below
- name: Display p.results.stat.isreg
debug:
msg: "{{ item }}"
loop: "{{ p.results|json_query('[].stat.isreg') }}"
gives
"msg": true
"msg": true

Related

How to get all indices of output array in Ansible

I'm gathering info on SSL certs on servers (looking for expiration date) using the find module.
- name: Find certs on server
find:
path: /etc/ssl/custom/certs
file_type: file
patterns: "*.crt"
recurse: yes
register: find_result
- debug:
var: find_result
The results are:
ok: [server00] => {
"find_result": {
"changed": false,
"examined": 5,
"failed": false,
"files": [
{
"atime": 1622749788.1552677,
"ctime": 1622744497.4393551,
"dev": 2050,
"gid": 0,
"gr_name": "root",
"inode": 19531534,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0644",
"mtime": 1622744497.4393551,
"nlink": 1,
"path": "/etc/ssl/custom/certs/somewebsite0.com.crt",
"pw_name": "root",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 1879,
"uid": 0,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
},
{
"atime": 1622719627.2477663,
"ctime": 1616545902.3681087,
"dev": 2050,
"gid": 0,
"gr_name": "root",
"inode": 19531253,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0644",
"mtime": 1613754568.0,
"nlink": 1,
"path": "/etc/ssl/custom/certs/somewebsite1.com.crt",
"pw_name": "root",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 2081,
"uid": 0,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
},
{
"atime": 1622719627.2197664,
"ctime": 1616545902.3721087,
"dev": 2050,
"gid": 0,
"gr_name": "root",
"inode": 19535012,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0644",
"mtime": 1601653231.0,
"nlink": 1,
"path": "/etc/ssl/custom/certs/somewebsite2.com.crt",
"pw_name": "root",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 2269,
"uid": 0,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
}
],
"matched": 3,
"msg": ""
}
}
I'm needing the path portion of the output ("path": "/etc/ssl/custom/certs/somewebsite1.com.crt"), and if I use find_result.files[0].path it only gives me a single result for each host, when I need every *.crt file.
How can I access each index? I try to use the shell module to perform an action on the .crt file, but again, it's only grabbing the first one due to the [0] index, like so:
- name: Check expiration
shell: "cat {{ find_result.files[0].path }} | openssl x509 -noout -enddate"
register: date
- debug:
var: date.stdout_lines
ok: [server00] => {
"date.stdout_lines": [
"notAfter=Apr 2 19:50:38 2018 GMT"
]
}
Here would be an example playbook based on it:
- hosts: localhost
tasks:
- name: Find certs on server
find:
path: /etc/ssl/custom/certs
file_type: file
patterns: "*.crt"
recurse: yes
register: find_result
- debug:
var: find_result
- name: Play with the data just to demonstrate
set_fact:
IRGeekSauce_list: "{{ (IRGeekSauce_list|default([])) + [item.path] }}"# <-- add each list item to a custom list
with_items: '{{ find_result.files }}' # <-- here we get the files as a list.
- name: your list
debug:
msg: '{{ IRGeekSauce_list }}'
- include_tasks: anothertasklist.yml
loop: '{{ IRGeekSauce_list }}'
loop_control:
loop_var: singlepathvariable
And then you have another "playbook" with just the tasks 'anothertasklist.yml'
- name: hello
debug:
msg: 'You are now in another playbook'
- name:
debug:
msg: 'Woho: {{ singlepathvariable }}'
- name:
openssl_certificate_info:
howeverthatmoduleworks...
And you should be able to just take the entire and include find_result.files as the loop, and then instead just use the loopvar singlepathvariable(and maybe rename it) and just take out the path as {{ singlepathvariable.path }}

How to grab filepath from ansibles find module

I'm using Ansible for some IAC(infra as code) tasks.
I have a playbook where I'm using the find module recursively to search for readable files.
Here is an example of it:
- name: Application logs with read access
become: true
find:
paths: /
file_type: file
recurse: yes
patterns:
- '*.log'
- '*.config'
register: rapplogs
- set_fact: read_app_logs={{rapplogs.matched}}
- debug: var=read_app_logs
- set_fact: read_log_list={{rapplogs.files}}
- debug: var=read_log_list
run_once: True
failed_when: read_app_logs >= 1
ignore_errors: True
The output of it is like this:
TASK [infra_pt : set_fact] ******************************************************************
ok: [192.168.47.135]
TASK [infra_pt : debug] *********************************************************************
ok: [192.168.47.135] => {
"read_app_logs": "72"
}
TASK [infra_pt : set_fact] ******************************************************************
ok: [192.168.47.135]
TASK [infra_pt : debug] *********************************************************************
fatal: [192.168.47.135]: FAILED! => {
"failed_when_result": true,
"read_log_list": {
"changed": false,
"examined": 210060,
"failed": false,
"files": [
{
"atime": 1558446815.3474104,
"ctime": 1558446815.3474104,
"dev": 64768,
"gid": 0,
"gr_name": "root",
"inode": 2065610,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0644",
"mtime": 1558446815.3474104,
"nlink": 1,
"path": "/test2.log",
"pw_name": "root",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 0,
"uid": 0,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
},
From the output list I actually want to access only the "mode" and "path" objects, how this can be done? Any idea?
Try json_query
- set_fact:
read_app_logs: "{{ rapplogs.files|json_query('[].{path: path, mode: mode}') }}"
(not tested)
Sure. You can just iterate over the list of matched files and refer to whichever keys are of interest:
- debug:
msg: "mode of {{ item.path }} is {{ item.mode }}"
loop: "{{ read_log_list.files }}"
Which, given your example output, would produce something like this:
TASK [debug] **********************************************************************************
ok: [localhost] => (item={u'islnk': False, u'uid': 0, u'rgrp': True, u'xoth': False, u'rusr': True, u'woth': False, u'nlink': 1, u'issock': False, u'mtime': 1558446815.3474104, u'gr_name': u'root', u'path': u'/test2.log', u'xusr': False, u'atime': 1558446815.3474104, u'inode': 2065610, u'isgid': False, u'size': 0, u'isdir': False, u'wgrp': False, u'ctime': 1558446815.3474104, u'isblk': False, u'xgrp': False, u'isuid': False, u'dev': 64768, u'roth': True, u'isreg': True, u'isfifo': False, u'mode': u'0644', u'pw_name': u'root', u'gid': 0, u'ischr': False, u'wusr': True}) => {
"msg": "mode of /test2.log is 0644"
}

Ansible retrieve multiple array values from results

I'd like to be able to iterate over all of the name values, but I'm not sure how to do so with Ansible. The variable domain is a list, and register is used.
- name: find *.ccfg files in domain(s)
find:
paths: "/tmp/opt/{{ item }}/ccfg"
patterns: "*.ccfg"
recurse: yes
excludes: "Admin.ccfg"
with_items: "{{ domain }}"
register: files
when: ('local' in group_names)
- debug:
msg: "{{ files.results }}"
The path value in each array could be anywhere from 1 to 20. Each index in the array has multiple values. Some arrays may not have any values
Standard Output:
ok: [127.0.0.1] => {
"msg": [
{
"_ansible_ignore_errors": null,
"_ansible_item_label": "CIE",
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": false,
"examined": 3,
"failed": false,
"files": [
{
"atime": 1541632866.4095802,
"ctime": 1541632866.4095802,
"dev": 64768,
"gid": 0,
"gr_name": "root",
"inode": 52174935,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0644",
"mtime": 1541632866.4095802,
"nlink": 1,
"path": "/tmp/opt/CIE/ccfg/cie.ccfg",
"pw_name": "root",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 0,
"uid": 0,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
}
],
Take a look at the json_query filter:
- debug:
msg: "{{ item }}"
with_items: "{{ files | json_query('results[*].files[*].path') }}"
Official doco: https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#json-query-filter
Shameless plug with more examples: https://parko.id.au/2018/08/16/complex-data-structures-and-the-ansible-json_query-filter

How do i filter on ansible stat existence flags?

I'm executing a simple stat task (Ansible 2.3.1.0) on the named pipe created by wpa_supplicant:
- stat:
path: "/var/run/wpa_supplicant/{{ item }}"
with_items:
- wifi
register: wpa_stats
sudo: true
The variable contains the following data after execution:
ok: [10.10.23.187] => {
"wpa_stats": {
"changed": false,
"msg": "All items completed",
"results": [
{
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": false,
"invocation": {
"module_args": {
"checksum_algorithm": "sha1",
"follow": false,
"get_attributes": true,
"get_checksum": true,
"get_md5": true,
"get_mime": true,
"path": "/var/run/wpa_supplicant/wifi"
}
},
"item": "wifi",
"stat": {
"atime": 1497900522.6306846,
"attr_flags": "",
"attributes": [],
"block_size": 4096,
"blocks": 0,
"charset": "binary",
"ctime": 1497900290.0605242,
"dev": 18,
"device_type": 0,
"executable": true,
"exists": true,
"gid": 0,
"gr_name": "root",
"inode": 796,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": false,
"issock": true,
"isuid": false,
"mimetype": "inode/socket",
"mode": "0770",
"mtime": 1497900290.0605242,
"nlink": 1,
"path": "/var/run/wpa_supplicant/wifi",
"pw_name": "root",
"readable": true,
"rgrp": true,
"roth": false,
"rusr": true,
"size": 0,
"uid": 0,
"version": null,
"wgrp": true,
"woth": false,
"writeable": true,
"wusr": true,
"xgrp": true,
"xoth": false,
"xusr": true
}
}
]
}
}
But this filter returns an empty result:
- debug:
msg: "{{ wpa_stats | json_query('results[*].stat[?exists].path') | list }}"
If I remove the [?exists] filter it works fine:
- debug:
msg: "{{ wpa_stats | json_query('results[*].stat.path') | list }}"
I've also tried using ==. Jmespath is installed and I'm querying other values with JSON filters successfully.
What am I missing?
I guess you want to use pipe expression:
results[*].stat | [?exists].path
From my understating of JMESPath in stat[?filter] filter is applied inside stat (to select elements that are down the path), but you want to apply filter to select/reject stat siblings, so you should stop further projections with pipe and filter elements.

Ansible stat module not working

I need some help here:
I am creating virtual machines with Vagrant to configure some cluster on them. For the first VM, which I want to use as an Ansible control node, I'm running Ansible from my local machine to install and configure Ansible in my Control Node.
The problem appears when I try to check if the .inventory file exists and try to copy the file to the home directory (inside the VM).
The same command that I use for the inventory file works just file to check the stat of the .ansible.cfg file but not for the inventory file.
Do you guys have any idea what I'm doing wrong?
roles/ansible/tasks/main.yml
---
- name: install epel-release
yum:
name: epel-release
state: present
- name: install ansible
yum:
name: ansible
state: present
- name: stat ansible configuration file
stat:
path: "{{ cfg_file }}"
register: stat_ansible_config
- name: copy .ansible.cfg to home directory
copy:
src: .ansible.cfg
dest: /home/{{ user }}/.ansible.cfg
owner: "{{ user }}"
group: "{{ group }}"
mode: 0644
when: stat_ansible_config.stat.exists
- name: stat ansible inventory file
stat:
path: "{{ inventory_file }}"
register: stat_inventory
- name: copy .inventory to home directory
copy:
src: .inventory
dest: /home/{{ user }}/.inventory
owner: "{{ user }}"
group: "{{ group }}"
mode: 0644
when: stat_inventory.stat.exists
...
roles/ansible/vars/main.yml
---
user: vagrant
group: vagrant
cfg_file: /{{ user }}/provision/playbooks/roles/ansible/files/.ansible.cfg
inventory_file: /{{ user }}/provision/playbooks/roles/ansible/files/.inventory
...
the playbook:
---
- hosts: controller
become: yes
roles:
- ansible
...
and the output:
TASK [ansible : stat ansible configuration file] *******************************
ok: [controller] => {
"changed": false,
"invocation": {
"module_args": {
"checksum_algorithm": "sha1",
"follow": false,
"get_checksum": true,
"get_md5": true,
"mime": false,
"path": "/vagrant/provision/playbooks/roles/ansible/files/.ansible.cfg"
},
"module_name": "stat"
},
"stat": {
"atime": 1485527858.0,
"checksum": "46acc076fda7e38fd7262fbc88f8ab4e1f52ddca",
"ctime": 1485452789.0,
"dev": 38,
"executable": false,
"exists": true,
"gid": 1000,
"gr_name": "vagrant",
"inode": 118,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"md5": "0cb8c97246776dc7e88fe44f19c3278f",
"mode": "0644",
"mtime": 1485452789.0,
"nlink": 1,
"path": "/vagrant/provision/playbooks/roles/ansible/files/.ansible.cfg",
"pw_name": "vagrant",
"readable": true,
"rgrp": true,
"roth": true,
"rusr": true,
"size": 164,
"uid": 1000,
"wgrp": false,
"woth": false,
"writeable": true,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
}
}
TASK [ansible : copy .ansible.cfg to home directory] ***************************
ok: [controller] => {
"changed": false,
"checksum": "46acc076fda7e38fd7262fbc88f8ab4e1f52ddca",
"dest": "/home/vagrant/.ansible.cfg",
"diff": {
"after": {
"path": "/home/vagrant/.ansible.cfg"
},
"before": {
"path": "/home/vagrant/.ansible.cfg"
}
},
"gid": 1000,
"group": "vagrant",
"invocation": {
"module_args": {
"backup": null,
"content": null,
"delimiter": null,
"dest": "/home/vagrant/.ansible.cfg",
"diff_peek": null,
"directory_mode": null,
"follow": false,
"force": false,
"group": "vagrant",
"mode": 420,
"original_basename": ".ansible.cfg",
"owner": "vagrant",
"path": "/home/vagrant/.ansible.cfg",
"recurse": false,
"regexp": null,
"remote_src": null,
"selevel": null,
"serole": null,
"setype": null,
"seuser": null,
"src": ".ansible.cfg",
"state": null,
"unsafe_writes": null,
"validate": null
}
},
"mode": "0644",
"owner": "vagrant",
"path": "/home/vagrant/.ansible.cfg",
"secontext": "unconfined_u:object_r:user_home_t:s0",
"size": 164,
"state": "file",
"uid": 1000
}
TASK [ansible : stat ansible inventory file] ***********************************
ok: [controller] => {
"changed": false,
"invocation": {
"module_args": {
"checksum_algorithm": "sha1",
"follow": false,
"get_checksum": true,
"get_md5": true,
"mime": false,
"path": null
},
"module_name": "stat"
},
"stat": {
"exists": false
}
}
TASK [ansible : copy .inventory to home directory] *****************************
task path: /Users/alessandro/Go/src/github.com/alesr/neo4go/provision/playbooks/roles/ansible/tasks/main.yml:31
skipping: [controller] => {
"changed": false,
"skip_reason": "Conditional check failed",
"skipped": true
}
inventory_file is a magic variable and is set by Ansible during playbook run overwriting any values you try to assign.
In your inventory stat task you may notice: invocation.module_args.path: null.
Rename your inventory_file variable to my_inventory_file and it will work.
copy sees the file is already there and is saying there's no change, but is identical to your source and not making a change.
The debug output shows:
stat_ansible_config.stat.exists = True
stat_inventory.stat.exists = False
and that explains the behavior difference.

Resources