0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ansible-playbookをssm経由で実行する

Posted at

準備

  • ダイナミックインベントリといって、タグなどの識別子からIPやIDを取得することができ、それに対しansibleを実行できる
  • タグやセキュリティグループIDでもフィルタリングができる模様
aws_ec2.yaml
plugin: aws_ec2
regions:
  - ap-northeast-1
boto_profile: vamdemic

# フィルタの設定
filters:
  instance-state-name: running
  tag:Name: vamdemic-app-integration-dev-server


# グルーピングの設定
# タグ毎にEC2インスタンスのグループをまとめる
keyed_groups:
  - key: tags.Application
    prefix: tag_Name_
    separator: ""

# inventory_hostnameの設定項目の優先順位
# 上から優先され、取得できなければ下に下がっていく
# この例だと Nameタグがなければ `ip-address`(パブリックIPv4アドレス)を採用し、
# それも設定されていなければプライベートIPv4アドレスを採用する
hostnames:
  - tag:Name
  - ip-address # パブリックIPv4アドレス
  - private-ip-address

compose:
  # SSM Session Managerでホストに接続したい場合は以下のようにする
  ansible_host: instance_id
ec2.yaml
- name: EC2 dynamic inventory
  hosts: "vamdemic-app-integration-dev-server"

  vars:
    ansible_ssh_common_args: "-o StrictHostKeyChecking=no -o ProxyCommand='aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p''"
    ansible_user: ec2-user
    ansible_ssh_private_key_file: ~/.ssh/vamdemic_aws.pem

  tasks:
    - name: Print Name tag
      debug:
        msg: "{{ tags.Name }}"

実行

一覧取得

ansible-inventory -i aws_ec2.yaml --graph
@all:
  |--@aws_ec2:
  |  |--vamdemic-app-integration-dev-server
  |--@ungrouped:

実行

ansible-playbook -i aws_ec2.yaml ec2.yaml

参考

https://zenn.dev/ohsawa0515/articles/enable-ec2-dynamic-inventory-by-ansible
https://docs.ansible.com/ansible/latest/collections/amazon/aws/aws_ec2_inventory.html

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?