「Amazon EC2でCentOS6を使用するときのハマリポイント」をAnsibleのPlaybookにしてみた
前提
クラスメソッドさんのこちらの記事の内容をAnsibleのPlaybookにしてみました。
AWSのEC2インスタンスはこちらをベースにすることを想定しています。
Ansible Playbook
Ansible 1.9.2で確認しました。インベントリファイルではsshの鍵認証方式でrootでアクセスするよう設定することを想定しています。デフォルトのユーザ名とパスワードは適宜変更してください。冪等性はちょっと微妙かもしれません。
---
- hosts: all
vars:
default_username: sadapon2008
default_password: sadapon2008
is_growroot: true
remote_user: root
tasks:
- name: get encrypted password
local_action: command openssl passwd -salt salty -1 {{ default_password }}
register: encrypted_password
- name: yum update
command: yum -y update
- name: install libselinux-python
yum: >
name=libselinux-python
state=present
- name: disable selinux 1
selinux: >
state=disabled
- name: disable selinux 2
shell: if [ "`/usr/sbin/getenforce`" != "Disabled" ]; then /usr/sbin/setenforce 0; fi
- name: set locale to /etc/sysconfig/i18n
replace: >
dest=/etc/sysconfig/i18n
regexp='^LANG=\"en_US\.UTF-8\"'
replace='LANG="ja_JP.UTF-8"'
- name: set zone to /etc/sysconfig/clock
replace: >
dest=/etc/sysconfig/clock
regexp='^ZONE=\"UTC\"'
replace='ZONE="Asia/Tokyo"'
- name: set localtime
file: >
src=/usr/share/zoneinfo/Asia/Tokyo
dest=/etc/localtime
state=link
force=yes
- name: reboot
command: shutdown -r now
- name: wait for SSH port down
local_action: wait_for host={{ inventory_hostname }} port=22 state=stopped
- name: wait for SSH port up
wait_for: host={{ inventory_hostname }} port=22 state=started delay=30
delegate_to: 127.0.0.1
- name: install epel
yum: >
name=http://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
state=present
- name: install dracut-modules-growroot
yum: >
name=dracut-modules-growroot
state=present
when: is_growroot
- name: growroot
shell: dracut --force --add growroot /boot/initramfs-$(uname -r).img
when: is_growroot
- name: reboot
command: shutdown -r now
when: is_growroot
- name: wait for SSH port down
local_action: wait_for host={{ inventory_hostname }} port=22 state=stopped
when: is_growroot
- name: wait for SSH port up
wait_for: host={{ inventory_hostname }} port=22 state=started delay=30
delegate_to: 127.0.0.1
when: is_growroot
- name: resize2fs /dev/xvda1
command: resize2fs /dev/xvda1
when: is_growroot
- name: install cloud-init
yum: >
name=cloud-init
state=present
- name: add group
group: >
name={{ default_username }}
- name: add user
user: >
name={{ default_username }}
group={{ default_username }}
password={{ encrypted_password.stdout }}
- name: stop iptables/ip6tables
service: >
name={{ item }}
state=stopped
enabled=no
with_items:
- iptables
- ip6tables
- name: install python-setuptools
yum: >
name=python-setuptools
state=present
- name: easy_install pip
command: easy_install pip
- name: pip install awscli
command: pip install awscli
- name: install ntp
yum: >
name=ntp
state=present
- name: start ntp
service: >
name=ntpd
state=started
enabled=yes