简体   繁体   English

来自另一个任务输出的可疑事实

[英]ansible set fact from another task output

I have trouble some ansible modules. 我遇到了一些麻烦的模块。 I wrote the custom module and its output like this: 我这样编写了自定义模块及其输出:

ok: [localhost] => {
    "msg": {
        "ansible_facts": {
            "device_id": "/dev/sdd"
        },
        "changed": true,
        "failed": false
    }
}

my custom module: 我的自定义模块:

#!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()

I want to use device_id variable on the other tasks. 我想在其他任务上使用device_id变量。 I think handle with module.exit_json method but how can I do that? 我认为使用module.exit_json方法可以处理,但是我该怎么办呢?

I want to use device_id variable on the other tasks 我想在其他任务上使用device_id变量

The thing you are looking for is register: in order to make that value persist into the "host facts" for the hosts against which that task ran. 您正在寻找的是register:为了使该值持久存在于运行该任务的主机的“主机事实”中。 Then, you can go with "push" model in which you set that fact upon every other host that interests you, or you can go with a "pull" model wherein interested hosts can reach out to get the value at the time they need it. 然后,您可以使用“推”模型,在该模型中将事实设置在您感兴趣的其他所有主机上,也可以使用“拉”模型,在该模型中,感兴趣的主机可以在需要时伸出手来获得价值。

Let's look at both cases, for comparison. 让我们看看这两种情况,以进行比较。

First, capture that value, and I'll use a host named "alpha" for ease of discussion: 首先,获取该值,为了方便讨论,我将使用一个名为“ alpha”的主机:

- hosts: alpha
  tasks:
  - name: find the uuid task
    # or whatever you have called your custom task
    find_uuid:
    register: the_uuid_result

Now the output is available is available on the host "alpha" as {{ vars["the_uuid_result"]["device_id"] }} which will be /dev/sdd in your example above. 现在,输出在主机“ alpha”上可用,为{{ vars["the_uuid_result"]["device_id"] }} ,在上面的示例中为/dev/sdd One can also abbreviate that as {{ the_uuid_result.device_id }} 也可以将其缩写为{{ the_uuid_result.device_id }}

In the "push" model, you can now iterate over all hosts, or just those in a specific group, that should also receive that device_id fact; 在“推送”模型中,您现在可以遍历所有主机,也可以仅遍历特定组中的主机,这些主机也应接收该device_id事实; for this example, let's target an existing group of hosts named "need_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"] }}'

And, finally, in contrast, one can reach over and pull that fact if host "beta" needs to look up the device_id that host "alpha" discovered: 最后,相反,如果主机“ beta”需要查找主机“ alpha”发现的device_id ,则可以device_id这一事实:

- 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"] }}'

You could also run that same set_fact: device_id: business on "alpha" in order to keep the "local" variable named the_uuid_result from leaking out of alpha's playbook. 您还可以在“ alpha”上运行相同的set_fact: device_id: business,以防止名为the_uuid_result的“本地”变量从alpha的剧本中泄漏出来。 Up to you. 由你决定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM