G
G
gremlintv22019-03-13 11:15:13
Ansible
gremlintv2, 2019-03-13 11:15:13

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

If I try to run the playbook I get an error:
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

Tried to debug:
- 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
                }
            }
        ]
    }
}

Where am I wrong? Is it possible to use not stat.existswith a list of variables?
Thanks for the answer!
UPD: or tell me how to skip (skip) task if directory exists using dict

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Puma Thailand, 2019-03-13
@opium

About creating variables, an excellent Zheka in ansible

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question