Hi All, i want to validate if certain directories exists or not. The below ansib . . .

Saravanan Jothilingam:
Hi All,
i want to validate if certain directories exists or not. The below ansible script not working. How to validate all directories here?

  • hosts: ‘{{ host }}’
    gather_facts: true
    become: true

    tasks:

    • name: Check for dir
      stat: path= “{{ item }}”
      register: check
      with_items:
      - “/tmp”
      - “/etc”
    • name: Print msg
      debug:
      msg: “Dir not exists”
      when: not check.stat.exists

Al West:
Your code makes 2 checks with with_items but you only check the result once.

Saravanan Jothilingam:
How to get the result for 2nd check?

Saravanan Jothilingam:
TASK [Task name] ***********************************************************************************************************
skipping: [http://xxxxxx01.domain.corp.com|xxxxxx01.domain.corp.com] => (item={‘invocation’: {‘module_args’: {‘checksum_algorithm’: ‘sha1’, ‘get_checksum’: True, ‘follow’: False, ‘path’: ‘’, ‘get_md5’: False, ‘get_mime’: True, ‘get_attributes’: True}}, ‘stat’: {‘exists’: False}, ‘changed’: False, ‘failed’: False, ‘item’: ‘/tmp’, ‘ansible_loop_var’: ‘item’})
skipping: [http://xxxxxx01.domain.corp.com|xxxxxx01.domain.corp.com] => (item={‘invocation’: {‘module_args’: {‘checksum_algorithm’: ‘sha1’, ‘get_checksum’: True, ‘follow’: False, ‘path’: ‘’, ‘get_md5’: False, ‘get_mime’: True, ‘get_attributes’: True}}, ‘stat’: {‘exists’: False}, ‘changed’: False, ‘failed’: False, ‘item’: ‘/etc’, ‘ansible_loop_var’: ‘item’})
skipping: [http://xxxxxx01.domain.corp.com|xxxxxx01.domain.corp.com]
skipping: [http://xxxxxx02.domain.corp.com|xxxxxx02.domain.corp.com] => (item={‘invocation’: {‘module_args’: {‘checksum_algorithm’: ‘sha1’, ‘get_checksum’: True, ‘follow’: False, ‘path’: ‘’, ‘get_md5’: False, ‘get_mime’: True, ‘get_attributes’: True}}, ‘stat’: {‘exists’: False}, ‘changed’: False, ‘failed’: False, ‘item’: ‘/tmp’, ‘ansible_loop_var’: ‘item’})
skipping: [http://xxxxxx02.domain.corp.com|xxxxxx02.domain.corp.com] => (item={‘invocation’: {‘module_args’: {‘checksum_algorithm’: ‘sha1’, ‘get_checksum’: True, ‘follow’: False, ‘path’: ‘’, ‘get_md5’: False, ‘get_mime’: True, ‘get_attributes’: True}}, ‘stat’: {‘exists’: False}, ‘changed’: False, ‘failed’: False, ‘item’: ‘/etc’, ‘ansible_loop_var’: ‘item’})
skipping: [http://xxxxxx02.domain.corp.com|xxxxxx02.domain.corp.com]
skipping: [http://xxxxxx03.domain.corp.com|xxxxxx03.domain.corp.com] => (item={‘invocation’: {‘module_args’: {‘checksum_algorithm’: ‘sha1’, ‘get_checksum’: True, ‘follow’: False, ‘path’: ‘’, ‘get_md5’: False, ‘get_mime’: True, ‘get_attributes’: True}}, ‘stat’: {‘exists’: False}, ‘changed’: False, ‘failed’: False, ‘item’: ‘/tmp’, ‘ansible_loop_var’: ‘item’})
skipping: [http://xxxxxx03.domain.corp.com|xxxxxx03.domain.corp.com] => (item={‘invocation’: {‘module_args’: {‘checksum_algorithm’: ‘sha1’, ‘get_checksum’: True, ‘follow’: False, ‘path’: ‘’, ‘get_md5’: False, ‘get_mime’: True, ‘get_attributes’: True}}, ‘stat’: {‘exists’: False}, ‘changed’: False, ‘failed’: False, ‘item’: ‘/etc’, ‘ansible_loop_var’: ‘item’})
skipping: [http://xxxxxx03.domain.corp.com|xxxxxx03.domain.corp.com]

Al West:
You should get an array back from the register with more than 1 item:

    - name: Print msg
      debug:
        msg: "Dir {{ item.stat.pw_name }} not exists"
      when: not item.stat.exists
      loop: "{{ check.results }}"