【问题标题】:Running tasks on dynamically collected EC2 instances在动态收集的 EC2 实例上运行任务
【发布时间】:2018-08-08 02:34:04
【问题描述】:

我正在尝试编写一个角色,其任务是过滤掉多个 EC2 实例,将它们添加到清单中,然后停止它们上的 PHP 服务。

这就是我的成果,我从这里复制的 add_host 想法:http://docs.catalystcloud.io/tutorials/ansible-create-x-servers-using-in-memory-inventory.html

我的服务任务似乎没有在目标实例上运行,而是在运行此角色的剧本中指定的主机上运行。

---
- 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,尽管我有一个解决方法,但我仍在尝试首先解决当前问题)。

【问题讨论】:

  • 好的,根据您的建议使用最新输出进行更新。

标签: amazon-ec2 ansible


【解决方案1】:

如果您想在一组目标上播放任务,您需要在使用 add_host 创建内存中目标列表后定义新的播放(如您的链接中所述)。所以要做到这一点,你需要这样:

   … # 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_remote_facts 返回任何 ec2 实例(在 ec2_info.instances 之后尝试使用任务 debug)。顺便说一句,ec2_remote_factsec2_instance_facts 取代。请回复原问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-07
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 2011-01-13
相关资源
最近更新 更多