Ansible 在循环中注册多个变量
我有一个变量yaml文件:Ansible 在循环中注册多个变量,ansible,Ansible,我有一个变量yaml文件: --- apps: - client - node - gateway - ic - consul 这项任务是: - name: register if apps exists stat: path="/etc/init.d/{{ item }}" with_items: apps register: "{{ item.stat.exists }}exists" 无论文件是否存在,我都需要为每个应用程序提供一个值为true或fals
---
apps:
- client
- node
- gateway
- ic
- consul
这项任务是:
- name: register if apps exists
stat: path="/etc/init.d/{{ item }}"
with_items: apps
register: "{{ item.stat.exists }}exists"
无论文件是否存在,我都需要为每个应用程序提供一个值为true或false的变量:
clientexists = true
nodeexists = true
gatewayexists = false
icexists = true
consulexists = false
由于某种原因,项
和存在
concat不工作
我怎样才能做到这一点呢?试试这个,希望这能帮到你。在
Stats.yml
中循环时,msg
字段将包含所需的输出
Variables.yml这里我们定义变量
---
apps:
- client
- node
- gateway
- ic
- consul
Stats.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}}"
...
文件夹\u 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
我以后如何使用
文件夹\u stats
作为变量?我需要看看每个应用程序是否存在,我该怎么做?@Moshe在这个剧本中,你可以像这样使用它:-“{{folder_stats.results}”进行循环,而循环时你会在循环中得到这样的结果ok:,。。。“更改”:false,“调用”:{“模块参数”:{“校验和算法”:“sha1”,“follow”:false,“获取校验和”:true,“获取md5”:true,“mime”:false,“路径”:“/etc/init.d/concur”},“模块名称”:“stat”},item:“concur”,“stat”:{“exists”:false},msg:“/etc/init.d/concur:false“}
和msg
字段将包含您想要的输出。您可以查看它,但我需要能够单独访问每个应用程序,而不是使用循环。我需要能够在不同的地方访问节点存在
,我不能每次都循环。