LoginSignup
7
8

More than 5 years have passed since last update.

ansibleで、AWSインスタンスを作成し、EIPの取得して、アタッチし、Route53を登録するまでを行う

Posted at

インスタンスを作成し、EIPの取得して、アタッチし、Route53を登録するまでを行う、playbookを作成してみました。

sample.yml

# Instance
   - name: Launch Instance
     ec2:
       aws_access_key: "{{ aws_access_key }}"
       aws_secret_key: "{{ aws_secret_key }}"
       region: "{{ region }}"
       key_name: "{{ keypair }}"
       instance_type: "{{ instance_type }}"
       image: "{{ ami }}"
       wait: "{{ instance_running_wait }}"
       group: "{{ security_group }}"
       count: 1
       vpc_subnet_id: "{{ vpc_subnet_id }}"
       assign_public_ip: "{{ assign_public_ip }}"
       instance_tags:
          Name: "{{ instance_name }}"
          User: "{{ user_name }}"
          Group: "{{ group_name }}"
       volumes:
          - volume_size: "{{ ebs_volume_size }}"
            device_name: "{{ ebs_device_name }}"
            delete_on_termination: true
     register: ec2

# Elastic IP
   - name: associate new elastic IPs with each of the instances
     ec2_eip:
       aws_access_key: "{{ aws_access_key }}"
       aws_secret_key: "{{ aws_secret_key }}"
       instance_id : "{{ item }}"
       region: "{{ region }}"
       in_vpc: yes
     with_items: ec2.instance_ids
     register: eip

# Route53
   - name: Add new route53 record
     route53:
      aws_access_key: "{{ aws_access_key  }}"
      aws_secret_key: "{{ aws_secret_key  }}"
      command: create
      zone: "{{ r53_zone }}"
      record: "{{ r53_record }}"
      type: "{{ r53_type }}"
      ttl: "{{ r53_ttl }}"
      value: "{{ item.public_ip }}"
      overwrite: yes
     with_items: eip.results

ec2の解説

assign_public_ip:パブリックIPをアサインするかどうか(yes/no)
delete_on_termination:インスタンス削除と同時にEBSを削除するかどうか(true/false)

Elastic IP

in_vpc:VPC内かどうか(yes/no)

Route53

overwrite:上書きするかどうか(yes/no)

7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8