通过 dict 变量访问 stat 模块的路径

Path to stat module via dict variable

我想用 stat 模块注册文件的状态,以便我可以设置权限(如果存在)。

在接下来的任务中,我将变量发送到 cron 和文件模块。使这些相同变量可用于 stat 模块的好方法是什么,或者根据 dict 变量检查文件是否存在的替代方法是什么?

- 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_

我想也许 with_items 至少是解决方案的一部分。

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

A:简答:创建文件及其状态的字典。在条件下使用它。

详情

1) 循环注册stat_result

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

2) 创建字典

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

3) 在条件中使用字典

- 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]


范例

- 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]

给予

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:下面的任务创建相同的字典但没有 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 }}"

参见 Combining items from multiple lists