gpt4 book ai didi

amazon-web-services - Ansible Dynamic Inventory 获取最新ec2信息失败

转载 作者:行者123 更新时间:2023-12-04 08:13:47 25 4
gpt4 key购买 nike

我正在使用 ec2.py 动态 list 来配置 ansible。我已将 ec2.py 放在 /etc/ansible/hosts 文件中并将其标记为可执行文件。我在 /etc/ansible/hosts 中也有 ec2.ini 文件。

[ec2]

regions = us-west-2
regions_exclude = us-gov-west-1,cn-north-1


destination_variable = public_dns_name

vpc_destination_variable = ip_address
route53 = False

all_instances = True
all_rds_instances = False


cache_path = ~/.ansible/tmp

cache_max_age = 0

nested_groups = False
group_by_instance_id = True
group_by_region = True
group_by_availability_zone = True
group_by_ami_id = True
group_by_instance_type = True
group_by_key_pair = True
group_by_vpc_id = True
group_by_security_group = True
group_by_tag_keys = True
group_by_tag_none = True
group_by_route53_names = True
group_by_rds_engine = True
group_by_rds_parameter_group = True

上面是我的ec2.ini文件

---
- hosts: localhost
connection: local
gather_facts: yes
vars_files:
- ../group_vars/dev_vpc
- ../group_vars/dev_sg
- ../hosts_vars/ec2_info
vars:
instance_type: t2.micro
tasks:
- name: Provisioning EC2 instance
local_action:
module: ec2
region: "{{ region }}"
key_name: "{{ key }}"
instance_type: "{{ instance_type }}"
image: "{{ ami_id }}"
wait: yes
group_id: ["{{ sg_npm }}", "{{sg_ssh}}"]
vpc_subnet_id: "{{ PublicSubnet }}"
source_dest_check: false
instance_tags: '{"Name": "EC2", "Environment": "Development"}'
register: ec2
- name: associate new EIP for the instance
local_action:
module: ec2_eip
region: "{{ region }}"
instance_id: "{{ item.id }}"
with_items: ec2.instances
- name: Waiting for NPM Server to come-up
local_action:
module: wait_for
host: "{{ ec2 }}"
state: started
delay: 5
timeout: 200
- include: ec2-configure.yml

现在配置脚本如下

- name: Configure EC2 server
hosts: tag_Name_EC2
user: ec2-user
sudo: True
gather_facts: True
tasks:
- name: Install nodejs related packages
yum: name={{ item }} enablerepo=epel state=present
with_items:
- nodejs
- npm

然而,当配置脚本被调用时,第二个脚本导致找不到主机。如果我单独执行 ec2-configure.yml 并且如果 EC2 服务器已启动并正在运行,则它能够找到它并进行配置。

我添加了 wait_for 以确保实例在调用 ec2-configure.yml 之前处于运行状态。

如果有人能指出我的错误,我将不胜感激。谢谢

最佳答案

经过研究,我了解到动态 list 不会在 playbook 调用之间刷新,它只会在您单独执行 playbook 时刷新。但是,我能够通过使用 add_host 命令解决问题。

- name: Add  Server to inventory
local_action: add_host hostname={{ item.public_ip }} groupname=webserver
with_items: webserver.instances

关于amazon-web-services - Ansible Dynamic Inventory 获取最新ec2信息失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28775436/

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