Ansible - 作成後のAWS EC2インスタンスの設定

Ansible - 作成後のAWS EC2インスタンスの設定

CloudFormation テンプレートから AWS EC2 インスタンスを作成する Ansible プレイブックがあります。一度作成したら、Ansibleで設定したいと思います。

これが私が今持っているものです:

---
- name: Create Amazon Linux Instance
  hosts: localhost
  connection: local
  gather_facts: no
  vars_files:
  - config.yml

  tasks:
  - name: Create CloudFormation Stack
    cloudformation:
      stack_name: "{{ stack_name }}"
      state: present
      template: basic-ec2-stack.json
      template_parameters:
        KeyName: "{{ key_name }}"
        VpcId: "{{ vpc_id }}"
        SubnetId: "{{ subnet_id }}"
        ...
    register: stack

  # The new instance name is in stack.stack_outputs.DnsName ...
  - debug: var=stack.stack_outputs.DnsName

何をすべきか?新しく作成されたホストに対してプレイブックの残りの部分をどのように実行しますか?

たとえば、ユーザー「blah」を作成したいが、localhost(cloudformationモジュールが実行されている場所)ではなく、新しいEC2インスタンスで作成したいとします。どうすればいいですか?

ありがとうございます!

ベストアンサー1

グループにインスタンスを追加できる必要があります。ホストの追加メモリインベントリを作成します。

   - name: Add instance  to host group
     add_host: hostname={{ item.DnsName }} groups=cloud_formation
     with_items: stack.stack_outputs

   - name: Wait for SSH to come up
     wait_for: host={{ item.DnsName }} port=22 delay=60 timeout=320 state=started
     with_items: stack.stack_outputs

   - name: Run your play
     hosts: cloud_formation
     ----- your play here -------

おすすめ記事