techtsubame’s blog

備忘録であり、何が起きても責任は取りません

RunCommandでAnsibleの実行(ApplyAnsible)

VPC

VPC

作成

セキュリティグループ

作成

ElasticIP

作成

エンドポイント

作成

Public

Subnet

PublicSubnet作成

InternetGateway

作成

VPCにアタッチ

RouteTable

作成

ルートにInternetGatewayを追加

サブネットの関連付け

Private

Subnet

PrivateSubnet作成

NatGateWay

作成

RouteTable

作成

サブネットの関連付け(PrivateSubnet)

IAM

ロール

作成

EC2

インスタンス

起動

サブネットはプライベートを指定

高度な詳細の最下部にあるユーザデータオプションにSSM-Agentのインストール処理を追記

ユーザデータに以下を記載

#!/bin/bash
cd /tmp
sudo dnf install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm
sudo systemctl enable amazon-ssm-agent
sudo systemctl start amazon-ssm-agent

テスト

SSM Inventory

インスタンスの確認

対象のインスタンスが表示されること

SSM Run Command (shell)

実施

結果確認

インスタンスidを押下

Ansible実行

手元PC

Playbookの作成(押下するとコード等が出ます)

.
└── playbook
    ├── roles
    │   └── httpd
    │       ├── tasks
    │       │   └── main.yml.      # Playbookの大元から呼ばれるRoleのPlaybook
    │       └── vars
    │           └── main.yml
    └── site.yml                     # Playbookの大元
find ./ -type f -name "*yml" | xargs -I{} -t cat -n {}
cat -n .//playbook/site.yml
     1  ---
     2  - name: Install httpd
     3    hosts: all
     4    gather_facts: true
     5    become: true
     6  
     7    tasks:
     8  #    - name: Show facts
     9  #      ansible.builtin.debug:
    10  #        var: ansible_facts
    11  
    12      - name: include role
    13        ansible.builtin.include_role:
    14          name: httpd
cat -n .//playbook/roles/httpd/vars/main.yml
     1  ---
     2  package:
     3    - name: httpd
     4      version: 2.4.53
cat -n .//playbook/roles/httpd/tasks/main.yml
     1  ---
     2  - name: install httpd package
     3    ansible.builtin.yum:
     4      name: "{{ item.name }}-{{ item.version }}"
     5      state: present
     6    with_items: "{{ package }}"

Playbookの圧縮

zip -r playbook.zip playbook
  adding: playbook/ (stored 0%)
  adding: playbook/site.yml (deflated 43%)
  adding: playbook/roles/ (stored 0%)
  adding: playbook/roles/httpd/ (stored 0%)
  adding: playbook/roles/httpd/vars/ (stored 0%)
  adding: playbook/roles/httpd/vars/main.yml (stored 0%)
  adding: playbook/roles/httpd/tasks/ (stored 0%)
  adding: playbook/roles/httpd/tasks/main.yml (deflated 28%)

IAM

ロール編集

S3にplaybookを置き、RunCommandでダウンロードするためS3のアクセス権を付与

S3

バケット作成

作成したPlaybookのzipをアップロード

アクセス先(オブジェクトURL)を取得

SSM

RunCommand

pathにアクセス先(オブジェクトURL)を取得で取得したパスを設定

Playbook Fileに作成したPlaybookを指定

結果確認

Failedが0であることを確認

EC2

セッションマネージャで接続し確認

[root@ip-10-1-1-124 ssm]# dnf history httpd
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

ID     | Command line                                      | Date and time    | Action(s)      | Altered
--------------------------------------------------------------------------------------------------------
     5 |                                                   | 2024-03-03 10:41 | Install        |   12
[root@ip-10-1-1-124 ssm]#
[root@ip-10-1-1-124 ssm]# dnf info httpd
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

Last metadata expiration check: 0:04:24 ago on Sun 03 Mar 2024 10:40:24 AM UTC.
Installed Packages
Name         : httpd
Version      : 2.4.53
Release      : 11.el9_2.5
Architecture : x86_64
Size         : 59 k
Source       : httpd-2.4.53-11.el9_2.5.src.rpm
Repository   : @System
From repo    : rhel-9-appstream-rhui-rpms
Summary      : Apache HTTP Server
URL          : https://httpd.apache.org/
License      : ASL 2.0
Description  : The Apache HTTP Server is a powerful, efficient, and extensible
             : web server.

Available Packages
Name         : httpd
Version      : 2.4.57
Release      : 5.el9
Architecture : x86_64
Size         : 52 k
Source       : httpd-2.4.57-5.el9.src.rpm
Repository   : rhel-9-appstream-rhui-rpms
Summary      : Apache HTTP Server
URL          : https://httpd.apache.org/
License      : ASL 2.0
Description  : The Apache HTTP Server is a powerful, efficient, and extensible
             : web server.

[root@ip-10-1-1-124 ssm]#

SSMログ確認

sudo grep -rn ansible /var/log/amazon/ssm/amazon-ssm-agent.log