通过带有 ansible 的标签启动之前停止的 EC2 实例

Adr*_*ian 3 amazon-ec2 ansible

我正在尝试创建一个剧本或角色,让我可以使用 ansible by tag 启动之前停止的 EC2 实例(EC2 实例通过标签分配到清单中的静态组)。该ec2.ini文件已更改为还返回有关已停止实例的信息。到目前为止我见过的唯一类似的例子依赖于ec2_factsget instance_ids

ansible 网站上的官方示例region假设和instance_ids已经提前已知/硬编码。


- name: Start sandbox instances
  hosts: localhost
  gather_facts: false
  connection: local
  vars:
    instance_ids:
      - 'i-xxxxxx'
      - 'i-xxxxxx'
      - 'i-xxxxxx'
    region: us-east-1
  tasks:
    - name: Start the sandbox instances
      ec2:
        instance_ids: '{{ instance_ids }}'
        region: '{{ region }}'
        state: running
        wait: True
        vpc_subnet_id: subnet-29e63245
        assign_public_ip: yes
  role:
    - do_neat_stuff
    - do_more_neat_stuff
Run Code Online (Sandbox Code Playgroud)

最好是我正在寻找一种解决方案,例如,如果可能的话,我可以从动态库存中获取必要的变量。

Adr*_*ian 5

现有的 EC2 实例可以通过下面的操作启动,无需对实例标识符或 EC2 区域进行硬编码。


--- ec2.yml
- hosts: aws
  gather_facts: false
  tasks:
  - name: start EC2 instance
    local_action:
      module: ec2
        state=running
        region={{ hostvars[inventory_hostname]['ec2_region'] }}
        instance_ids={{ hostvars[inventory_hostname]['ec2_id'] }}
    tags:
      - start-instance
Run Code Online (Sandbox Code Playgroud)

假设动态实例被标记,以下命令将启动实例:

$ ansible-playbook -i inventory --limit [instance static group name] --tags start-instance ec2.yml