SIOS Coati開発チームの清水です。
今回はAnsibleで開発するうえで知っておくと便利なデバッグTIPSを2つ紹介します。
- Playbook Debugger
- Step実行
どちらも公式のドキュメントに書かれていることなので、既に知っている方もいらっしゃるかと思いますが、もしご存知ではない方はこの機会に是非活用してみてください。 続きを読む
SIOS Coati開発チームの清水です。
今回はAnsibleで開発するうえで知っておくと便利なデバッグTIPSを2つ紹介します。
どちらも公式のドキュメントに書かれていることなので、既に知っている方もいらっしゃるかと思いますが、もしご存知ではない方はこの機会に是非活用してみてください。 続きを読む
遂にニンテンド〇スイッチを入手したSIOS Coati開発チームの沼野井です。SIOS Coatiの開発に深刻な影響が
…前回まで、AnsibleによるAWS環境構築について説明しました。そこではAnsible 2.3を使用していたのですが、2017/9/19に新バージョンの2.4がリリースされました(10/25には早くも2.4.1がリリースされています)。
今回は、Ansible 2.3と2.4の、AWS関連モジュールの違いについて調べました。
以下が2.3と2.4のAWS関連モジュールの一覧です。
ansible 2.3 | ansible 2.4 |
aws_api_gateway – Manage AWS API Gateway APIs | |
aws_direct_connect_connection – Creates, deletes, modifies a DirectConnect connection | |
aws_direct_connect_link_aggregation_group – Manage Direct Connect LAG bundles. | |
aws_kms – Perform various KMS management tasks. | aws_kms – Perform various KMS management tasks. |
aws_s3 – manage objects in S3. | |
aws_s3_bucket_facts – Lists S3 buckets in AWS | |
aws_waf_facts – Retrieve facts for WAF ACLs, Rule , Conditions and Filters. | |
cloudformation – Create or delete an AWS CloudFormation stack | cloudformation – Create or delete an AWS CloudFormation stack |
cloudformation_facts – Obtain facts about an AWS CloudFormation stack | cloudformation_facts – Obtain facts about an AWS CloudFormation stack |
cloudfront_facts – Obtain facts about an AWS CloudFront distribution | cloudfront_facts – Obtain facts about an AWS CloudFront distribution |
cloudtrail – manage CloudTrail creation and deletion | cloudtrail – manage CloudTrail create, delete, update |
cloudwatchevent_rule – Manage CloudWatch Event rules and targets | cloudwatchevent_rule – Manage CloudWatch Event rules and targets |
data_pipeline – Create and manage AWS Datapipelines | |
dynamodb_table – Create, update or delete AWS Dynamo DB tables. | dynamodb_table – Create, update or delete AWS Dynamo DB tables. |
dynamodb_ttl – set TTL for a given DynamoDB table. | |
ec2 – create, terminate, start or stop an instance in ec2 | ec2 – create, terminate, start or stop an instance in ec2 |
ec2_ami – create or destroy an image in ec2 | ec2_ami – create or destroy an image in ec2 |
ec2_ami_copy – copies AMI between AWS regions, return new image id | ec2_ami_copy – copies AMI between AWS regions, return new image id |
ec2_ami_find – Searches for AMIs to obtain the AMI ID and other information | ec2_ami_find – Searches for AMIs to obtain the AMI ID and other information |
ec2_ami_search (D) – Retrieve AWS AMI information for a given operating system. | ec2_ami_search **(D)** – Retrieve AWS AMI information for a given operating system. |
ec2_asg – Create or delete AWS Autoscaling Groups | ec2_asg – Create or delete AWS Autoscaling Groups |
ec2_asg_facts – Gather facts about ec2 Auto Scaling Groups (ASGs) in AWS | ec2_asg_facts – Gather facts about ec2 Auto Scaling Groups (ASGs) in AWS |
ec2_customer_gateway – Manage an AWS customer gateway | ec2_customer_gateway – Manage an AWS customer gateway |
ec2_eip – manages EC2 elastic IP (EIP) addresses. | ec2_eip – manages EC2 elastic IP (EIP) addresses. |
ec2_elb – De-registers or registers instances from EC2 ELBs | ec2_elb – De-registers or registers instances from EC2 ELBs |
ec2_elb_facts – Gather facts about EC2 Elastic Load Balancers in AWS | ec2_elb_facts – Gather facts about EC2 Elastic Load Balancers in AWS |
ec2_elb_lb – Creates or destroys Amazon ELB. | ec2_elb_lb – Creates or destroys Amazon ELB. |
ec2_eni – Create and optionally attach an Elastic Network Interface (ENI) to an instance | ec2_eni – Create and optionally attach an Elastic Network Interface (ENI) to an instance |
ec2_eni_facts – Gather facts about ec2 ENI interfaces in AWS | ec2_eni_facts – Gather facts about ec2 ENI interfaces in AWS |
ec2_facts – Gathers facts about remote hosts within ec2 (aws) | |
ec2_group – maintain an ec2 VPC security group. | ec2_group – maintain an ec2 VPC security group. |
ec2_group_facts – Gather facts about ec2 security groups in AWS. | ec2_group_facts – Gather facts about ec2 security groups in AWS. |
ec2_instance_facts – Gather facts about ec2 instances in AWS | |
ec2_key – maintain an ec2 key pair. | ec2_key – maintain an ec2 key pair. |
ec2_lc – Create or delete AWS Autoscaling Launch Configurations | ec2_lc – Create or delete AWS Autoscaling Launch Configurations |
ec2_lc_facts – Gather facts about AWS Autoscaling Launch Configurations | ec2_lc_facts – Gather facts about AWS Autoscaling Launch Configurations |
ec2_lc_find – Find AWS Autoscaling Launch Configurations | ec2_lc_find – Find AWS Autoscaling Launch Configurations |
ec2_metadata_facts – Gathers facts (instance metadata) about remote hosts within ec2 | |
ec2_metric_alarm – Create/update or delete AWS Cloudwatch ‘metric alarms’ | ec2_metric_alarm – Create/update or delete AWS Cloudwatch ‘metric alarms’ |
ec2_remote_facts – Gather facts about ec2 instances in AWS | ec2_remote_facts **(D)** – Gather facts about ec2 instances in AWS |
ec2_scaling_policy – Create or delete AWS scaling policies for Autoscaling groups | ec2_scaling_policy – Create or delete AWS scaling policies for Autoscaling groups |
ec2_snapshot – creates a snapshot from an existing volume | ec2_snapshot – creates a snapshot from an existing volume |
ec2_snapshot_copy – copies an EC2 snapshot and returns the new Snapshot ID. | |
ec2_snapshot_facts – Gather facts about ec2 volume snapshots in AWS | ec2_snapshot_facts – Gather facts about ec2 volume snapshots in AWS |
ec2_tag – create and remove tag(s) to ec2 resources. | ec2_tag – create and remove tag(s) to ec2 resources. |
ec2_vol – create and attach a volume, return volume id and device map | ec2_vol – create and attach a volume, return volume id and device map |
ec2_vol_facts – Gather facts about ec2 volumes in AWS | ec2_vol_facts – Gather facts about ec2 volumes in AWS |
ec2_vpc (D) – configure AWS virtual private clouds | ec2_vpc **(D)** – configure AWS virtual private clouds |
ec2_vpc_dhcp_options – Manages DHCP Options, and can ensure the DHCP options for the given VPC match what’s requested | ec2_vpc_dhcp_option – Manages DHCP Options, and can ensure the DHCP options for the given VPC match what’s requested |
ec2_vpc_dhcp_options_facts – Gather facts about dhcp options sets in AWS | ec2_vpc_dhcp_option_facts – Gather facts about dhcp options sets in AWS |
ec2_vpc_endpoint – Create and delete AWS VPC Endpoints. | |
ec2_vpc_endpoint_facts – Retrieves AWS VPC endpoints details using AWS methods. | |
ec2_vpc_igw – Manage an AWS VPC Internet gateway | ec2_vpc_igw – Manage an AWS VPC Internet gateway |
ec2_vpc_igw_facts – Gather facts about internet gateways in AWS | ec2_vpc_igw_facts – Gather facts about internet gateways in AWS |
ec2_vpc_nacl – create and delete Network ACLs. | ec2_vpc_nacl – create and delete Network ACLs. |
ec2_vpc_nacl_facts – Gather facts about Network ACLs in an AWS VPC | ec2_vpc_nacl_facts – Gather facts about Network ACLs in an AWS VPC |
ec2_vpc_nat_gateway – Manage AWS VPC NAT Gateways. | ec2_vpc_nat_gateway – Manage AWS VPC NAT Gateways. |
ec2_vpc_nat_gateway_facts – Retrieves AWS VPC Managed Nat Gateway details using AWS methods. | ec2_vpc_nat_gateway_facts – Retrieves AWS VPC Managed Nat Gateway details using AWS methods. |
ec2_vpc_net – Configure AWS virtual private clouds | ec2_vpc_net – Configure AWS virtual private clouds |
ec2_vpc_net_facts – Gather facts about ec2 VPCs in AWS | ec2_vpc_net_facts – Gather facts about ec2 VPCs in AWS |
ec2_vpc_peer – create, delete, accept, and reject VPC peering connections between two VPCs. | ec2_vpc_peer – create, delete, accept, and reject VPC peering connections between two VPCs. |
ec2_vpc_peering_facts – Retrieves AWS VPC Peering details using AWS methods. | |
ec2_vpc_route_table – Manage route tables for AWS virtual private clouds | ec2_vpc_route_table – Manage route tables for AWS virtual private clouds |
ec2_vpc_route_table_facts – Gather facts about ec2 VPC route tables in AWS | ec2_vpc_route_table_facts – Gather facts about ec2 VPC route tables in AWS |
ec2_vpc_subnet – Manage subnets in AWS virtual private clouds | ec2_vpc_subnet – Manage subnets in AWS virtual private clouds |
ec2_vpc_subnet_facts – Gather facts about ec2 VPC subnets in AWS | ec2_vpc_subnet_facts – Gather facts about ec2 VPC subnets in AWS |
ec2_vpc_vgw – Create and delete AWS VPN Virtual Gateways. | ec2_vpc_vgw – Create and delete AWS VPN Virtual Gateways. |
ec2_vpc_vgw_facts – Gather facts about virtual gateways in AWS | ec2_vpc_vgw_facts – Gather facts about virtual gateways in AWS |
ec2_vpc_vpn – Create, modify, and delete EC2 VPN connections. | |
ec2_win_password – gets the default administrator password for ec2 windows instances | ec2_win_password – gets the default administrator password for ec2 windows instances |
ecs_attribute – manage ecs attributes | |
ecs_cluster – create or terminate ecs clusters | ecs_cluster – create or terminate ecs clusters |
ecs_ecr – Manage Elastic Container Registry repositories | ecs_ecr – Manage Elastic Container Registry repositories |
ecs_service – create, terminate, start or stop a service in ecs | ecs_service – create, terminate, start or stop a service in ecs |
ecs_service_facts – list or describe services in ecs | ecs_service_facts – list or describe services in ecs |
ecs_task – run, start or stop a task in ecs | ecs_task – run, start or stop a task in ecs |
ecs_taskdefinition – register a task definition in ecs | ecs_taskdefinition – register a task definition in ecs |
efs – create and maintain EFS file systems | efs – create and maintain EFS file systems |
efs_facts – Get information about Amazon EFS file systems | efs_facts – Get information about Amazon EFS file systems |
elasticache – Manage cache clusters in Amazon Elasticache. | elasticache – Manage cache clusters in Amazon Elasticache. |
elasticache_parameter_group – Manage cache security groups in Amazon Elasticache. | elasticache_parameter_group – Manage cache security groups in Amazon Elasticache. |
elasticache_snapshot – Manage cache snapshots in Amazon Elasticache. | elasticache_snapshot – Manage cache snapshots in Amazon Elasticache. |
elasticache_subnet_group – manage Elasticache subnet groups | elasticache_subnet_group – manage Elasticache subnet groups |
elb_application_lb – Manage an Application load balancer | |
elb_application_lb_facts – Gather facts about application ELBs in AWS | |
elb_classic_lb – Creates or destroys Amazon ELB. | |
elb_classic_lb_facts – Gather facts about EC2 Elastic Load Balancers in AWS | |
elb_instance – De-registers or registers instances from EC2 ELBs | |
elb_target_group – Manage a target group for an Application load balancer | |
elb_target_group_facts – Gather facts about ELB target groups in AWS | |
execute_lambda – Execute an AWS Lambda function | execute_lambda – Execute an AWS Lambda function |
iam – Manage IAM users, groups, roles and keys | iam – Manage IAM users, groups, roles and keys |
iam_cert – Manage server certificates for use on ELBs and CloudFront | iam_cert – Manage server certificates for use on ELBs and CloudFront |
iam_group – Manage AWS IAM groups | |
iam_managed_policy – Manage User Managed IAM policies | |
iam_mfa_device_facts – List the MFA (Multi-Factor Authentication) devices registered for a user | iam_mfa_device_facts – List the MFA (Multi-Factor Authentication) devices registered for a user |
iam_policy – Manage IAM policies for users, groups, and roles | iam_policy – Manage IAM policies for users, groups, and roles |
iam_role – Manage AWS IAM roles | iam_role – Manage AWS IAM roles |
iam_server_certificate_facts – Retrieve the facts of a server certificate | iam_server_certificate_facts – Retrieve the facts of a server certificate |
kinesis_stream – Manage a Kinesis Stream. | kinesis_stream – Manage a Kinesis Stream. |
lambda – Manage AWS Lambda functions | lambda – Manage AWS Lambda functions |
lambda_alias – Creates, updates or deletes AWS Lambda function aliases. | lambda_alias – Creates, updates or deletes AWS Lambda function aliases. |
lambda_event – Creates, updates or deletes AWS Lambda function event mappings. | lambda_event – Creates, updates or deletes AWS Lambda function event mappings. |
lambda_facts – Gathers AWS Lambda function details as Ansible facts | lambda_facts – Gathers AWS Lambda function details as Ansible facts |
lambda_policy – Creates, updates or deletes AWS Lambda policy statements. | |
lightsail – Create or delete a virtual machine instance in AWS Lightsail | |
rds – create, delete, or modify an Amazon rds instance | rds – create, delete, or modify an Amazon rds instance |
rds_param_group – manage RDS parameter groups | rds_param_group – manage RDS parameter groups |
rds_subnet_group – manage RDS database subnet groups | rds_subnet_group – manage RDS database subnet groups |
redshift – create, delete, or modify an Amazon Redshift instance | redshift – create, delete, or modify an Amazon Redshift instance |
redshift_facts – Gather facts about Redshift cluster(s) | |
redshift_subnet_group – mange Redshift cluster subnet groups | redshift_subnet_group – mange Redshift cluster subnet groups |
route53 – add or delete entries in Amazons Route53 DNS service | route53 – add or delete entries in Amazons Route53 DNS service |
route53_facts – Retrieves route53 details using AWS methods | route53_facts – Retrieves route53 details using AWS methods |
route53_health_check – add or delete health-checks in Amazons Route53 DNS service | route53_health_check – add or delete health-checks in Amazons Route53 DNS service |
route53_zone – add or delete Route53 zones | route53_zone – add or delete Route53 zones |
s3 – manage objects in S3. | |
s3_bucket – Manage S3 buckets in AWS, Ceph, Walrus and FakeS3 | s3_bucket – Manage S3 buckets in AWS, Ceph, Walrus and FakeS3 |
s3_lifecycle – Manage s3 bucket lifecycle rules in AWS | s3_lifecycle – Manage s3 bucket lifecycle rules in AWS |
s3_logging – Manage logging facility of an s3 bucket in AWS | s3_logging – Manage logging facility of an s3 bucket in AWS |
s3_sync – Efficiently upload multiple files to S3 | s3_sync – Efficiently upload multiple files to S3 |
s3_website – Configure an s3 bucket as a website | s3_website – Configure an s3 bucket as a website |
sns – Send Amazon Simple Notification Service (SNS) messages | |
sns_topic – Manages AWS SNS topics and subscriptions | sns_topic – Manages AWS SNS topics and subscriptions |
sqs_queue – Creates or deletes AWS SQS queues. | sqs_queue – Creates or deletes AWS SQS queues. |
sts_assume_role – Assume a role using AWS Security Token Service and obtain temporary credentials | sts_assume_role – Assume a role using AWS Security Token Service and obtain temporary credentials |
sts_session_token – Obtain a session token from the AWS Security Token Service | sts_session_token – Obtain a session token from the AWS Security Token Servicea |
青字が2.4で新しくできたモジュール、赤字が2.4でなくなったモジュールです。
ec2_factsはec2_metadata_factsに、s3はaws_s3に名称が変更されたようです。移行する方はご注意を!
2.4では、API Gateway, Direct Connectなどのサービスのモジュールが新規追加されていますね。
あと、XXXX_factsというモジュールが増えています。前回ほんのちょっと記載したように、”facts”は情報を収集するためのモジュールです。
IDとかIPアドレスとか、既存のリソースの情報を取得したいケースは結構あるので、嬉しいですね。
EC2関連では, ec2_instance_factsというインスタンスの属性情報を収集するモジュールが登場していますが、これは、ec2_remote_factsの代わりです。
ec2_remote_factsは2.4でもまだ存在しますが、2.4からはDeprecated(非推奨)の扱いになりましたので、これもご注意ください。
さらに便利になったAnsibleに興味津々だけど、導入や運用にちょっと不安があるな・・という方は、ぜひ弊社テクニカルサービスをご検討ください!
食欲の秋に(体重的な意味で)危機感を覚える Coati開発チーム 沼野井です。
でも秋の味覚を食べないという選択肢はないですよね!
ansibleで、
VPCと、それに紐づく サブネット、ルートテーブル、セキュリティグループを作ったCoatiだった・・・
というわけで、前回に続いて、ansibleでEC2・VPC環境を構築していこうと思います。
今回は、
します。
Coatiチームで使用している、環境デプロイ用のPlaybookのディレクトリ構成と、変数定義をしているall.ymlの内容をもう一回掲載します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
├── ansible.cfg ├── group_vars │ └── all.yml # 変数定義 ├── hosts ├── roles │ ├── VPC │ │ └── tasks │ │ ├── main.yml # VPC.ymlの本体 │ │ ├── setup_ec2.yml # VPC.ymlの本体 │ │ └── setup_vpc.yml # VPC.ymlの本体 │ └── vpc_peering │ └── tasks │ ├── main.yml # VPC.ymlの本体 │ └── vpc_peering.yml # create_vpc_peering.ymlの本体 ├── create_vpc_peering.yml # 実行するplaybook └── VPC.yml # 実行するplaybook |
group_vars/all.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
PEERING_VPC_ID: vpc-XXXXXXXX # お客様のVPC ID PEERING_VPC_CIDR: XXX.XXX.XXX.XXX/XX # お客様のVPC CIDR CUSTOMER: Coati Coffee.break.fun # お客様名 EMAIL: COATI@coati.coffee_break_fun.com # お客様のEメールアドレス PREFIX: "CoatiManager_{{ CUSTOMER }}" # VPC, Subnet, ルートテーブル, インスタンス名のprefix REGION: ap-northeast-1 # リージョン(東京) OFFICE_IP: ZZZ.ZZZ.ZZZ.ZZZ # SIOSのオペレーション端末のIPアドレス OFFICE_IP_CIDR: "{{ OFFICE_IP }}/32" # SIOSのオペレーション端末のIPアドレスのCIDR VPC_NAME: "{{ PREFIX }}_VPC" # VPC名称 VPC_CIDR_LEFT: YYY.YYY.YYY # Coati ManagerのCIDRの左部分 VPC_CIDR_BLOCK: "{{ VPC_CIDR_LEFT }}.0/23" # VPCのCIDR Block SUBNET1_NAME: "{{ PREFIX }}_SUBNET1" # Subnet名 SUBNET1_CIDR_BLOCK: "{{ VPC_CIDR_LEFT }}.0/24" # SubnetのCIDR Block ROUTETABLE1_NAME: "{{ PREFIX }}_RT1" # ルートテーブル名 KEY_PAIR: XXXXXXXXXXXXXXXXXXXXXXXXXXX # Coati Managerインスタンスにアクセスするためのキーペア INSTANCE_TYPE: t2.small # Coati Managerインスタンスのインスタンス・タイプ EC2_NAME: "{{ PREFIX }}" # Coati Managerインスタンス名 EC2_PRIVATE_IP: "{{ VPC_CIDR_LEFT }}.10" # Coati ManagerインスタンスのプライベートIPアドレス IAM_ROLE: XXXXXXXXXXXX # Coati Managerインスタンスに割り当てるIAMロール SIOS_ACCOUNT_ID: 987654321098 # サイオスのAWSアカウントID |
作成したVPC内にEC2インスタンスを作成するplaybook, setup_ec2.ymlの中身を見ていきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
- ec2_remote_facts: region: "{{ REGION }}" filters: private-ip-address: "{{ EC2_PRIVATE_IP }}" subnet-id: "{{ SUBNET_ID }}" register: ec2 # Search for the AMI tagged "Role: CoatiManager" and "Version: Master" - ec2_ami_find: owner: self region: "{{ REGION }}" ami_tags: Role: CoatiManager Version: Master sort: creationDate sort_order: descending sort_end: 1 no_result_action: fail register: ami_find - name: Create AWS Instance ec2: key_name: "{{ KEY_PAIR }}" instance_type: "{{ INSTANCE_TYPE }}" image: "{{ ami_find.results[0].ami_id }}" region: "{{ REGION }}" vpc_subnet_id: "{{ SUBNET_ID }}" private_ip: "{{ EC2_PRIVATE_IP }}" instance_profile_name: "{{ IAM_ROLE }}" assign_public_ip: no wait: yes instance_tags: Name: "{{ EC2_NAME }}" Role: CoatiManager volumes: - device_name: /dev/sda1 volume_type: gp2 volume_size: 20 delete_on_termination: true when: not ec2.instances # refresh remote facts - ec2_remote_facts: region: "{{ REGION }}" filters: private-ip-address: "{{ EC2_PRIVATE_IP }}" subnet-id: "{{ SUBNET_ID }}" register: ec2 - name: Associate EIP for instance ec2_eip: region: "{{ REGION }}" device_id: "{{ ec2.instances[0].id }}" reuse_existing_ip_allowed: yes in_vpc: yes register: eip |
順にみていきます。
1 2 3 4 5 6 7 8 |
- ec2_remote_facts: region: "{{ REGION }}" filters: private-ip-address: "{{ EC2_PRIVATE_IP }}" subnet-id: "{{ SUBNET_ID }}" register: ec2 |
ec2_remote_factsはAWS内の(複数の)インスタンスの情報を集めるモジュールです。
IPアドレス・サブネット名など多種のFilterを設定して検索することができます。
今回は、SUBNET_IDのサブネット、EC2_PRIVATE_IPのIPアドレスのインスタンスが、環境内に既に存在しているかどうかをチェックしています。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
なお、<span style="font-family: georgia, palatino, serif;"><strong>ec2_remote_facts</strong></span> は ansible 2.4 では<strong>非推奨</strong>モジュールとなっており、代わりに <strong><span style="font-family: georgia, palatino, serif;"><a href="http://docs.ansible.com/ansible/latest/ec2_instance_facts_module.html#ec2-instance-facts">ec2_instance_facts</a></span></strong> がリリースされています。 - ec2_ami_find: owner: self region: "{{ REGION }}" ami_tags: Role: CoatiManager Version: Master sort: creationDate sort_order: descending sort_end: 1 no_result_action: fail register: ami_find |
続いて、AMIの検索です。
Coati開発チームでは、Coati Manager用のAMIをあらかじめ用意していて、タグ Role=CoatiManager をつけています。ec2_ami_findモジュールを利用して、タグをキーにしてAMIのIDを取得します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
- name: Create AWS Instance ec2: key_name: "{{ KEY_PAIR }}" instance_type: "{{ INSTANCE_TYPE }}" image: "{{ ami_find.results[0].ami_id }}" region: "{{ REGION }}" vpc_subnet_id: "{{ SUBNET_ID }}" private_ip: "{{ EC2_PRIVATE_IP }}" instance_profile_name: "{{ IAM_ROLE }}" assign_public_ip: no wait: yes instance_tags: Name: "{{ EC2_NAME }}" Role: CoatiManager volumes: - device_name: /dev/sda1 volume_type: gp2 volume_size: 20 delete_on_termination: true when: not ec2.instances |
いよいよ、インスタンスの作成です。そのものズバリな ec2モジュールを使います。
パラメータはAWS Management ConsoleでEC2インスタンスを作成する際にお馴染みな面々を指定していきます。imageには、さきほど検索したAMIのIDを指定しています。
1 |
<span style="font-size: 12pt; font-family: arial, helvetica, sans-serif;"><code> image: "{{ ami_find.results[0].ami_id }}"</code></span> |
instance_tags: で Name=名前, Role=CoatiManager、というタグ付けもしています。
最後の
1 |
<span style="font-size: 12pt; font-family: arial, helvetica, sans-serif;"><code> when: not ec2.instances</code></span> |
は、ec2.instancesがある場合にはインスタンスの作成をしない、という条件判定です。
一番最初に ec2_remote_factモジュールで同じIPアドレスとサブネットのインスタンスを検索したときに、もし既に存在している場合には変数ec2.instancesなんらかの値が入っているはずですので、その場合にはインスタンスの作成は行わないようにしています。
VPCピアリングを行うplaybook, vpc_peering.ymlです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
--- - name: include vars include_vars: '/tmp/__temp_fact__.yml' - name: Setup VPC peering for customer ec2_vpc_peer: region: "{{ REGION }}" vpc_id: "{{ manager_vpc_id }}" peer_vpc_id: "{{ PEERING_VPC_ID }}" peer_owner_id: "{{ PEER_OWNER_ID }}" state: present tags: Name: Peering connection for "{{ CUSTOMER }}" register: vpc_peering_for_customer |
ec2_vpc_peerモジュールを使い、接続するVPCをそれぞれ指定して、VPCピアリング接続を行います。
このplaybookを実行したあと、お客様に接続を承認していただくと、晴れて両VPCがピアリング接続されることになります。
ついに、Coati環境が完成しました!
いかがでしたでしょうか?
今後も、役に立ちそうなplaybookを紹介していく予定ですので、よろしくお願いします。
シルバーウィークに遅い夏休みを満喫した沼野井です。
・・・本当はこれ書いてるの9/14なんですが、公開日の調整の結果↑にような挨拶となりました。
こんな笑点みたいな挨拶をする機会があるとは思ってもいませんでした。
この構成をansibleで作ろう、と思うCoatiだった・・・
というわけで、前回の続きで、ansibleによるEC2・VPC環境構築の、実際のplaybookの内容についてです。
あ、前回すっかり忘れていたのですが、使用しているansibleのバージョンは 2.3 です。ご承知おきください。
Coatiチームで使用している、環境デプロイ用のPlaybookのディレクトリ構成は以下のようになっています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
├── ansible.cfg ├── group_vars │ └── all.yml # 変数定義 ├── hosts ├── roles │ ├── VPC │ │ └── tasks │ │ ├── main.yml # VPC.ymlの本体 │ │ ├── setup_ec2.yml # VPC.ymlの本体 │ │ └── setup_vpc.yml # VPC.ymlの本体 │ └── vpc_peering │ └── tasks │ ├── main.yml # VPC.ymlの本体 │ └── vpc_peering.yml # create_vpc_peering.ymlの本体 ├── create_vpc_peering.yml # 実行するplaybook └── VPC.yml # 実行するplaybook |
なんぞこれ? と目がハテナな方はこちら(英語ですが)
ansibleの標準的なディレクトリ構成と思っていただいてよいと思います。
個々に中身をご紹介していきます。
(1) group_vars/all.yml
全playbookで使う変数を定義しています。以下のような感じです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<span style="font-size: 10pt; font-family: arial, helvetica, sans-serif;"><code> PEER_OWNER_ID: 123456789012 # お客様のAWSアカウント PEERING_VPC_ID: vpc-XXXXXXXX # お客様のVPC ID PEERING_VPC_CIDR: XXX.XXX.XXX.XXX/XX # お客様のVPC CIDR CUSTOMER: Coati Coffee.break.fun # お客様名 EMAIL: COATI@coati.coffee_break_fun.com # お客様のEメールアドレス PREFIX: "CoatiManager_{{ CUSTOMER }}" # VPC, Subnet, ルートテーブル, インスタンス名のprefix REGION: ap-northeast-1 # リージョン(東京) OFFICE_IP: ZZZ.ZZZ.ZZZ.ZZZ # SIOSのオペレーション端末のIPアドレス OFFICE_IP_CIDR: "{{ OFFICE_IP }}/32" # SIOSのオペレーション端末のIPアドレスのCIDR VPC_NAME: "{{ PREFIX }}_VPC" # VPC名称 VPC_CIDR_LEFT: YYY.YYY.YYY # Coati ManagerのCIDRの左部分 VPC_CIDR_BLOCK: "{{ VPC_CIDR_LEFT }}.0/23" # VPCのCIDR Block SUBNET1_NAME: "{{ PREFIX }}_SUBNET1" # Subnet名 SUBNET1_CIDR_BLOCK: "{{ VPC_CIDR_LEFT }}.0/24" # SubnetのCIDR Block ROUTETABLE1_NAME: "{{ PREFIX }}_RT1" # ルートテーブル名 KEY_PAIR: XXXXXXXXXXXXXXXXXXXXXXXXXXX # Coati Managerインスタンスにアクセスするためのキーペア INSTANCE_TYPE: t2.small # Coati Managerインスタンスのインスタンス・タイプ EC2_NAME: "{{ PREFIX }}" # Coati Managerインスタンス名 EC2_PRIVATE_IP: "{{ VPC_CIDR_LEFT }}.10" # Coati ManagerインスタンスのプライベートIPアドレス IAM_ROLE: XXXXXXXXXXXX # Coati Managerインスタンスに割り当てるIAMロール SIOS_ACCOUNT_ID: 987654321098 # サイオスのAWSアカウントID </code></span> |
冒頭の5行の、
1 2 3 4 5 6 7 |
<span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><code> PEER_OWNER_ID: 123456789012 # お客様のAWSアカウント PEERING_VPC_ID: vpc-XXXXXXXX # お客様のVPC ID PEERING_VPC_CIDR: XXX.XXX.XXX.XXX/XX # お客様のVPC CIDR CUSTOMER: Coati Coffee.break.fun # お客様名 EMAIL: COATI@coati.coffee_break_fun.com # お客様のEメールアドレス </code></span> |
はお客様のお申込み内容に従って入力しています。残りの項目は固定値または、↑の内容から自動的に作成されるという寸法です。
(2) VPC.yml
VPCを作成して、その中にCoati Managerインスタンスを作成するplaybookです。インスタンスも作るのにVPC.ymlってネーミングがイマイチです。すみません。
そのネーミングのイマイチなVPC.ymlの中身はこんな感じです。
1 2 3 4 5 6 7 8 |
<span style="font-size: 10pt; font-family: arial, helvetica, sans-serif;"><code> --- - name: setup localhost hosts: localhost roles: - VPC </code></span> |
こんだけ。
これは、「ロールVPCのタスクを実行しなさいね」と言ってるだけです。
ロールVPCのタスクとは
1 2 3 4 5 6 7 8 |
├── roles │ ├── VPC │ │ └── tasks │ │ ├── main.yml # VPC.ymlの本体 │ │ ├── setup_ec2.yml # VPC.ymlの本体 │ │ └── setup_vpc.yml # VPC.ymlの本体 |
こいつらです。実際にはmain.ymlがまず呼ばれます。
main.ymlは
1 2 3 4 5 |
<span style="font-size: 10pt; font-family: arial, helvetica, sans-serif;"><code> --- - include: setup_vpc.yml - include: setup_ec2.yml SUBNET_ID="{{ subnet.subnet.id }}" </code></span> |
これもこんだけで、本体であるsetup_vpc.ymlとsetup_ec2.ymlをincludeしています。
本体の1つ、setup_vpc.ymlの中身を見ていきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
<span style="font-size: 10pt; font-family: arial, helvetica, sans-serif;"><code> --- - name: Gather ec2 facts ec2_facts: register: ec2_facts - name: Setup VPC ec2_vpc_net: name: "{{ VPC_NAME }}" cidr_block: "{{ VPC_CIDR_BLOCK }}" dns_hostnames: yes dns_support: yes region: "{{ REGION }}" tags: Role: CoatiManager register: vpc - name: Setup subnet ec2_vpc_subnet: resource_tags: Name: "{{ SUBNET1_NAME }}" cidr: "{{ SUBNET1_CIDR_BLOCK }}" region: "{{ REGION }}" vpc_id: "{{ vpc.vpc.id }}" register: subnet - name: Setup internet gateway ec2_vpc_igw: region: "{{ REGION }}" vpc_id: "{{ vpc.vpc.id }}" register: igw - name: Setup route table ec2_vpc_route_table: resource_tags: Name: "{{ ROUTETABLE1_NAME }}" region: "{{ REGION }}" vpc_id: "{{ vpc.vpc.id }}" subnets: - "{{ subnet.subnet.id }}" routes: - dest: 0.0.0.0/0 gateway_id: "{{ igw.gateway_id }}" register: route_table - name: create temporary file lineinfile: dest: "/tmp/__temp_fact__.yml" create: yes line: "---\n" - name: write vpc_id into temporary file lineinfile: dest: "/tmp/__temp_fact__.yml" line: "manager_vpc_id: {{ vpc.vpc.id }}\n" - name: write route table id into temporary file lineinfile: dest: "/tmp/__temp_fact__.yml" line: "manager_route_table_id: {{ route_table.route_table.id }}\n" - name: Setup security group. part 1 ec2_group: name: default description: default VPC security group region: "{{ REGION }}" vpc_id: "{{ vpc.vpc.id }}" purge_rules: yes rules: - proto: tcp from_port: 80 to_port: 80 cidr_ip: 0.0.0.0/0 - proto: tcp from_port: 22 to_port: 22 cidr_ip: "{{ OFFICE_IP_CIDR }}" - proto: tcp from_port: 22 to_port: 22 cidr_ip: "{{ ec2_facts.ansible_facts.ansible_ec2_public_ipv4 }}/32" - proto: icmp from_port: -1 to_port: -1 cidr_ip: "{{ OFFICE_IP_CIDR }}" - proto: icmp from_port: -1 to_port: -1 cidr_ip: "{{ ec2_facts.ansible_facts.ansible_ec2_public_ipv4 }}/32" changed_when: False register: default_sg - name: Setup security group. part 2 ec2_group: name: default description: default VPC security group region: "{{ REGION }}" vpc_id: "{{ vpc.vpc.id }}" purge_rules: no rules: - proto: all from_port: 0 to_port: 65535 group_id: "{{ default_sg.group_id }}" changed_when: False </code></span> |
一転して長くなりましたね。本体ですから、実際いろいろやってます。順にみていきますと、
1 2 3 4 5 |
<span style="font-size: 10pt; font-family: arial, helvetica, sans-serif;"><code> - name: Gather ec2 facts ec2_facts: register: ec2_facts </code></span> |
ec2_factsは(playbookを実行している)ホストの情報を色々集めてくるモジュールです。今回は、パブリックIPアドレスを取得するため(だけ)に使っています。
registerは、集めたネタを変数(この場合 変数名もec2_facts)に格納します。
実際にec2_factsを使っているところは後ほど!
1 2 3 4 5 6 7 8 9 10 11 12 |
<span style="font-size: 10pt; font-family: arial, helvetica, sans-serif;"><code> - name: Setup VPC ec2_vpc_net: name: "{{ VPC_NAME }}" cidr_block: "{{ VPC_CIDR_BLOCK }}" dns_hostnames: yes dns_support: yes region: "{{ REGION }}" tags: Role: CoatiManager register: vpc </code></span> |
Coati Managerインスタンスを格納するVPCを作成しています。ec2_vpc_netモジュールを使います。
{{ VPC_NAME }} は group_vars/all.ymlで定義してあるものです(ansibleでは、”{{}}”で囲むと変数の値が取り出せます)
同様に{{VPC_CIDR_BLOCK}}, {{ REGION }}もgroup_vars/all.ymlで定義しています。
あと、tags:を利用して、 作成したVPCにRole=CoatiManager というタグをつけています。
ec2_vpc_netモジュールは、作成したVPCのIDなどを実行時の結果として戻しますので、実行結果をregisterで変数vpcに格納しておいて、あとで使うことができます。
1 2 3 4 5 6 7 8 9 10 |
<span style="font-size: 10pt; font-family: arial, helvetica, sans-serif;"><code> - name: Setup subnet ec2_vpc_subnet: resource_tags: Name: "{{ SUBNET1_NAME }}" cidr: "{{ SUBNET1_CIDR_BLOCK }}" region: "{{ REGION }}" vpc_id: "{{ vpc.vpc.id }}" register: subnet </code></span> |
作ったVPCにsubnetを作成します。今作ったVPC IDが必要です。ここで、さきほどregisterで保存した変数vpcを使います。VPC IDは、
1 2 3 4 |
<span style="font-size: 10pt; font-family: arial, helvetica, sans-serif;"><code> vpc_id: "{{ vpc.vpc.id }}" </code></span> |
のようにして参照できます。
1 2 3 4 5 6 7 |
<span style="font-size: 10pt; font-family: arial, helvetica, sans-serif;"><code> - name: Setup internet gateway ec2_vpc_igw: region: "{{ REGION }}" vpc_id: "{{ vpc.vpc.id }}" register: igw </code></span> |
同様に、作成したVPCにインターネットゲートウェイを作成しています。ec2_vpc_igwモジュールを使います。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span style="font-size: 10pt; font-family: arial, helvetica, sans-serif;"><code>- name: Setup route table ec2_vpc_route_table: resource_tags: Name: "{{ ROUTETABLE1_NAME }}" region: "{{ REGION }}" vpc_id: "{{ vpc.vpc.id }}" subnets: - "{{ subnet.subnet.id }}" routes: - dest: 0.0.0.0/0 gateway_id: "{{ igw.gateway_id }}" register: route_table </code></span> |
ルートテーブルの作成です。ec2_vpc_route_tableモジュールを使います。ちょっとパラメータが増えましたが、基本は同じです。
なお、ここでは送信先を0.0.0.0/0にしていますが、適切に設定してくださいね!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<span style="font-size: 10pt; font-family: arial, helvetica, sans-serif;"><code> - name: Setup security group. part 1 ec2_group: name: default description: default VPC security group region: "{{ REGION }}" vpc_id: "{{ vpc.vpc.id }}" purge_rules: yes rules: - proto: tcp from_port: 80 to_port: 80 cidr_ip: 0.0.0.0/0 - proto: tcp from_port: 22 to_port: 22 cidr_ip: "{{ OFFICE_IP_CIDR }}" - proto: tcp from_port: 22 to_port: 22 cidr_ip: "{{ ec2_facts.ansible_facts.ansible_ec2_public_ipv4 }}/32" - proto: icmp from_port: -1 to_port: -1 cidr_ip: "{{ OFFICE_IP_CIDR }}" - proto: icmp from_port: -1 to_port: -1 cidr_ip: "{{ ec2_facts.ansible_facts.ansible_ec2_public_ipv4 }}/32" changed_when: False register: default_sg </code></span> |
セキュリティグループの作成です。ec2_groupモジュールを使います。以下のポートに穴をあけています。
ポート番号 | ソース | 備考 |
80 | 0.0.0.0/0(※) | |
22 | 手元のPCのIPアドレス | 私がオフィスから直接ログインして操作するため |
22 | オペレーション端末のIPアドレス | オペレーション端末からログインして色々設定するため |
-1 | 手元のPCのIPアドレス | ICMPです。ping用 |
-1 | オペレーション端末のIPアドレス | ICMPです。ping用 |
※ここでは0.0.0.0/0と書いてありますが、適切に設定してくださいね!
「オペレーション端末の(グローバル)IPアドレス」の指定に、最初の方で取っておいたec2_factsを使っています。
やっと伏線回収できました!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span style="font-size: 10pt; font-family: arial, helvetica, sans-serif;"><code> - name: Setup security group. part 2 ec2_group: name: default description: default VPC security group region: "{{ REGION }}" vpc_id: "{{ vpc.vpc.id }}" purge_rules: no rules: - proto: all from_port: 0 to_port: 65535 group_id: "{{ default_sg.group_id }}" changed_when: False </code></span> |
セキュリティグループ構築その2、です。同じセキュリティグループに割り当てられたインスタンスからのトラフィックを許可しています。
・・・いかがでしたでしょうか。ここまでで、
の構成ができました。
少し長くなってしまったので、Coati Managerインスタンスの作成とVPC Peeringの設定は次回にしようと思います!
未だにNintendo Switchが買えないSIOS Coati 開発チーム、沼野井です。もうニンゴジラは見たくない!
本日は、AnsibleによるAWS EC2・VPC環境構築についてお話ししたいと思います。
コマンドベースの環境設定ツールとしてすっかりメジャーになったAnsible。Playbookというファイルにお願いごとを書いておいてコマンドを実行すると、夜までまたずともすぐに叶えてくれる、7人の小人以上よりも便利なナイスガイ。
近年、開発元を赤い帽子のあの会社が買収するなどメジャー街道を驀進中です。
私は、Ansibleの
が気に入っています。
Ansibleの公式ドキュメントはこちらをどうぞ。
公式ドキュメントは英語なんですが、日本語の書籍やWebサイトが多く存在します。それもまた良いところですね。
実は、何を隠そう、SIOS Coatiでも、本番環境・テスト環境の構築をAnsibleで行っています!
まぁ、ここまで前振りして使ってなかったら逆にびっくりですが…
というわけで、今回は、SIOS Coatiの監視環境を自動作成するのに使っている、AWS用のモジュールについてご紹介します。
AWSで繰り返し環境を構築したいけどマニュアルでやってるよ・・という方の参考となれば幸いです。
SIOS Coatiの基本的な監視環境は、以下の通りです。
作業と、それに使うモジュールは以下の通りです。
Coati ManagerのVPC関連の設定
# | 作業 | モジュール名 |
1 | VPCの設定 | ec2_vpc_net |
2 | subnetの設定 | ec2_vpc_subnet |
3 | ルートテーブルの設定 | ec2_vpc_route_table |
4 | インターネットゲートウェイの設定 | ec2_vpc_igw |
5 | セキュリティグループの設定 | ec2_group |
Coati ManagerのEC2インスタンスの設定
# | 作業 | モジュール名 |
6 | インスタンスの設定 | ec2 |
Coati ManagerのVPC Peeringインスタンスの設定
# | 作業 | モジュール名 |
7 | VPC Peeringの設定 | ec2_vpc_peer |
Ansibleの標準モジュールだけで一通りできてしまって、楽チンなことこの上ありません!
上記のほかにも、様々なモジュールが用意されていますよ!
本日ご紹介したモジュールを利用した、実際のplaybookについては、次回ご説明したいと思います。
最後にまた宣伝で恐縮ですが
弊社では、Ansibleについてのテクニカルサポートをご提供しております!