前言
Ansible 入門詳解參考等源於此
適用於自己的Ansible playbook
獲取阿里雲Ansible的源,安裝Ansible
Centos7
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
Centos6
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
安裝Ansible
yum -y install ansible
下載Tomcat
mkdir -p /data/ansible-script/ && cd /data/ansible-script/
vim tomcat.yml
- 手動下載一個對應版本的tomcat.tar.gz,並放在相應目錄
Tomcat官網
編寫劇本
# 指明在webservers組內所有主機上執行任務
# tempip是指目標服務器IP地址:SSH端口(或修改主機清單ansible/host使用別名也可)
# 例如 192.168.2.25:22
# 指明在webservers組內所有主機上執行任務
- hosts: tempip
#遠程執行任務的用戶爲
remote_user: root
#自定義變量
vars:
- tomcat: "/data/source/apache-tomcat-9.0.19.tar.gz"
- path: "/data1"
#任務列表
tasks:
#聲明
- name: mkdir {{ path }} && installation
#shell 模塊
shell: mkdir -p {{ path }}/installation
- name: copy Tomcat
copy: src={{ tomcat }} dest={{ path }}/apache-tomcat-9.0.19.tar.gz backup=yes
- name: tar && mv
shell: cd {{ path }} && tar -xvf apache-tomcat-9.0.19.tar.gz && mv apache-tomcat-9.0.19/ tomcat
- name: mv tomcat package
shell: mv {{ path }}/apache-tomcat-9.0.19.tar.gz {{ path }}/installation/
- name: check JAVA_HOME in /etc/profile
#shell: cat /etc/profile
shell: cat ~/.bashrc
register: bashrc_result
- name: 引用環境變量
when: bashrc_result.stdout.find('JAVA_HOME') == -1
shell: source /etc/profile
執行劇本
ansible-playbook tomcat.yml
報錯相關
若需要密碼(嫌麻煩),或失敗。請先打通單向登錄免密即可
單向登錄命令與腳本