ansible 并重新加载 AWS 动态清单

Son*_*ton 7 amazon-web-services ansible

另请参阅:https : //stackoverflow.com/questions/29003420/reload-ansibles-dynamic-inventory

我的问题:有没有更好的方法来做下面的事情?

我有一个 ansible 角色可以预配 AWS 机器并正确运行(注意provision标签):

- name: AWS provision
  hosts: localhost
  gather_facts: no
  vars_files:
    - vars/dev.yml
  user: ec2-user
  roles:
    - provision
  tags:
    - provision
Run Code Online (Sandbox Code Playgroud)

然后我有一个base角色,我希望能够独立运行(例如在开发过程中,所以我不必等待重新配置(注意base标签)。我运行一个find running instances过滤器并将主机存储在小组started

- name: find running instances
  hosts: localhost
  vars_files:
    - vars/dev.yml
  gather_facts: no
  tags:
    - base
  tasks:
    - name: gather remote facts
      ec2_remote_facts:
        region: "{{ target_aws_region }}"
        filters:
          instance-state-name: running
          "tag:Name": "{{ instance_name }}"
      register: ec2_facts

    - debug: var=ec2_facts

    - name: add hosts to groups
      add_host:
        name: "{{ item.id }}"
        ansible_ssh_host: "{{ item.public_dns_name }}"
        groups: started
      changed_when: false
      with_items: ec2_facts.instances

- name: base setup
  hosts: started
  gather_facts: no
  vars_files:
    - vars/dev.yml
  user: ec2-user
  roles:
    - base
  tags:
    - base
Run Code Online (Sandbox Code Playgroud)

我的问题是:戏剧正在奏效,但有没有更好的方法来做到这一点?例如,我被gather_facts: no跟踪ec2_remote_factsfilters- 这一切似乎相当复杂。

澄清:感谢您的评论ec2.py——我已经在我的第一部剧中使用了它(当我调用这个provision角色时)。

但出于测试目的,我想在不重新进行(慢速)配置的情况下跳入后续播放。那么如何重新填充我的主机数据呢?ec2_remote_facts其次add_host的正确方法?或者我可以以某种方式使用gather_facts: yes

Juk*_*kka 6

我可能会改用 EC2 动态清单脚本,您可以通过配置ec2.ini并传递-i ec2.pyansible-playbook.

有关更多信息,请参阅http://docs.ansible.com/ansible/intro_dynamic_inventory.html#example-aws-ec2-external-inventory-script

请注意, 中有很多选项ec2.ini。一定要看看那些,例如cache_max_age。您还可以通过过滤不必要的资源(例如,rds = False如果您只对 EC2 实例感兴趣,则设置)使清单生成更快。

更新:使用 Ansible 2.x+,您还可以使用- meta: refresh_inventory中游。