はじめに
Amazon EC2インスタンス(Amazon Linux)にAnsibleをインストールし、AnsibleによりAmazon EC2インスタンスにyumで指定したパッケージをインストールする手順です。
環境
今回の手順で使用したAmazon EC2インスタンス(Amazon Linux)は以下のAMIで作成しました。
・Amazon Linux AMI 2016.03.3 (HVM), SSD Volume Type - ami-374db956
amzn-ami-hvm-2016.03.3.x86_64-gp2 (ami-374db956)
Amazon EC2インスタンスにインストールしたAnsibleのバージョンは以下になります。
・ansible-playbook 2.1.1.0
参考資料
以下の資料を参考にさせて頂きました。ありがとうございました。
yum - Manages packages with the yum package manager
https://docs.ansible.com/ansible/yum_module.html
Check Mode (“Dry Run”)
https://docs.ansible.com/ansible/playbooks_checkmode.html
構成管理ツール Ansible について
http://apatheia.info/blog/2013/04/06/about-ansible/
AmazonLinux に yum 経由で Ansible を入れても動かない
http://qiita.com/a-suenami/items/78be4677da597804b6e0
Amazon EC2へのAnsibleインストール手順
(1) AWSマネジメントコンソールへログインして、AnsibleをインストールするAmazon EC2インスタンス(Amazon Linux)を作成します。
(2) 作成したAmazon EC2インスタンスに対して、ec2-userでsshログインします。
ssh ec2-user@EC2インスタンスのIPアドレス
(3) Amazon EC2インスタンスのyumパッケージをアップデートします。
$ sudo su -
# yum -y update
(4) Amazon EC2インスタンスを再起動します。
今回の例ではAmazon EC2インスタンスのホスト名を以下のように変更します。
# vi /etc/sysconfig/network
(中略)
HOSTNAME=example-ansible-server
(中略)
Amazon EC2インスタンスを再起動します。
# reboot
Amazon EC2インスタンスが起動してきたら、再度ec2-userでsshログインします。
(5) Amazon EC2インスタンスのPythonのバージョンを確認します。
Anshibleを実行するマシンにはPythonがインストールされている必要があります。Amazon EC2インスタンスにPythonがインストールされている事を確認します。
(2016年9月25日時点では、EC2インスタンスに以下のバージョンのPythonがデフォルトでインストールされています)
[ec2-user@example-ansible-server ~]$ python --version
Python 2.7.12
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ uname -a
Linux example-ansible-server 4.4.19-29.55.amzn1.x86_64 #1 SMP Mon Aug 29 23:29:40 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
[ec2-user@example-ansible-server ~]$
(6) Amazon EC2インスタンスにpipをインストールします。
[ec2-user@example-ansible-server ~]$ sudo easy_install pip
Searching for pip
Best match: pip 6.1.1
Adding pip 6.1.1 to easy-install.pth file
Installing pip script to /usr/local/bin
Installing pip2.7 script to /usr/local/bin
Installing pip2 script to /usr/local/bin
Using /usr/lib/python2.7/dist-packages
Processing dependencies for pip
Finished processing dependencies for pip
[ec2-user@example-ansible-server ~]$
(7) Amazon EC2インスタンスにAansibleをインストールします。
Aansibleをインストールします。
[ec2-user@example-ansible-server ~]$ sudo pip install ansible
You are using pip version 6.1.1, however version 8.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting ansible
Downloading ansible-2.1.1.0.tar.gz (1.9MB)
100% |????????????????????????????????| 1.9MB 249kB/s
Requirement already satisfied (use --upgrade to upgrade): paramiko in /usr/lib/python2.7/dist-packages (from ansible)
Requirement already satisfied (use --upgrade to upgrade): jinja2 in /usr/lib/python2.7/dist-packages (from ansible)
Requirement already satisfied (use --upgrade to upgrade): PyYAML in /usr/lib64/python2.7/dist-packages (from ansible)
Requirement already satisfied (use --upgrade to upgrade): setuptools in /usr/lib/python2.7/dist-packages (from ansible)
Requirement already satisfied (use --upgrade to upgrade): pycrypto>=2.6 in /usr/lib64/python2.7/dist-packages (from ansible)
Requirement already satisfied (use --upgrade to upgrade): ecdsa>=0.11 in /usr/lib/python2.7/dist-packages (from paramiko->ansible)
Requirement already satisfied (use --upgrade to upgrade): markupsafe in /usr/lib64/python2.7/dist-packages (from jinja2->ansible)
Installing collected packages: ansible
Running setup.py install for ansible
Successfully installed ansible-2.1.1.0
[ec2-user@example-ansible-server ~]$
Aansibleがインストールされた事を確認します。
[ec2-user@example-ansible-server ~]$ which ansible
/usr/local/bin/ansible
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ ansible --version
ansible 2.1.1.0
config file =
configured module search path = Default w/o overrides
[ec2-user@example-ansible-server ~]$
Ansibleの設定
(8) Aansible操作対象マシンのIPアドレスリストを作成します。
AansibleをインストールしたAmazon EC2インスタンスに、Aansible操作対象マシンのIPアドレスリストを作成します。
[ec2-user@example-ansible-server ~]$ sudo mkdir /etc/ansible
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ sudo vi /etc/ansible/hosts
[local_node]
127.0.0.1
(9) Aansibleのplaybookを作成します。
AansibleでAmazon EC2インスタンスに指定したyumパッケージをインストールします。
今回の例ではEC2インスタンスに対して、yumでjq, sysstat(sarコマンド), git, gccをインストールします。
Ansibleのplaybookファイルを作成します。playbookファイルにインストールしたいyumのパッケージを記述します。
[ec2-user@example-ansible-server ~]$ vi /home/ec2-user/example_playbook.yml
---
- hosts:
- localhost
connection: local
tasks:
- name: update all packages
yum: name=* state=latest
become: yes
- name: install the latest version of gcc
yum: name=gcc state=latest
become: yes
- name: install the latest version of git
yum: name=git state=latest
become: yes
- name: install the latest version of jq
yum: name=jq state=latest
become: yes
- name: install the latest version of sysstat
yum: name=sysstat state=latest
become: yes
[ec2-user@example-ansible-server ~]$ cat /home/ec2-user/example_playbook.yml
---
- hosts:
- localhost
connection: local
tasks:
- name: update all packages
yum: name=* state=latest
become: yes
- name: install the latest version of gcc
yum: name=gcc state=latest
become: yes
- name: install the latest version of git
yum: name=git state=latest
become: yes
- name: install the latest version of jq
yum: name=jq state=latest
become: yes
- name: install the latest version of sysstat
yum: name=sysstat state=latest
become: yes
[ec2-user@example-ansible-server ~]$
(10) Aansibleのplaybookの構文をチェックします。
作成したplaybookファイルの構文をチェックし、問題ない事を確認します。
[ec2-user@example-ansible-server ~]$ ansible-playbook --syntax-check /home/ec2-user/example_playbook.yml
playbook: /home/ec2-user/example_playbook.yml
[ec2-user@example-ansible-server ~]$
(11) Aansibleのplaybookをテストモードで実行します。
Aansibleのplaybookをテストモードで実行します。テストモードの場合、実際にyumのパッケージはインストールされません。
インストール対象として指定したパッケージ名が表示される事を確認します。
[ec2-user@example-ansible-server ~]$ ansible-playbook --check /home/ec2-user/example_playbook.yml
PLAY [localhost] ***************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [update all packages] *****************************************************
ok: [localhost]
TASK [install the latest version of gcc] ***************************************
changed: [localhost]
TASK [install the latest version of git] ***************************************
changed: [localhost]
TASK [install the latest version of jq] ****************************************
changed: [localhost]
TASK [install the latest version of sysstat] ***********************************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=6 changed=4 unreachable=0 failed=0
[ec2-user@example-ansible-server ~]$
AnsibleでAmazon EC2インスタンスにyumで指定したパッケージをインストールする
(12) Aansibleのplaybookを実行し、Amazon EC2インスタンスにyumでパッケージをインストールします。
Aansibleのplaybookを実行し、EC2インスタンスに対して、yumでjq, sysstat, git, gccをインストールします。
まずインストール前の状態を確認します。
[ec2-user@example-ansible-server ~]$ rpm -qa | grep jq
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ rpm -qa | grep sysstat
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ rpm -qa | grep git
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ rpm -qa | grep gcc
libgcc48-4.8.3-9.109.amzn1.x86_64
[ec2-user@example-ansible-server ~]$
Aansibleのplaybookを実行し、Amazon EC2インスタンスにyumでパッケージをインストールします。
[ec2-user@example-ansible-server ~]$ ansible-playbook /home/ec2-user/example_playbook.yml
PLAY [localhost] ***************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [update all packages] *****************************************************
ok: [localhost]
TASK [install the latest version of gcc] ***************************************
changed: [localhost]
TASK [install the latest version of git] ***************************************
changed: [localhost]
TASK [install the latest version of jq] ****************************************
changed: [localhost]
TASK [install the latest version of sysstat] ***********************************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=6 changed=4 unreachable=0 failed=0
[ec2-user@example-ansible-server ~]$
(13) Amazon EC2インスタンスにyumパッケージがインストールされた事を確認します。
前述のAnsibleのplaybook実行により、Amazon EC2インスタンスにyumパッケージがインストールされた事を確認します。
(13-1) 再度Ansibleのplaybookを実行し、Amazon EC2インスタンスにyumパッケージがインストールされた事を確認します。
再度Ansibleのplaybookを実行し、Amazon EC2インスタンスにyumパッケージがインストールされた事を確認出来ます。
Ansibleには冪等性があるので、再度Playbookを実行しても、yumパッケージが既にインストールされている場合、インストールはスキップされます。
今回の例ではコマンド実行結果に「ok=6」と表示される事を確認します。
[ec2-user@example-ansible-server ~]$ ansible-playbook /home/ec2-user/example_playbook.yml
PLAY [localhost] ***************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [update all packages] *****************************************************
ok: [localhost]
TASK [install the latest version of gcc] ***************************************
ok: [localhost]
TASK [install the latest version of git] ***************************************
ok: [localhost]
TASK [install the latest version of jq] ****************************************
ok: [localhost]
TASK [install the latest version of sysstat] ***********************************
ok: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=6 changed=0 unreachable=0 failed=0
[ec2-user@example-ansible-server ~]$
(13-2) 手動でコマンドを実行し、Amazon EC2インスタンスにyumパッケージがインストールされた事を確認します。
Ansibleのplaybookを使用せず、手動でAmazon EC2インスタンスにyumパッケージがインストールされた事を確認する事も可能です。
以下のコマンドを実行し、yumで各パッケージがインストールされた事を確認出来ます。
[ec2-user@example-ansible-server ~]$ rpm -qa | grep jq
jq-libs-1.5-1.2.amzn1.x86_64
jq-1.5-1.2.amzn1.x86_64
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ rpm -qa | grep git
git-2.7.4-1.47.amzn1.x86_64
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ rpm -qa | grep sysstat
sysstat-9.0.4-27.10.amzn1.x86_64
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ rpm -qa | grep gcc
libgcc48-4.8.3-9.109.amzn1.x86_64
gcc48-4.8.3-9.109.amzn1.x86_64
gcc-4.8.3-3.20.amzn1.noarch
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ which jq
/usr/bin/jq
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ which git
/usr/bin/git
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ which sar
/usr/bin/sar
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ which gcc
/usr/bin/gcc
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ jq --version
jq-1.5
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ git --version
git version 2.7.4
[ec2-user@example-ansible-server ~]$
[ec2-user@example-ansible-server ~]$ sar -v
Linux 4.4.19-29.55.amzn1.x86_64 (example-ansible-server) 09/25/2016 _x86_64_ (1 CPU)
[ec2-user@example-ansible-server ~]$ gcc --version
gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[ec2-user@example-ansible-server ~]$
以上になります。