2

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

[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调用之前处于运行状态。

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

4

4 回答 4

6

After researching I came to know that the dynamic inventory doesnt refresh between playbook calls, it will only refresh if you are executing the playbook seprately. However I was able to resolve the issue by using add_host command.

- name: Add  Server to inventory
  local_action: add_host hostname={{ item.public_ip }} groupname=webserver
  with_items: webserver.instances
于 2015-03-13T23:34:44.610 回答
2

使用 ansible 2.0+,您可以在 playbook 中间刷新动态库存,任务如下:

- meta: refresh_inventory

为了扩展这一点,如果您在剧本中遇到缓存问题,那么您可以像这样使用它:

   - name: Refresh the ec2.py cache
     shell: "./inventory/ec2.py --refresh-cache"
     changed_when: no

   - name: Refresh inventory
     meta: refresh_inventory

./inventory您的动态库存的路径在哪里,请相应调整。

希望这会帮助你。

于 2016-03-19T20:30:59.160 回答
0

Configure EC2 serverplay 无法从 EC2 动态清单中找到任何主机,因为新实例是在 playbook 的第一次播放中添加的 - 在同一执行期间。读取库存时,库存中不存在组tag_Name_EC2,因此无法找到。

当您再次运行相同的剧本时,Configure EC2 server应该会找到该组。

在这种情况下,我们使用了以下解决方法来指导用户。

首先,配置实例:

tasks:
  - name: Provisioning EC2 instance
    local_action:
    module: ec2
    ...
    register: ec2

然后在之前添加一个新的玩法ec2-configure.yml。play 使用ec2了注册的变量,Provisioning EC2 instance如果启动了任何实例,它将失败并退出 playbook:

- name: Stop and request a re-run if any instances were launched
  hosts: localhost
  gather_facts: no
  tasks:
    - name: Stop if instances were launched
      fail: msg="Re-run the playbook to load group variables from EC2 dynamic inventory for the just launched instances!"
      when: ec2.changed

- include: ec2-configure.yml 
于 2015-03-03T09:10:31.020 回答
0

您还可以刷新缓存:

ec2.py --refresh-cache

或者,如果您用作 Ansible 主机文件:

/etc/ansible/hosts --refresh-cache

于 2015-09-05T01:17:22.993 回答