LoginSignup
4
0

More than 1 year has passed since last update.

【AWS】ansibleで面倒な作業を自動化してみた

Posted at

デジタルキューブ & ヘプタゴン Advent Calendar 2022 の12/21分の投稿です。

仕事で何度か触る機会があったので、これを機にansibleの勉強をしてみようと思います。

ansibleとは

Ansibleとは、システム設定やソフトウェアの導入(インストール)などを自動化、効率化する構成管理ツールの一つ。Red Hat社が開発・販売しており、オープンソースソフトウェアとしても公開されている。

今まで手を動かしていた面倒な作業を自動化してくれるんだな〜ぐらいの認識です。
ansibleを利用する事で以下のようなメリットがあります。

  • チーム共有が可能
  • 同じ環境を作れる
  • 修正が簡単
  • コーヒーを飲む時間ができる

類似サービス

ChefやPuppetなどのサービスがあります。(独自言語のため管理が大変らしい)
興味があれば、以下のリンクから調べてみてください。

導入環境

今回は以下の環境で作業していきます。

[local]
- MacBook Air 2019 (intel)
- macOS Ventura 13.0.1
- brew 3.6.15
- ansible 2.13.6

[server]
- Amazon Linux 2
- t3.micro
- EIP関連付け済み(52.69.34.67)

brewが入ってないよ!っていう方はこちらの手順からインストールしてください。

事前準備

ansibleを使えるようにローカル環境の準備します。
brewを使えば簡単にインストールできます。

[local]
# ansibleをインストール
brew install ansible

# バージョン確認
ansible --version
ansible [core 2.13.6]

こんな感じで怒られた時はエラーの下に書いてあるものをインストールして、再度ansibleをインストールしてください。

# エラー内容
Error: python@3.11: the bottle needs the Apple Command Line Tools to be installed.

You can install them, if desired, with:
    xcode-select --install

# xcode-selectをインストール
xcode-select --install

# ansibleをインストール
brew install ansible

# バージョン確認
ansible --version
ansible [core 2.13.6]

初期設定

任意の場所にディレクトリを作ります。

# 任意の場所にディレクトリを作る
mkdir /hoge/ansible
cd /hoge/ansible

その後、設定ファイルを設置していきます。
構成は以下の通りです。

<プロジェクトディレクトリ>
└── ansible
    ├── hosts
    ├── site.yaml
    ├── ansible.cfg
    └── index.html
hosts
[aws]
52.69.34.67
site.yaml
# 対象の指定(インベントリファイルで指定した名前)
- hosts: aws

  # 管理者権限で実行
  become: true

  # 以下に実際の操作を記述
  tasks:

    # パッケージの更新
    - name: yum updated
      yum:
        name: "*"
        state: latest
    
    # タイムゾーンの変更
    - name: set timezone to Asia/Tokyo
      community.general.timezone:
        name: Asia/Tokyo
      become: yes

    # httpdのインストール
    - name: httpd installed
      yum:
        name: httpd
        state: installed
      become: yes

    # httpdの起動・自動起動設定
    - name: httpd start and automatic startup setting
      service:
        name: httpd
        state: started
        enabled: yes
      become: yes

    # リポジトリの有効化
    - name: enable to install php7.4
      shell: "amazon-linux-extras enable php7.4"
      changed_when: False

    # phpのインストール
    - name: php7.4 installed
      yum:
        name: php
        enablerepo: amzn2extra-php7.4
        state: present

    # index.htmlファイルを対象に配置
    - name: index.html deployed
      template:
        src: ./index.html
        dest: /var/www/html/index.html
        mode: 0644
ansible.cfg
[defaults]
remote_user = ec2-user
private_key_file = ~/.ssh/hogehoge.pem
host_key_checking = false
retry_files_enabled = false
inventory = ./hosts
interpreter_python = auto

[privilege_escalation]
become = true
index.html
ansible

実行してみる

# 設定に問題がないかチェック
ansible-playbook -i hosts site.yaml --syntax-check

# 実行
ansible-playbook site.yaml
実行結果
ansible-playbook site.yaml

PLAY [aws] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [52.69.34.67]

TASK [yum updated] *************************************************************
ok: [52.69.34.67]

TASK [set timezone to Asia/Tokyo] **********************************************
ok: [52.69.34.67]

TASK [httpd installed] *********************************************************
ok: [52.69.34.67]

TASK [httpd start and automatic startup setting] *******************************
ok: [52.69.34.67]

TASK [enable to install php7.4] ************************************************
ok: [52.69.34.67]

TASK [php7.4 installed] ********************************************************
ok: [52.69.34.67]

TASK [index.html deployed] *****************************************************
changed: [52.69.34.67]

PLAY RECAP *********************************************************************
52.69.34.67                : ok=8    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

スクリーンショット 2022-12-21 2.47.27.png

# date
Wed Dec 21 02:48:20 JST 2022

# httpd -v
Server version: Apache/2.4.54 ()
Server built:   Jun 30 2022 11:02:23

# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Active: active (running) since Wed 2022-12-21 02:37:06 JST; 11min ago

# php -v
PHP 7.4.33 (cli) (built: Nov 19 2022 00:22:13) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

感想

最初の学習コストはかかりますが、同じ構成なら他の環境でも流用できるので一回覚えてしまえば簡単に構築することができそうです。今回は基本的な操作になりましたが、今後は細かい設定周りやrolesを使ってplaybookを整理するなどもう少し深掘りしていきたいと思います。

参考

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