Last active
August 29, 2015 14:06
-
-
Save kaz-tk/b991f0b18305905aadd0 to your computer and use it in GitHub Desktop.
bash update
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
_ANSIBLE_BIN=`which ansible-playbook` | |
_INVENTORY_FILE="./inventory" | |
_PLAYBOOK_FILE="bash_update.yml" | |
############################### | |
# pre-check for your environment variable. | |
############################### | |
if [ -z ${_ANSIBLE_BIN} ];then | |
echo "- ERROR -" | |
echo "Ansible Binary is not found." | |
echo "please install ansible" | |
echo " or forget to workon / source .virtualenv/ansible/bin/activate" | |
exit 1 | |
fi | |
if [ -z ${_INVENTORY_FILE} ];then | |
echo "- ERROR -" | |
echo "is not found." | |
echo "please check your filename: " ${_INVENTORY_FILE} | |
exit 2 | |
fi | |
if [ -z ${_PLAYBOOK_FILE} ];then | |
echo "- ERROR -" | |
echo "playbook is not found." | |
echo "please check your filename: " ${_PLAYBOOK_FILE} | |
exit 2 | |
fi | |
function _answer_example(){ | |
echo "実行中に質問の意味は以下のとおりです。" | |
echo "心配な場合は、n/空でEnterを連打してください。" | |
echo "" | |
echo "y: YES! 次のステップに進む。" | |
echo "n: NO! 実行しないで次のステップに進む。" | |
echo "c: Continue. 次回からは質問しないで、継続Y実行" | |
echo "空Enter: NO! 実行しないで次のステップに進む。" | |
echo "" | |
} | |
_answer_example | |
############################### | |
# execute ansible. | |
############################### | |
${_ANSIBLE_BIN} -i ${_INVENTORY_FILE} ${_PLAYBOOK_FILE} --step -K | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- name: Bash のアップデート | |
hosts: all | |
sudo: True | |
vars: | |
- work_dir: /var/tmp/bash_update | |
- pkg_filename: | |
Debian: '{{ansible_machine}}.deb' | |
RedHat: '{{ansible_machine}}.rpm' | |
tasks: | |
########################### | |
# 事前作業 | |
########################### | |
- name: OS Family の確認 | |
debug: msg="this is {{ansible_os_family}}" | |
- name: ワークディレクトリの作成({{work_dir}}) | |
file: path={{work_dir}} | |
state=directory | |
- name: rpm の一覧の作成(RedHat) | |
shell: rpm -qa | tee -a {{work_dir}}/rpm_qa_`hostname`_before.log | |
when: ansible_os_family == 'RedHat' | |
- name: bash のバージョン確認(RedHat) | |
shell: rpm -q bash |tee -a {{work_dir}}/rpm_bash_`hostname`_before.log | |
when: ansible_os_family == 'RedHat' | |
- name: dpkg の一覧の作成(Debian) | |
shell: dpkg -l | tee -a {{work_dir}}/dpkg_bash_`hostname`_before.log | |
when: ansible_os_family == 'Debian' | |
- name: bash のバージョン確認(Debian) | |
shell: dpkg -l bash | tee -a {{work_dir}}/dpkg_bash_`hostname`_before.log | |
when: ansible_os_family == 'Debian' | |
- name: 転送するファイルの一覧を作成 | |
local_action: shell find files_to_{{ansible_os_family}}/ -type f | |
register: transfer_files | |
sudo: False | |
- debug: msg='{{item|basename}}' | |
with_items: transfer_files.stdout_lines | |
- name: ファイルを転送する | |
copy: src={{item}} | |
dest={{work_dir}}/{{item|basename}} | |
with_items: transfer_files.stdout_lines | |
########################### | |
# アップデート作業 | |
########################### | |
- name: 以下のファイルがインストールに使われます | |
debug: msg='{{work_dir}}/{{pkg_filename[ansible_os_family]}}' | |
- name: bash のアップデート | |
apt: name={{work_dir}}/{{pkg_filename[ansible_os_family]}} | |
state=present | |
register: result | |
ignore_errors: True | |
when: ansible_os_family == 'Debian' | |
- debug: msg='pkg_filename が空または誤っています。debパッケージ名を入力してください。例:bash-.{{ansible_machine}}.deb' | |
when: result|failed | |
- name: bash のアップデート | |
yum: name={{work_dir}}/{{pkg_filename[ansible_os_family]}} | |
state=present | |
register: result | |
ignore_errors: True | |
when: ansible_os_family == 'RedHat' | |
- debug: msg='pkg_filename が空または誤っています。rpmパッケージ名を入力してください。例:bash-.{{ansible_machine}}.rpm' | |
when: result|failed | |
########################### | |
# 事後作業 | |
########################### | |
- name: rpm の一覧の作成 | |
shell: rpm -qa | tee -a {{work_dir}}/rpm_qa_`hostname`_after.log | |
- name: bash のバージョン確認 | |
shell: rpm -q bash |tee -a {{work_dir}}/rpm_bash_`hostname`_after.log | |
- name: ログファイルのアーカイブ化 | |
shell: tar cvzf {{work_dir}}.tar.gz {{work_dir}} | |
- name: ログの収集 | |
fetch: flat=true | |
src={{work_dir}}.tar.gz | |
dest=./logs/{{inventory_hostname}}/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[redhat] | |
centos.local ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 ansible_ssh_user=vagrant ansible_ssh_password=vagrant | |
[debian] | |
debian.local ansible_ssh_host=127.0.0.1 ansible_ssh_port=2200 ansible_ssh_user=vagrant ansible_ssh_password=vagrant | |
ubuntu.local ansible_ssh_host=127.0.0.1 ansible_ssh_port=2201 ansible_ssh_user=vagrant ansible_ssh_password=vagrant |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment