gpt4 book ai didi

ansible - 我可以更新主机 list 并在同一剧本中使用新主机吗?

转载 作者:行者123 更新时间:2023-12-03 21:35:24 25 4
gpt4 key购买 nike

我通过剧本在主机 list 文件中添加了几个主机。现在我在同一个剧本中使用那些新添加的主机。但是那些新添加的主机似乎无法在同一次运行中被同一个剧本读取,因为我得到 -

skipping: no hosts matched

当我单独运行它时,即我通过一个剧本更新主机文件并通过另一个剧本使用其中更新的主机,它工作正常。

最佳答案

我最近想做这样的事情,使用 ansible 1.8.4。我发现 add_host需要使用组名,否则将跳过播放并显示“没有主机匹配”。同时,我希望第二场比赛使用在第一场比赛中发现的事实。变量和事实通常保持在每个主机的范围内,因此这需要使用魔术变量 hostvarsgroups .

这是我想出的。它有效,但有点难看。我很想看到一个更清洁的替代品。

# test.yml
#
# The name of the active CFN stack is provided on the command line,
# or is set in the environment variable AWS_STACK_NAME.
# Any host in the active CFN stack can tell us what we need to know.
# In real life the selection is random.
# For a simpler demo, just use the first one.
- hosts:
tag_aws_cloudformation_stack-name_{{ stack
|default(lookup('env','AWS_STACK_NAME')) }}[0]
gather_facts: no
tasks:
# Get some facts about the instance.
- action: ec2_facts
# In real life we might have more facts from various sources.
- set_fact: fubar='baz'
# This could be any hostname.
- set_fact: hostname_next='localhost'

# It's too late for variables set in this play to affect host matching
# in the next play, but we can add a new host to temporary inventory.
# Use a well-known group name, so we can set the hosts for the next play.
# It shouldn't matter if another playbook uses the same name,
# because this entry is exclusive to the running playbook.
- name: add new hostname to temporary inventory
connection: local
add_host: group=temp_inventory name='{{ hostname_next }}'

# Now proceed with the real work on the designated host.
- hosts: temp_inventory
gather_facts: no

tasks:
# The host has changed, so the facts from play #1 are out of scope.
# We can still get to them through hostvars, but it isn't easy.
# In real life we don't know which host ran play #1,
# so we have to check all of them.
- set_fact:
stack='{{ stack|default(lookup("env","AWS_STACK_NAME")) }}'
- set_fact:
group_name='{{ "tag_aws_cloudformation_stack-name_" + stack }}'
- set_fact:
fubar='{% for h in groups[group_name] %} {{
hostvars[h]["fubar"]|default("") }} {% endfor %}'
- set_fact:
instance_id='{% for h in groups[group_name] %} {{
hostvars[h]["ansible_ec2_instance_id"]|default("") }} {% endfor %}'
# Trim extra leading and trailing whitespace.
- set_fact: fubar='{{ fubar|replace(" ", "") }}'
- set_fact: instance_id='{{ instance_id|replace(" ", "") }}'

# Now we can use the variables instance_id and fubar.
- debug: var='{{ fubar }}'
- debug: var='{{ instance_id }}'

# end

关于ansible - 我可以更新主机 list 并在同一剧本中使用新主机吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24215187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com