Answer the question
In order to leave comments, you need to log in
ANSIBLE How to check the existence of a directory through when using a dictionary of variables?
Hi
I have a problem with checking for a NOT existing folder using a dictionary 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
FAILED! => {"failed": true, "msg": "The conditional check 'check_file_id.stat.exists == True' failed. The error was: error while evaluating conditional (check_file_id.stat.exists == True): 'dict' object has no attribute 'stat'\n\n
- debug: var=check_folder:
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
}
}
]
}
}
not stat.exists
with a list of variables? Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question