我有一个可变的 yaml 文件:
---
apps:
- client
- node
- gateway
- ic
- consul
Run Code Online (Sandbox Code Playgroud)
和这个任务:
- name: register if apps exists
stat: path="/etc/init.d/{{ item }}"
with_items: apps
register: "{{ item.stat.exists }}exists"
Run Code Online (Sandbox Code Playgroud)
无论文件是否存在,我都需要为每个应用程序创建一个值为 true 或 false 的变量:
clientexists = true
nodeexists = true
gatewayexists = false
icexists = true
consulexists = false
Run Code Online (Sandbox Code Playgroud)
出于某种原因,itemand existsconcat 不起作用。
我怎样才能做到这一点??
试试这个希望这会帮助你。循环时,Stats.yml则msg字段将包含您想要的输出。
变量.yml Here we are defining variables
---
apps:
- client
- node
- gateway
- ic
- consul
Run Code Online (Sandbox Code Playgroud)
统计.yml
---
- hosts: localhost
name: Gathering facts
vars_files:
- /path/to/variables.yml
tasks:
- name: "Here we are printing variables"
debug:
msg: "{{apps}}"
- name: "Here we are gathering stats and registering it"
stat:
path: "/etc/init.d/{{item}}"
register: folder_stats
with_items:
- "{{apps}}"
- name: "Looping over registered variables, Its msg field will contain desired output"
debug:
msg: "{{item.invocation.module_args.path}}: {{item.stat.exists}}"
with_items:
- "{{folder_stats.results}}"
...
Run Code Online (Sandbox Code Playgroud)
folder_stats 将包含整个结果,用于单独引用单个结果您可以在您的剧本中像这样使用它。
folder_stats.results[0].stat.exists
folder_stats.results[1].stat.exists
folder_stats.results[2].stat.exists
folder_stats.results[3].stat.exists
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3735 次 |
| 最近记录: |