で、いよいよ初期設定かと。
流れとしては、
・libselinux-pythonインストール
・SELinix状態確認
・(SELinuxが有効なら)SELinux無効化
・hostname確認
・hostname変更
・(SELinixを無効化しらなら)再起動
・Server再起動開始待機
・Server起動待機
・hosts.allow追記
・hosts.deny追記
・rootでのalias(dir)設定
・EPELインストール
・REMIインストール
・ユーザー(conoha)作成
・ユーザー(conoha)でのalias(dir)設定
・ユーザー(conoha)でのsudo確認
・ユーザー(conoha)でのsudo追加
・ユーザー(conoha)でのpublicキー登録
・sshdでのPort番号変更
・sshdでのrootログイン拒否
・sshdでのPassword認証拒否
・firewalldインストール
・firewalld起動
・firewalldでのssh port追加
・firewalldでのDMZのssh削除
・firewalldでのEXTERNALDMZのssh削除
・firewalldでのhomeのssh削除
・firewalldでのinternalのssh削除
・firewalld再起動
・sshd再起動
とまあ、ここまで一気に行います。
で、これを行うPlaybookが下記です。
$ sudo vi /etc/ansible/conoha-test/01-initial.yml
- hosts: conoha-test
vars_files:
- /etc/ansible/conoha-test/vars/initial.yml
become: no
remote_user: root
tasks:
- name: libselinux-pythonインストール
yum: name=libselinux-python state=present
- name: SELinix状態確認
shell: getenforce
register: selinix_result
changed_when: false
- name: SELinux無効化
selinux: state=disabled
when: selinix_result.stdout != "Disabled"
- name: hostname確認
shell: grep {{ hname }} /etc/hostname
register: hostname_contents
changed_when: false
failed_when: false
- name: hostname変更
shell: echo {{ hname }} >/etc/hostname
when: hostname_contents.stdout.find("{{ hname }}") == -1
- name: SELinix無効化後再起動
shell: sleep 2s && shutdown -r now
async: 1
poll: 0
when: selinix_result.stdout != "Disabled"
- name: Server再起動開始待機
local_action: wait_for host={{ inventory_hostname }} port=22 state=stopped
when: selinix_result.stdout != "Disabled"
- name: Server起動待機
local_action: wait_for host={{ inventory_hostname }} port=22 delay=30
when: selinix_result.stdout != "Disabled"
- name: hosts.allow追加
lineinfile: >
dest=/etc/hosts.allow
line="sshd : {{ hssh }}"
- name: hosts.deny追加
lineinfile: >
dest=/etc/hosts.deny
line="sshd : ALL"
- name: rootのalias(dir)設定
lineinfile: >
dest=/root/.bashrc
line="alias dir='ls -al --color=auto'"
- name: rootのalias(dir)設定
lineinfile: >
dest=/root/.bashrc
line="alias dir='ls -al --color=auto'"
- name: EPELインストール
yum: name=epel-release state=present
- name: REMIインストール
yum: name="http://rpms.famillecollet.com/enterprise/remi-release-7.rpm"
- name: ユーザー追加
user:
name={{ uname }}
password={{ upass }}
uid={{ uid }}
state=present
- name: alias(dir)設定
lineinfile: >
dest=/home/{{ uname }}/.bashrc
line="alias dir='ls -al --color=auto'"
state=present
- name: sudo確認
shell: grep {{ uname }}$'\t'ALL=\(ALL\)$'\t'ALL /etc/sudoers
register: sudo_result
# failed_when: sudo_result.rc not in [ 0, 1 ]
ignore_errors: true
changed_when: false
- name: sudo追加
shell: sed -i -e 's/root\tALL=(ALL) \tALL/&\n{{ uname }}\tALL=(ALL)\tALL/g' /etc/sudoers
when: sudo_result.stdout.find("{{ uname }}") == -1
- name: publicキー登録
authorized_key:
user={{ uname }}
key="{{ lookup('file', ukeypath) }}"
state=present
- name: publicキー登録
authorized_key:
user={{ uname }}
key="{{ lookup('file', ukeypath) }}"
state=present
- name: Port番号変更
lineinfile:
dest=/etc/ssh/sshd_config
regexp="^#Port 22"
line="Port {{ sshport }}"
state=present
backrefs=yes #regexpでマッチされない場合は追加しない
- name: rootログイン拒否
lineinfile:
dest=/etc/ssh/sshd_config
regexp="^#PermitRootLogin yes"
line="PermitRootLogin no"
state=present
backrefs=yes #regexpでマッチされない場合は追加しない
- name: Password認証拒否
lineinfile:
dest=/etc/ssh/sshd_config
regexp="PasswordAuthentication yes"
line="PasswordAuthentication no"
state=present
backrefs=yes #regexpでマッチされない場合は追加しない
- name: firewalldインストール
yum: name=firewalld state=present
- name: firewalld起動
service: name=firewalld enabled=yes state=started
- name: ssh port追加
firewalld:
permanent=true
port="{{ sshport }}/tcp"
state=enabled # "enabled" of "disabled"
- name: DMZのssh削除
firewalld:
permanent=true
zone=dmz
service=ssh
state=disabled # "enabled" of "disabled"
- name: EXTERNALDMZのssh削除
firewalld:
permanent=true
zone=external
service=ssh
state=disabled # "enabled" of "disabled"
- name: homeのssh削除
firewalld:
permanent=true
zone=home
service=ssh
state=disabled # "enabled" of "disabled"
- name: internalのssh削除
firewalld:
permanent=true
zone=internal
service=ssh
state=disabled # "enabled" of "disabled"
- name: firewalld再起動
service: name=firewalld state=restarted
- name: sshd再起動
service: name=sshd state=restarted
で、ansibleの実行。
$ sudo ansible-playbook /etc/ansible/conoha-test/01-initial.yml -k SSH password:
とSSHパスワードの入力を求められるので、ConoHaのrootパスワードを入力しましょう。
あとは、眺めているだけで、初期設定が完了してくれるかと。
次は、ミドルウェアインストールかな。
まとめページは、こちらです。
コメント