linux - 来自另一个任务输出的 ansible set fact
问题描述
我遇到了一些 ansible 模块。我编写了自定义模块及其输出,如下所示:
ok: [localhost] => {
"msg": {
"ansible_facts": {
"device_id": "/dev/sdd"
},
"changed": true,
"failed": false
}
}
我的自定义模块:
#!bin/env python
from ansible.module_utils.basic import *
import json
import array
import re
def find_uuid():
with open("/etc/ansible/roles/addDisk/library/debug/disk_fact.json") as disk_fact_file, open("/etc/ansible/roles/addDisk/library/debug/device_links.json") as device_links_file:
disk_fact_data = json.load(disk_fact_file)
device_links_data = json.load(device_links_file)
device = []
for p in disk_fact_data['guest_disk_facts']:
if disk_fact_data['guest_disk_facts'][p]['controller_key'] == 1000 :
if disk_fact_data['guest_disk_facts'][p]['unit_number'] == 3:
uuid = disk_fact_data['guest_disk_facts'][p]['backing_uuid'].split('-')[4]
for key, value in device_links_data['ansible_facts']['ansible_device_links']['ids'].items():
for d in device_links_data['ansible_facts']['ansible_device_links']['ids'][key]:
if uuid in d:
if key not in device:
device.append(key)
if len(device) == 1:
json_data={
"device_id": "/dev/" + device[0]
}
return True, json_data
else:
return False
check, jsonData = find_uuid()
def main():
module = AnsibleModule(argument_spec={})
if check:
module.exit_json(changed=True, ansible_facts=jsonData)
else:
module.fail_json(msg="error find device")
main()
我想在其他任务上使用 device_id 变量。我想用 module.exit_json 方法处理,但我该怎么做呢?
解决方案
我想在其他任务上使用 device_id 变量
您正在寻找的东西是register:
为了使该值持续存在于运行该任务的主机的“主机事实”中。然后,您可以使用“推送”模型,在该模型中将这一事实设置在您感兴趣的每个其他主机上,或者您可以使用“拉”模型,其中感兴趣的主机可以在他们需要的时候伸出手来获取价值.
让我们看看这两种情况,以进行比较。
首先,获取该值,为了便于讨论,我将使用一个名为“alpha”的主机:
- hosts: alpha
tasks:
- name: find the uuid task
# or whatever you have called your custom task
find_uuid:
register: the_uuid_result
现在输出可用主机“alpha”上{{ vars["the_uuid_result"]["device_id"] }}
可用/dev/sdd
,如上面的示例所示。也可以缩写为{{ the_uuid_result.device_id }}
在“推送”模型中,您现在可以遍历所有主机,或者只遍历特定组中的主机,这些主机也应该收到该device_id
事实;对于这个例子,让我们定位一个名为“need_device_id”的现有主机组:
- hosts: alpha # just as before, but for context ...
tasks:
- find_uuid:
register: the_uuid_result
# now propagate out the value
- name: declare device_id fact on other hosts
set_fact:
device_id: '{{ the_uuid_result.device_id }}'
delegate_to: '{{ item }}'
with_items: '{{ groups["need_device_id"] }}'
最后,相比之下,如果主机“beta”需要查找device_id
主机“alpha”发现的主机,则可以伸手去拿这个事实:
- hosts: alpha
# as before
- hosts: beta
tasks:
- name: set the device_id fact on myself from alpha
set_fact:
device_id: '{{ hostvars["alpha"]["the_uuid_result"]["device_id"] }}'
您还可以在“alpha”上运行相同set_fact: device_id:
的业务,以防止命名的“本地”变量the_uuid_result
从 alpha 的剧本中泄漏出来。由你决定。
推荐阅读
- r - 使用 dplyr 在特定列值的第一个实例之后过滤 R 中数据框中的行
- assembly - 我怎样才能画出一个直立的星号三角形?
- c# - 如何检测用户何时从 Visual Studio 运行或何时是常规运行服务
- ubuntu - pandoc:如何设置几何驱动程序
- html - 悬停时如何重置/控制转换延迟?
- java - 如何仅捕获可见通知?安卓
- angular - Angular Reactive Forms Select Option 预选默认值不适用于对象
- jquery - Jquery 按 id 汇总数组
- excel - VBA 扩展数据透视表行不起作用。获取错误无法获取 PivotItem 类的 ShowDetail 属性
- javascript - 按图像而不是按钮 HTML