gpt4 book ai didi

python - 通过 dict 变量到 stat 模块的路径

转载 作者:行者123 更新时间:2023-12-01 06:20:54 26 4
gpt4 key购买 nike

我想用 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."

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

详情

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?"

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

关于python - 通过 dict 变量到 stat 模块的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60364169/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com