LoginSignup
0
0

More than 1 year has passed since last update.

EC2で簡単にAnsibleの環境構築

Posted at

1.はじめに

下記記事を参考にAnsibleの環境構築を行いましたので手順を記事にしました。

2.EC2作成

1.EC2を2台作成します。
後の手順で使用するためEC2(ansible_Clientserver)作成時にキーペアの指定をお願いします。

インスタンス名 SGの開放ポート(IPアドレス)
ansible_server 22
ansible_Clientserver 22(ansible_serverのパブリックIPアドレス)
80(0.0.0.0/0)

3.Ansible環境構築

1.Ansibleインストール

sudo amazon-linux-extras install ansible2 -y

2.Ansibleのバージョン確認、インストールされている事を確認します。

ansible --version

3.ansible_Clientserverの秘密鍵をansible_serverの/home/ec2-user/.ssh/配下に格納します。

vi /home/ec2-user/.ssh/ansible_Clientserver-key.pem

4.秘密鍵の権限変更

chmod 600 /home/ec2-user/.ssh/ansible_Clientserver-key.pem

5.Ansibleのコードを格納するためのディレクトリを作成します。

cd && mkdir ansible-test && cd ansible-test

6.vi hostsで下記記述を追加し、{$ansible_Clientserver private ip address}の箇所を変更します。

# hosts
[server]
{$ansible_Clientserver private ip address}

[server:vars]
ansible_ssh_port=22
ansible_ssh_user=ec2-user
ansible_ssh_private_key_file=~/.ssh/ansible_Clientserver-key.pem
__working_user=ec2-user

7.vi site.ymlで下記記述を追加します。

# 対象の指定(インベントリファイルで指定した名前)
- hosts: server

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

  # 以下に実際の操作を記述していく
  tasks:

    # パッケージの更新
    - name: updated yum
      yum:
        name: "*"
        state: latest

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

    # httpdの起動・自動起動設定
    - name: httpd booted and auto boot conf
      service:
        name: httpd
        state: started
        enabled: yes
      become: yes

8.構文チェック

ansible-playbook -i hosts site.yml --syntax-check

9.Ansible実行

ansible-playbook -i hosts site.yml

10.ansible_ClientserverのパブリックIPアドレスでApacheのTest Pageが表示されれば成功です。
image.png

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