负责并重新加载AWSdynamic库存

另见: https : //stackoverflow.com/questions/29003420/reload-ansibles-dynamic-inventory 。

我的问题:是否有更好的方式做下面的事情?

我有一个提供AWS机器的正确angular色(注意provision标签):

 - name: AWS provision hosts: localhost gather_facts: no vars_files: - vars/dev.yml user: ec2-user roles: - provision tags: - provision 

然后我有一个baseangular色,我希望能够独立运行(例如在开发过程中,所以我不必等待重新供应(注意base标签))我运行一个播放find running instancesfilter并将该主机存储在组中:

 - 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 

我的问题:戏剧正在工作,但有没有更好的方式来做到这一点? 例如,我得到了gather_facts: no跟着ec2_remote_factsfilters – 这一切似乎相当复杂。

澄清:感谢有关ec2.py的评论 – 我已经在我的第一个播放中使用它(当我呼吁provisionangular色)。

但为了testing的目的,我想跳进后续的游戏,而不用重新进行(慢速)configuration。 那么如何重新填充我的主机数据呢? ec2_remote_facts跟着add_host是正确的吗? 或者我可以以某种方式使用gather_facts: yes吗?

我可能会使用EC2dynamic库存脚本,您可以通过configurationec2.ini并将-i ec2.py传递给ansible-playbook

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

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

更新:有了Ansible 2.x +,你也可以使用- meta: refresh_inventory mid-play。

虽然meta: refresh_inventory是“首选方法”,但我倾向于将OP的build议与ec2_remote_facts一起使用add_host 。 我已经设置了这样一个剧本,它有100%的动力,没有caching小故障。

假设您的ASG使用env: cool_asg_instance标签激发了实例,只需在ec2_asg playbook调用下添加以下内容:

 -  ec2_remote_facts:
    filter:
       “tag:env”:“cool_asg_instance”
  注册:instance_facts

然后,您将收集包含所有必需信息的完整JSON数据集,从那里您可以使用剧本中的Jinja2function提取新创build的IP地址,即:

 - 名称:组主机
   add_host:hostname = {{item}} groups =启动
   with_items:“{{instance_facts.instances | selectattr('state','equalto','running')| map(attribute ='private_ip_address')| list}}”

filter是这个好博客文章的礼貌: https ://bonovoxly.github.io/2016-02-11-ansible-stuffs-ec2_remote_facts_instead_of_ec2_py

从现在开始,您可以像这样在您的父级部署YAML文件上使用已launched组:

 - 主持人:启动
   gather_facts:不

  任务: 
     - 名称:等待SSH
       wait_for:port = 22 host =“{{inventory_hostname}}”search_regex = OpenSSH delay = 5

有人可能会问,为什么头痛,想象不是有一个丑陋的用户数据,将从互联网git clone Ansible和剧本,你可以触发从你自己的部署中心实例设置通过设置一个简单的SNS主题,将发布到一个SQS队列,由10行python代码( https://github.com/alexandregama/python-sqs-consumer/blob/master/sqs-message-consumer-polling.py )观看,这将触发Ansible当一个新的实例出来。

我发现这比我想象的要容易。 以下代码允许您针对现有AWS清单运行操作手册,这正是我最初想要做的。 基于[1]和[2]。

(我的解决scheme比较灵活,比如定制ansible.cfgssh_config

设置一个合理的configuration文件并使用它:

 % cat ./ansible.cfg.foo [defaults] hostfile = inventory host_key_checking = false private_key_file = bar.pem remote_user = ec2-user ssh_args = -F ./ssh_config export ANSIBLE_CONFIG=ansible.cfg.foo 

(可选)设置自定义的sshconfiguration,所以你的笔记本电脑的设置不会干扰。 sshconfiguration也可以在ansible.cfg.foo中完成,但我喜欢将它分开,所以现有的~/.ssh/config可以被第三方使用。

 % cat ./ssh_config StrictHostKeyChecking no ... 

运行你的剧本:

 ansible-playbook config.yml --tags base 

剧本可能如下所示。 aws标签(例如tag_env_test )用于select主机。

 % cat config.yml - name: base setup hosts: - tag_env_test become: true vars_files: - vars/qux.yml roles: - base tags: - base - name: java setup hosts: ... 

我使用./ec2.py --list > ../ec2_output.txt来调查我有兴趣select的主机。

我已经将Jukka的答案标记为解决scheme,感谢他的帮助:-)

[1] http://www.slideshare.net/bfschott/using-ansible-dynamic-inventory-with-amazon-ec2

[2] https://aws.amazon.com/blogs/apn/getting-started-with-ansible-and-dynamic-amazon-ec2-inventory-management/