【ansible/aws】ansibleで ec2 インスタンスを作成
リポジトリはこちら github.com
目次
構成
. ├── aws.yml ├── hosts │ ├── aws │ │ ├── ec2.ini │ │ └── ec2.py └── roles └── aws ├── ec2 └── tasks └── main.yml
aws.yml
変数を管理
--- - hosts: localhost connection: local gather_facts: no roles: - role: aws/ec2 vars: ami_image: ami-2a69be4c # 2017-11-04設定 key_name: awsのpemファイル名 tag_name: インスタン名 instance_type: "t2.micro" device_name: "/dev/xvda" device_type: "gp2" volume_size: 30 region: ap-northeast-1 security_group: - セキュリティーグループを - 複数指定 - IDではなく、名前で指定する vpc_subnet_id: vpc subnet ID assign_public_ip: yes
roles/aws/ec2/tasks/main.yml
--- - name: EC2インスタンスを作成 ec2: image: "{{ ami_image }}" instance_type: "{{ instance_type }}" region: "{{ region }}" key_name: "{{ key_name }}" group: "{{ security_group }}" # ここでsecurity_groupを設定する vpc_subnet_id: "{{ vpc_subnet_id }}" instance_tags: Name: "{{ tag_name }}" wait: yes wait_timeout: 300 assign_public_ip: "{{ assign_public_ip }}" volumes: - device_name: "{{ device_name }}" device_type: "{{ device_type }}" volume_size: "{{ volume_size }}" delete_on_termination: yes count_tag: Name: "{{ tag_name }}" exact_count: 1 #重複作成停止 上のタグがいる register: ec2