LoginSignup
2
3

More than 5 years have passed since last update.

EC2 Systems Manager からの Ansible Playbook の実行

Posted at

:beginner:
東京リージョンでも AWS-RunAnsiblePlaybook が使用出来るようになったようなので、
以下を参考に試しにEC2 System Manager から Ansible Playbook を実行してみます。
Running Ansible Playbooks using EC2 Systems Manager Run Command and State Manager

以下の作業を行います。
・特定のインスタンスへAnsibleをインストール
・Playbook を使った Apache のインストールと起動

内容としては初心者向けになります。

前提条件

SSMエージェントのインストールは実施済みであること。
:beginner: Amazon EC2 Simple Systems Manager (SSM) エージェントのインストール

対象インスタンスへのAnsible のインストール

EC2のSYSTEMS MANAGER SERVICE > 「コマンドを実行」で「AWS-RunShellScript」を選択。

2017-06-27-13-36-59.png

対象のインスタンスを選択します。試しに複数インスタンスを選択してみます。
「Commands」に以下を記入し「RUN」を選択します。

:warning: 以下はAmazon Linux 用になります。
CentOSやUbuntuの場合はyum or apt を使用してAnsible をインストールして下さい。

Commands
/usr/bin/pip install ansible
ansible --version

2017-06-27-13-50-31.png

2017-06-27-13-52-24.png

複数インスタンスを指定した場合はインスタンス毎に同じコマンドIDの履歴が表示されます。
2017-06-27-13-57-29.png

インスタンスごとに表示されるので複数インスタンスにRunCommandを実行した場合でも、
実行結果を各インスタンス単位で確認することが可能です。
2017-06-27-14-02-44.png

なお出力結果にpipのバージョンに関してERRORが出ていますがAnsibleのインストールは出来ています。

----------ERROR-------
You are using pip version 6.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

2017-06-27-14-11-11.png

ちなみに pip の更新も同時に行う場合は以下の設定でコマンドを実行すればOKです。

Commands
pip install --upgrade pip
pip install ansible
ansible --version

2017-06-27-14-16-19.png

Ansible Playbook の実行

EC2のSYSTEMS MANAGER SERVICE > 「コマンドを実行」で「AWS-RunAnsiblePlaybook」を選択します。

2017-06-27-16-51-00.png

Playbook に 以下のYAMLを記入します。
HTTPで取得可能な場所にplaybookがある場合は「playbookurl」にURLを入力してもOKです。

Playbook
- hosts: all
  become: true

  tasks:
    - name: gather ec2 facts
      action: ec2_facts

    - name: install apache on redhat or centos instances
      yum: name=httpd state=present
      when: ansible_os_family == "RedHat"

    - name: install apache on debian or ubuntu instances
      apt: name=apache2 state=present
      when: ansible_os_family == "Debian"

    - name: enable apache on startup and start service for redhat or centos
      service: name=httpd enabled=yes state=started
      when: ansible_os_family == "RedHat"

    - name: enable apache on startup and start service for debian or ubuntu
      service: name=apache2 enabled=yes state=started
      when: ansible_os_family == "Debian"

2017-06-28-14-20-01.png

出力の表示を確認すると以下のような結果が表示されてAnsibleが無事に実行されていることがわかります。

2017-06-28-14-38-43.png

実行結果の確認

インスタンスにSSHでログインしログとWebページが表示されることを確認できました。

/var/log/messages
Jun 28 05:20:20 ip-10-0-13-43 ansible-setup: Invoked with filter=* gather_subset=['all'] fact_path=/etc/ansible/facts.d gather_timeout=10
Jun 28 05:20:21 ip-10-0-13-43 ansible-ec2_facts: Invoked with url_password=NOT_LOGGING_PARAMETER force=False use_proxy=True url=None force_basic_auth=False http_agent=ansible-httpget url_username=None validate_certs=True
Jun 28 05:20:22 ip-10-0-13-43 ansible-yum: Invoked with name=['httpd'] list=None install_repoquery=True conf_file=None disable_gpg_check=False state=present disablerepo=None update_cache=False enablerepo=None exclude=None validate_certs=True installroot=/ skip_broken=False
Jun 28 05:20:25 ip-10-0-13-43 yum[23264]: Installed: apr-1.5.1-1.12.amzn1.x86_64
Jun 28 05:20:25 ip-10-0-13-43 yum[23264]: Installed: apr-util-1.4.1-4.17.amzn1.x86_64
Jun 28 05:20:25 ip-10-0-13-43 yum[23264]: Installed: httpd-tools-2.2.32-1.9.amzn1.x86_64
Jun 28 05:20:25 ip-10-0-13-43 yum[23264]: Installed: apr-util-ldap-1.4.1-4.17.amzn1.x86_64
Jun 28 05:20:25 ip-10-0-13-43 yum[23264]: Installed: httpd-2.2.32-1.9.amzn1.x86_64
Jun 28 05:20:27 ip-10-0-13-43 ansible-service: Invoked with name=httpd pattern=None enabled=True state=started sleep=None arguments= runlevel=default

今回はPrivate Subnet にインスタンスを作ってしまったので確認できれば良いと思いlynxを使って確認しました。

2017-06-28-15-56-06.png

ひとまず、Ansibleが実行できることまでは確認できたので次回はawslogsの設定などをやってみようかな

2
3
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
2
3