Reputation: 165
UPD: (As advised @vladimir-botka)
I have a problem with checking NOT existing folder using dictonary in Ansible.
---
- name: "check folder"
hosts: "myserver"
gather_facts: yes
remote_user: root
vars:
git_repos:
repo1:
git_repo_folder: /var/www/folder1
repo2:
git_repo_folder: /var/www/folder2
tasks:
- name: "1. Check that the folder exists"
stat:
path: "{{ item.value.git_repo_folder }}"
loop: "{{ lookup('dict', git_repos) }}"
register: check_folder
- name: "2. debug"
debug:
msg: '{{item.item.item.value.git_repo_folder}} exists'
with_items: "{{ check_folder.results }}"
when: not item.stat.exists
Then, if I trying to run playbook, I got the error:
FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'item'\n\nThe error appears to have been in '/home/reset/ansible-prod/SSL-GENERATOR/TEST.yml': line 22, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: \"2. debug\"\n ^ here\n"}
UPD2:
I've tried to debug it:
- debug: var=check_folder and got:
ok: [myserver] => {
"check_folder": {
"changed": false,
"msg": "All items completed",
"results": [
{
"_ansible_ignore_errors": null,
"_ansible_item_label": {
"key": "repo1",
"value": {
"git_repo_folder": "/var/www/folder1"
}
},
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": false,
"failed": false,
"invocation": {
"module_args": {
"checksum_algorithm": "sha1",
"follow": false,
"get_attributes": true,
"get_checksum": true,
"get_md5": null,
"get_mime": true,
"path": "/var/www/folder1"
}
},
"item": {
"key": "repo1",
"value": {
"git_repo_folder": "/var/www/folder1"
}
},
"stat": {
"exists": false
}
},
{
"_ansible_ignore_errors": null,
"_ansible_item_label": {
"key": "repo2",
"value": {
"git_repo_folder": "/var/www/folder2"
}
},
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": false,
"failed": false,
"invocation": {
"module_args": {
"checksum_algorithm": "sha1",
"follow": false,
"get_attributes": true,
"get_checksum": true,
"get_md5": null,
"get_mime": true,
"path": "/var/www/folder2"
}
},
"item": {
"key": "repo2",
"value": {
"git_repo_folder": "/var/www/folder2"
}
},
"stat": {
"exists": false
}
}
]
}
}
Where I am wrong? Is it possible to use "not stat.exists" with list of variables?
Thanks for answer! Thanks for answer! Thanks for answer! Thanks for answer! Thanks for answer!
Upvotes: 0
Views: 3611
Reputation: 68144
Let's test non-existence, because the folders do not exist. The task below
- name: "2. debug"
debug:
msg: "{{ item.item.value.git_repo_folder }} does not exist"
with_items: "{{ check_folder.results }}"
when: not item.stat.exists
gives:
"msg": "/var/www/folder1 does not exist"
"msg": "/var/www/folder2 does not exist"
Upvotes: 2