Search code examples
pythonansiblestat

Path to stat module via dict variable


I want to register status of a file with the stat module so that I can set permissions if it exists.

In the following tasks, I send variables to the cron and file modules. What is a good way to make those same variables available to the stat module, or what is an alternate method of checking for the existence of a file based on dict variables?

- name: Task One
  cron:
    name: "{{ item.key }} nightly S3 backup"
    minute: "0"
    hour: "12"
    user: "{{ web_user }}"
    job: "cd {{ www_root }}/{{ item.key }}/{{ item.value.current_path | default('current') }}/scripts && ./backup-to-s3.sh > /dev/null 2>&1"
    cron_file: "backup-nightly-{{ item.key | replace('.', '_') }}"
  with_dict: "{{ my_dictionary }}"

- name: Task Two
  stat:
    path: "{{ www_root }}/{{ item.key }}/{{ item.value.current_path | default('current') }}/scripts/backup-to-s3.sh"
    register: stat_result

- name: Task Three
  file:
    path: "{{ www_root }}/{{ item.key }}/{{ item.value.current_path | default('current') }}/scripts/backup-to-s3.sh"
    owner: "{{ web_user }}"
    group: "{{ web_group }}"
    mode: 0755
  when: stat_result.stat.exists == True
  with_dict: "{{ my_dictionary }}"

my_dictionary:
  example.com:
    site_hosts:
      - canonical: example.com
    local_path: ../example.com
    env:
      db_prefix: my_

I am thinking maybe with_items will be at at least part of the solution.


Solution

  • Q: "Check for the existence of a file based on dict variables."

    A: Short answer: Create a dictionary of the files and their statuses. Use it in the conditions.

    Details

    1) Register stat_result in the loop

    - name: Task Two
      stat:
        path: "{{ www_root }}/{{ item.key }}/{{ item.value.current_path | ...
      register: stat_result
      with_dict: "{{ my_dictionary }}"
    

    2) Create the dictionary

    - set_fact:
        files_stat: "{{ dict(stat_result.results|
                        json_query('[].[item.key, stat.exists]')) }}"
    

    3) Use the dictionary in the condition

    - name: Task Three
      file:
        path: "{{ www_root }}/{{ item.key }}/{{ item.value.current_path | ...
        owner: "{{ web_user }}"
        group: "{{ web_group }}"
        mode: 0755
      with_dict: "{{ my_dictionary }}"
      when: files_stat[item.key]
    


    Example

    - hosts: localhost
    
      vars:
        my_dictionary:
          file1:
            local_path: "find_cpy/file1.ext"
          file2:
            local_path: "find_cpy/file2.ext"
          file3:
            local_path: "find_cpy/file9.ext"
    
      tasks:
        - stat:
            path: "{{ item.value.local_path }}"
          register: stat_result
          with_dict: "{{ my_dictionary }}"
    
        - set_fact:
            files_stat: "{{ dict(stat_result.results|
                            json_query('[].[item.key, stat.exists]')) }}"
        - debug:
            var: files_stat
    
        - file:
            state: file
            mode: "0644"
            path: "{{ item.value.local_path }}"
          with_dict: "{{ my_dictionary }}"
          when: files_stat[item.key]
    

    give

    TASK [debug] ***
    ok: [localhost] => {
        "files_stat": {
            "file1": true, 
            "file2": true, 
            "file3": false
        }
    }
    
    TASK [file] ***
    skipping: [localhost] => (item={'value': {u'local_path': u'find_cpy/file9.ext'}, 'key': u'file3'}) 
    ok: [localhost] => (item={'value': {u'local_path': u'find_cpy/file2.ext'}, 'key': u'file2'})
    ok: [localhost] => (item={'value': {u'local_path': u'find_cpy/file1.ext'}, 'key': u'file1'})
    

    Q: "json_query requires installing jmespath. Would you offer an approach without that requirement?"

    A: The task below creates the same dictionary without json_query.

        - set_fact:
            files_stat: "{{ dict(my_keys|zip(my_stats)) }}"
          vars:
            my_keys: "{{ stat_result.results|map(attribute='item.key')|list }}"
            my_stats: "{{ stat_result.results|map(attribute='stat.exists')|list }}"
    

    See Combining items from multiple lists.