gpt4 book ai didi

amazon-ec2 - ansible get tag_Name 并在多个主机上设置主机名

转载 作者:行者123 更新时间:2023-12-03 13:15:32 25 4
gpt4 key购买 nike

- hosts: ALL
gather_facts: true
remote_user:test
vars:
Env: "{{ env }}"
tasks:
- ec2_remote_facts:
region: us-east-1
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
filters:
"tag:Env": "{{ env }}"
register: instance_facts
- name: group_hosts
add_host: hostname={{ item }} groups=dev
with_items: "{{ instance_facts.instances|map(attribute="private_ip_address)|list }}"
- name: "loop over hosts for hostnames"
hostname:
name: {{ item }}
with_items: "{{ instance_facts.instances|map(attribute=' ')|list }}"

我在此剧本中的目的是获取 tag_Name 并将其设置为与实例中的主机名相同。我正在尝试不同的事情,但在“为主机循环主机”任务中使用标签属性时卡住了。我如何提及 Tag = "Name"以将其保存为主机名,因为它是一个嵌套属性?

最佳答案

你应该把你的剧本分成两个剧本:

  1. 生成内存库存
  2. 在主机上运行任务

这是一个例子:

- hosts: localhost
gather_facts: no
tasks:
- ec2_remote_facts:
region: us-east-1
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
filters:
"tag:Env": "{{ env }}"
register: instance_facts
- add_host:
name: "{{ item.tags.Name }}"
ansible_host: "{{ item.private_ip_address }}"
group: dev
with_items: "{{ instance_facts.instances }}"

- hosts: dev
gather_facts: true
tasks:
- hostname:
name: {{ inventory_hostname }}

关于amazon-ec2 - ansible get tag_Name 并在多个主机上设置主机名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47563848/

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