python - 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.
解决方案
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 }}"
推荐阅读
- opencv - 树莓派 3B OpenCV 3.4.1 空屏/帧
- java - 没有通配符或 SAN 证书的站点调用 HostNameVerifier
- angularjs - 获取日期时出现错误数据 || 弹簧/AngularJS
- javascript - Java Script 中的正则表达式:匹配 1-3 个字符后的空格
- ios - iOS - 在屏幕边缘检测 panGesture 以打开侧面菜单 + UICollectionView 与水平滚动
- jquery - 第二个列表项应该被选中
- shiny - 部署我的 APP 不断请求 ggplot2
- javascript - 调整不同主题的文字颜色
- android - Butterknife 抛出 java.lang.IllegalStateException 因为找不到视图(现在不存在)
- kubernetes - 如何以及在何处将参数路径放置到调用 URL