我正在尝试编写一个角色,其中包含过滤掉几个EC2实例的任务,将它们添加到库存中,然后停止对它们的PHP服务 .
这是我从这里复制的距离:http://docs.catalystcloud.io/tutorials/ansible-create-x-servers-using-in-memory-inventory.html
我的服务任务似乎没有在目标实例上运行,而是在运行此角色的playbook中指定的主机上运行 .
---
- name: Collect ec2 data
connection: local
ec2_remote_facts:
region: "us-east-1"
filters:
"tag:Name": MY_TAG
register: ec2_info
- name: "Add the ec2 hosts to a group"
add_host:
name: "{{ item.id }}"
groups: foobar
ansible_user: root
with_items: "{{ ec2_info.instances }}"
- name: Stop the service
hosts: foobar
become: yes
gather_facts: false
service: name=yii-queue@1 state=stopped enabled=yes
更新:当我尝试baptistemm的建议时,我得到了这个:
PLAY [MAIN_HOST_NAME] ***************************
TASK [ec2-manage : Collect ec2 data]
*******************************************
ok: [MAIN_HOST_NAME]
TASK [ec2-manage : Add the hosts to a new group]
*******************************************************
PLAY RECAP **********************************************************
MAIN_HOST_NAME : ok=1 changed=0 unreachable=0 failed=0
更新#2 - 是的,ec2_remote_tags过滤器确实返回实例(使用真实标签值而不是我在这篇文章中放置的假标签) . 此外,我已经看到了ec2_instance_facts,但我遇到了一些问题(boto3需要虽然我有一个解决方法,但我仍然试图先解决当前的问题) .
1 回答
如果要在一组目标上执行任务,则需要在使用
add_host
创建内存目标列表后定义新的游戏(如链接中所述) . 所以要做到这一点,你需要这样: