Amazon ec2 在动态收集的EC2实例上运行任务

Amazon ec2 在动态收集的EC2实例上运行任务,amazon-ec2,ansible,Amazon Ec2,Ansible,我试图编写一个包含任务的角色,该任务过滤掉几个EC2实例,将它们添加到清单中,然后停止它们上的PHP服务 这就是我取得的成就,我从这里复制的添加主机想法: 我的服务任务似乎不是在目标实例上运行,而是在运行此角色的playbook中指定的主机上运行 --- - name: Collect ec2 data connection: local ec2_remote_facts: region: "us-east-1" filters: "tag:Name": M

我试图编写一个包含任务的角色,该任务过滤掉几个EC2实例,将它们添加到清单中,然后停止它们上的PHP服务

这就是我取得的成就,我从这里复制的添加主机想法:

我的服务任务似乎不是在目标实例上运行,而是在运行此角色的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#U remote#tags筛选器确实返回实例(使用真实的标记值,而不是我在本文中输入的伪标记值)。另外,我也看到了ec2_instance_的事实,但我遇到了一些问题(虽然我有一个解决方法,但需要boto3,但我仍在尝试先解决当前问题)。

如果您想在一组目标上执行任务,则需要在使用
添加主机创建内存中目标列表后定义新的重头戏(如您的链接中所述)。为此,您需要这样做:

   … # omit the code before

   - name: "Add the ec2 hosts to a group"
     add_host:
       name: "{{ item.id }}"
       groups: foobar
       ansible_user: root
     with_items: "{{ ec2_info.instances }}"

- hosts: foobar
  gather_facts: false
  become: yes
  tasks:

   - name: Stop the service
     hosts: foobar
     service: name=yii-queue@1 state=stopped enabled=yes

这似乎对我不起作用,“停止服务”任务没有为我运行。您确定
ec2\u remote\u facts
返回任何ec2实例(在
ec2\u info.instances
之后尝试任务
debug
)。顺便说一下,
ec2\u remote\u facts
已被
ec2\u instance\u facts
替换。请回答原始问题,请确定,并根据您的建议更新最新输出。