運維工做中一般會遇到須要安裝軟件,須要在多臺主機上安裝同一個軟件,若是是一臺一臺去安裝配置,這樣既耗精力又要花費大量時間,所以,爲了減小這些重複性工做。咱們都會使用不一樣的方法來實現自動化,自動在多臺主機上實現軟件的安裝及配置。在開源界也提供了大量這樣的工具,或者若是本身使用不爽,也能夠本身寫一個。這裏不展開介紹多種開源自動化運維工具,因爲我的對ansible使用比較熟悉,因此這裏就用ansible來實現部分系統的配置和tomcat的安裝及簡單配置。java
ansible簡單介紹python
ansible是基於模塊工做的,它的每種功能都依賴於模塊,ansible自身只是一個框架,它可以指揮着遠程主機作某些操做,可是每個特定操做都須要模塊來實現,好比依賴yum模塊,可讓遠程工具用yum源的方式安裝應用程序或者卸載應用程序,能夠基於command模塊讓遠程主機執行命令等。linux
若是須要讓遠程主機同時運行多個任務,此時就須要多項配置,這多項配置能夠定義在一個文本文件中,讓ansible經過讀取這個文件一次性把任務都執行完,而此文件就叫playbook,playbook是yaml格式的shell
架構(architecture)apache
ansible是經過指揮着多個遠程主機來完成運維工做,hostX是遠程主機(被指揮端),ansible鏈接每個被指揮端讓其可以工做時,是使用鏈接插件(Connection Plugins)向每個被控制端發起鏈接請求,這個鏈接請求是ssh的,ansible須要讀取配置文件,在配置文件中事先定義好有主機組,這些主機組都是分別作不一樣的功用的,就要依賴於(Host Inventory),這個文件能夠靜態的(手動編寫好)也能夠動態生成,這個文件中把事先分好組,然後使用ansible指揮操做時,ansible會自動讀取那個配置文件,去獲取指定組名下都有哪些主機然後僅去鏈接所指定的組名下的主機,在默認狀況ansible只控制5臺節點,5個完成後就接着去執行下面的5個,一批一批去執行,要改變其值能夠本身設定,ansible模塊有兩類:核心模塊、自定義模塊(可使用多種語言開發屬於本身的模塊),【工做過程:用戶指定主機,ansible讀取Inventory獲取主機,而後去鏈接主機,而且藉助模塊,經過Connection Plugins向主機發送指令,每個節點運行指令,並返回結果】,要使用其餘什麼功能均可以經過加載對應的插件來實現。windows
開始以前,首先是準備工做,看本身的需求,我這裏是要實現時間同步和禁用ipv6功能、安裝及簡單配置tomcat。這裏不去介紹ansible的使用。tomcat
把各需求細分:bash
1.common
配置好時間同步 > /dev/null中
禁用ipv6架構
2.app中
1.複製jdk過去
2.安裝jdk
3.配置好環境變量
執行環境變量文件
4.複製tomcat過去,解壓便可
5.建立軟鏈接
6.配置好環境變量
執行環境變量文件
啓動腳本須要提供
7.設置觸發器,修改配置文件
8傳送過去app
這裏我使用兩個roles,一個common、一個app。在common中實現通用的功能配置時間同步和禁用ipv6,在app中實現tomcat的安裝。若是是要實現多個不一樣主機進行多種不一樣的操做,能夠經過setup模塊獲取各主機的信息,經過指定不一樣主機的標識來進行不一樣的操做。如:
tasks:
- name: "shutdown Debian flavored systems"
command: /sbin/shutdown -h now
when: ansible_os_family == "Debian"
當主機的操做系統爲D耳邊時,會執行關機動做
ansible all –m setup 獲取遠程主機信息
官網https://www.ansible.com/ 是基於Python研發,python-jinja2是模板語言,ansible基於 agent less依賴於python-paramiko程序。
環境及配置
# cat /etc/redhat-release
CentOS release 6.8 (Final)
2G內存 2核
使用的主機
test1 test5 test6
192.168.40.101 192.168.40.105 192.168.40.106
A.準備工做
在test1上安裝ansible
安裝ansible,先配置好yum源
把
[epel]
name=Epel package install
baseurl=https://mirrors.aliyun.com/epel/6/x86_64/
enabled=1
gpgcheck=0
添加到/etc/yum.repos.d/CentOS-Base.repo文件中
yum install –y ansible 便可安裝ansible
rpm –ql ansible
/etc/ansible/ansible.cfg 主配置文件
/etc/ansible/hosts 主機分組定義的文件
/usr/bin/ansible-playbook 運行定義好的playbook程序
/usr/bin/ansible-doc ansible幫助信息的程序
/usr/bin/ansible-vault 程序是把playbook文件加密存放的,讀取時能夠經過vault解密以後去執行playbook文件
/usr/bin/ansible-pull 工做在推送模式
。。。。。。。。
ansible –h 查看幫助
命令格式: -m 模塊名稱 -a 模塊參數
# ansible <host pattern> [-m MODULE] -a 'MODULE_ARGS'
<host pattern> 主機模式
經常使用模塊
模塊:
command 這裏不能執行管道
user
copy
cron
file
filesystem
group
hostname
ping
yum
service
shell 能夠執行管道
script
獲取模塊使用幫助
ansible-doc –l 顯示全部可用模塊
ansible-doc MODULE_NAME
使用密鑰實現單向信任
ssh-keygen –t rsa –P ‘’
ssh-copy-id –i .ssh/id_rsa.pub 192.168.40.105
# ssh 192.168.40.105 'hostname'
test5 輸出不用輸入密碼說明OK了
ssh-copy-id –i .ssh/id_rsa.pub 192.168.40.106
# ssh 192.168.40.106 'hostname'
test6
用ansible模塊實現密鑰認證
用ansible提供的模塊方式,來自動實現公用的遠程傳送,ansible提供了authorized_key模塊,能夠實現本地公鑰複製到多個遠程主機上,而且放到對應用戶家目錄的.ssh/authorized_keys文件中,而且權限也不須要修改,從而實現密鑰的認證。可是須要在/etc/ansible/hosts文件中提供遠程用戶的密碼。
實現方式:
/etc/ansible/hosts文件內容以下:
[test]
192.168.40.105 ansible_ssh_pass=123456
192.168.40.106 ansible_ssh_pass=123456
authorized_key模塊的使用可用ansible-doc authorized_key查看
新增yml文件,內容以下:
# cat autho.yml
---
- name: authorized
hosts: test
tasks:
- name: authorized remote
authorized_key: user=root key="{{ lookup('file', lookup('env','HOME') + '/.ssh/id_rsa.pub') }}"
檢查一下語法
# ansible-playbook autho.yml --syntax-check
playbook: autho.yml
執行yml文件
# ansible-playbook autho.yml
PLAY [authorized] **************************************************************
TASK [setup] *******************************************************************
ok: [192.168.40.106]
ok: [192.168.40.105]
TASK [authorized remote] *******************************************************
changed: [192.168.40.105]
changed: [192.168.40.106]
PLAY RECAP *********************************************************************
192.168.40.105 : ok=2 changed=1 unreachable=0 failed=0
192.168.40.106 : ok=2 changed=1 unreachable=0 failed=0
查看authorized_keys文件是否建立成功
# ansible all -m command -a 'ls /root/.ssh/'
192.168.40.105 | SUCCESS | rc=0 >>
authorized_keys
192.168.40.106 | SUCCESS | rc=0 >>
authorized_keys
對比公鑰內容是否一致
# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAlq0JFI60Wu/tKid/a3O5i8+EbxSAgOcn9icELNF7iN8DC4hEOOtCr2Y4xd3LUw0pp4bRU1NyM8YSrXPCBk4VpaZ6s3A4+a/KEorsoKRCeVx6h5KsQUJO54LsRglMDsZf12/GAqqEpCO4uu2UCx7bTaL5r997UCLr2Y2aDvuvRDTWhCYlq3kiGpQLUMbf8pPbONBoswBYmKVJ3KDFk4qO296hcNBjDQlURNulxvTiR3rT/AkhgJezyIGix/9wQXIowWgDwDd1l0iPyMcxpxHW35QNmvA9OvDetLa7hup2GAK997sCkPS1pGpEjGr4j4svJxmVjQt3qE2pv8WYW+6CTw== root@test1
# ansible all -m command -a 'cat /root/.ssh/authorized_keys'
192.168.40.105 | SUCCESS | rc=0 >>
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAlq0JFI60Wu/tKid/a3O5i8+EbxSAgOcn9icELNF7iN8DC4hEOOtCr2Y4xd3LUw0pp4bRU1NyM8YSrXPCBk4VpaZ6s3A4+a/KEorsoKRCeVx6h5KsQUJO54LsRglMDsZf12/GAqqEpCO4uu2UCx7bTaL5r997UCLr2Y2aDvuvRDTWhCYlq3kiGpQLUMbf8pPbONBoswBYmKVJ3KDFk4qO296hcNBjDQlURNulxvTiR3rT/AkhgJezyIGix/9wQXIowWgDwDd1l0iPyMcxpxHW35QNmvA9OvDetLa7hup2GAK997sCkPS1pGpEjGr4j4svJxmVjQt3qE2pv8WYW+6CTw== root@test1
192.168.40.106 | SUCCESS | rc=0 >>
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAlq0JFI60Wu/tKid/a3O5i8+EbxSAgOcn9icELNF7iN8DC4hEOOtCr2Y4xd3LUw0pp4bRU1NyM8YSrXPCBk4VpaZ6s3A4+a/KEorsoKRCeVx6h5KsQUJO54LsRglMDsZf12/GAqqEpCO4uu2UCx7bTaL5r997UCLr2Y2aDvuvRDTWhCYlq3kiGpQLUMbf8pPbONBoswBYmKVJ3KDFk4qO296hcNBjDQlURNulxvTiR3rT/AkhgJezyIGix/9wQXIowWgDwDd1l0iPyMcxpxHW35QNmvA9OvDetLa7hup2GAK997sCkPS1pGpEjGr4j4svJxmVjQt3qE2pv8WYW+6CTw== root@test1
修改/etc/ansible/hosts
[test]
192.168.40.105
192.168.40.106
測試執行yml文件是否須要輸入密碼
# ansible all -m command -a 'hostname'
192.168.40.106 | SUCCESS | rc=0 >>
test6
192.168.40.105 | SUCCESS | rc=0 >>
test5
不須要輸入密碼了,一切都OK
把須要的軟件包都放到/usr/local/src目錄下
apache-tomcat-7.0.42.tar.gz jdk-7u67-linux-x64.rpm
B.配置主機組
編輯/etc/ansible/hosts時,先備份
cp /etc/ansible/hosts /etc/ansible/hosts_$(date +%F_%H:%M)
cat /etc/ansible/hosts
[test]
192.168.40.105
192.168.40.106
測試是否OK
# ansible test -m ping
192.168.40.105 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.40.106 | SUCCESS => {
"changed": false,
"ping": "pong"
}
說明都OK
C.寫roles和playbook
根據需求寫兩個roles
1.common
配置好時間同步 > /dev/null中
禁用ipv6
2.app中
1.複製jdk過去
2.安裝jdk
3.配置好環境變量
執行環境變量文件
4.複製tomcat過去,解壓便可
5.建立軟鏈接
6.配置好環境變量
執行環境變量文件
啓動腳本須要提供
7.設置觸發器,修改配置文件
8傳送過去
建立好目錄
mkdir /usr/local/src/roles/{common/{tasks,handlers,files},{app/{tasks,handlers,files}}}
把apache-tomcat-7.0.42.tar.gz、jdk-7u67-linux-x64.rpm、jdk.sh、server.xml、tomcat、tomcat.sh
jdk.sh是添加環境變量的
# cat roles/app/files/jdk.sh
#!/bin/bash
#
#
PATH_DIR=/usr/local/src
PROPATH=/etc/profile.d
cat > ${PROPATH}/java.sh << EOF
export JAVA_HOME=/usr/java/jdk1.7.0_67
export PATH=/usr/java/jdk1.7.0_67/bin:\${PATH}
EOF
source ${PROPATH}/java.sh
server.xml是tomcat的主配置文件這個文件根據需求本身改
tomcat是啓動腳本
#!/bin/sh
# Tomcat init script for linux.
#
#chkconfig: 2345 98 12
# descriptioin: The Apache Tomcat servlet/JSP container.
#
JAVA_HOME=/usr/java/latest
CATALINA_HOME=/usr/local/tomcat
export JAVA_HOME CATALINA_HOME
case $1 in
start)
exec $CATALINA_HOME/bin/catalina.sh start;;
stop)
exec $CATALINA_HOME/bin/catalina.sh stop;;
restart)
exec $CATALINA_HOME/bin/catalina.sh stop
sleep 2
exec $CATALINA_HOME/bin/catalina.sh start;;
configtest)
exec $CATALINA_HOME/bin/catalina.sh configtest;;
*)
echo "Usage: 'basename $0' {start|stop|restart|configtest}"
exit 1
;;
esac
tomcat.sh是添加環境變量和簡單配置主配置文件的
#!/bin/bash
#
#
PATH_DIR=/usr/local/src
PROPATH=/etc/profile.d
DEST_DIR=/usr/local
TOMCAT_VERSION=apache-tomcat-7.0.55
CONF=/usr/local/tomcat/conf/server.xml
#for file in $(ls ${PATH_DIR})
#do
# if [ "${file}" == "${TOMCAT_VERSION}.tar.gz" ];then
# tar xf ${PATH_DIR}/${TOMCAT_VERSION}.tar.gz -C ${DEST_DIR}
# ln -sv ${DEST_DIR}/${TOMCAT_VERSION} ${DEST_DIR}/tomcat
# break
# fi
#done
cat > ${PROPATH}/tomcat.sh << EOF
export CATALINA_HOME=${DEST_DIR}/tomcat
export PATH=${DEST_DIR}/tomcat/bin:\${PATH}
EOF
source ${PROPATH}/tomcat.sh
#AJP protocal comments
#<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> =>
#<!-- <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> -->
sed -i 's@<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />@<!-- <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> -->@' ${CONF}
# modified tomcat configuration file character utf-8
#<Connector port="8080" protocol="HTTP/1.1"
# connectionTimeout="20000"
# redirectPort="8443" />
#<Connector port="54966" protocol="HTTP/1.1"
# connectionTimeout="20000"
# redirectPort="8443" URIEncoding="UTF-8" useBodyEncodingForURI="true" />
sed -i ':a;N;$!ba;s@redirectPort="8443" />@redirectPort="8443" URIEncoding="UTF-8" useBodyEncodingForURI="true" />@' ${CONF}
在/usr/local/src/目錄下建立app.yml,內容如何
---
- name: tomcat install
hosts: test
roles:
- common
- app
1.common
配置好時間同步 > /dev/null中
禁用ipv6
# cat roles/common/tasks/main.yml
---
- name: setting datetime sync
cron: name="datatime sync" minute="*/5" job="/usr/sbin/ntpdate time.windows.com > /dev/null"
- name: disable ipv6 all
sysctl: name="net.ipv6.conf.all.disable_ipv6" value=1 sysctl_set=yes state=present reload=yes
- name: disable ipv6 default
sysctl: name="net.ipv6.conf.default.disable_ipv6" value=1 sysctl_set=yes state=present reload=yes
- name: disable ipv6 lo
sysctl: name="net.ipv6.conf.lo.disable_ipv6" value=1 sysctl_set=yes state=present reload=yes
2.app中
1.複製jdk過去
2.安裝jdk
3.配置好環境變量
執行環境變量文件
4.複製tomcat過去,解壓便可
5.建立軟鏈接
6.配置好環境變量
執行環境變量文件
啓動腳本須要提供
7.設置觸發器,修改配置文件
8傳送過去
# cat roles/app/handlers/main.yml
---
- name: restart tomcat
service: name=tomcat state=restarted
# cat roles/app/tasks/main.yml
---
- name: copy jdk to remote host
copy: src=jdk-7u67-linux-x64.rpm dest=/usr/local/src
- name: install jdk
yum: name=/usr/local/src/jdk-7u67-linux-x64.rpm state=present
- name: config jdk env
script: /usr/local/src/roles/app/files/jdk.sh
- name: copy unarchive tomcat to remote host
unarchive: src=/usr/local/src/roles/app/files/apache-tomcat-7.0.42.tar.gz dest=/usr/local copy=yes
- name: create soft link
file: src=/usr/local/apache-tomcat-7.0.42 dest=/usr/local/tomcat state=link
- name: copy tomcat init script for linux
copy: src=tomcat dest=/etc/rc.d/init.d/ mode='a+x'
- name: install tomcat
script: /usr/local/src/roles/app/files/tomcat.sh
- name: configuration file
copy: src=server.xml dest=/usr/local/tomcat/conf/server.xml
notify: restart tomcat
- name: start tomcat
service: name=tomcat enabled=yes state=started
最終的目錄結構爲:
# pwd
/usr/local/src
# tree .
.
├── apache-tomcat-7.0.42.tar.gz
├── app.yml
├── jdk-7u67-linux-x64.rpm
├── roles
│ ├── app
│ │ ├── files
│ │ │ ├── apache-tomcat-7.0.42.tar.gz
│ │ │ ├── jdk-7u67-linux-x64.rpm
│ │ │ ├── jdk.sh
│ │ │ ├── server.xml
│ │ │ ├── tomcat
│ │ │ └── tomcat.sh
│ │ ├── handlers
│ │ │ └── main.yml
│ │ └── tasks
│ │ └── main.yml
│ └── common
│ ├── files
│ ├── handlers
│ └── tasks
│ └── main.yml
└── tengine-2.0.1.tar.gz
D.執行playbook
# ansible test -m command -a 'crontab –l' 遠程主機無crontab
192.168.40.106 | FAILED | rc=1 >>
no crontab for root
192.168.40.105 | FAILED | rc=1 >>
no crontab for root
# ansible test -m command -a 'ifconfig eth0' 遠程主機ipv6未禁用
192.168.40.105 | SUCCESS | rc=0 >>
eth0 Link encap:Ethernet HWaddr 00:0C:29:56:9C:E1
inet addr:192.168.40.105 Bcast:192.168.40.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe56:9ce1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:16873 errors:0 dropped:0 overruns:0 frame:0
TX packets:9864 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:19181636 (18.2 MiB) TX bytes:716948 (700.1 KiB)
192.168.40.106 | SUCCESS | rc=0 >>
eth0 Link encap:Ethernet HWaddr 00:0C:29:4A:CB:A1
inet addr:192.168.40.106 Bcast:192.168.40.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe4a:cba1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:23568 errors:0 dropped:0 overruns:0 frame:0
TX packets:14044 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:29443133 (28.0 MiB) TX bytes:1011683 (987.9 KiB)
# ansible test -m command -a 'ls /usr/local/' 遠程主機沒有tomcat
192.168.40.105 | SUCCESS | rc=0 >>
bin
etc
games
include
lib
lib64
libexec
sbin
share
src
192.168.40.106 | SUCCESS | rc=0 >>
bin
etc
games
include
lib
lib64
libexec
sbin
share
src
檢查是否yaml寫的是否有語法錯誤
# ansible-playbook app.yml --syntax-check
playbook: app.yml
執行yaml
# ansible-playbook app.yml
PLAY [tomcat install] **********************************************************
TASK [setup] *******************************************************************
ok: [192.168.40.106]
ok: [192.168.40.105]
TASK [common : setting datetime sync] ******************************************
changed: [192.168.40.105]
changed: [192.168.40.106]
TASK [common : disable ipv6 all] ***********************************************
changed: [192.168.40.105]
changed: [192.168.40.106]
TASK [common : disable ipv6 default] *******************************************
changed: [192.168.40.105]
changed: [192.168.40.106]
TASK [common : disable ipv6 lo] ************************************************
changed: [192.168.40.105]
changed: [192.168.40.106]
TASK [app : copy jdk to remote host] *******************************************
changed: [192.168.40.106]
changed: [192.168.40.105]
TASK [app : install jdk] *******************************************************
changed: [192.168.40.106]
changed: [192.168.40.105]
TASK [app : config jdk env] ****************************************************
changed: [192.168.40.105]
changed: [192.168.40.106]
TASK [app : copy unarchive tomcat to remote host] ******************************
changed: [192.168.40.105]
changed: [192.168.40.106]
TASK [app : create soft link] **************************************************
changed: [192.168.40.106]
changed: [192.168.40.105]
TASK [app : copy tomcat init script for linux] *********************************
changed: [192.168.40.106]
changed: [192.168.40.105]
TASK [app : install tomcat] ****************************************************
changed: [192.168.40.105]
changed: [192.168.40.106]
TASK [app : configuration file] ************************************************ 配置文件沒有改,這裏是ok狀態
ok: [192.168.40.105]
ok: [192.168.40.106]
TASK [app : start tomcat] ******************************************************
changed: [192.168.40.105]
changed: [192.168.40.106]
PLAY RECAP *********************************************************************
192.168.40.105 : ok=14 changed=9 unreachable=0 failed=0
192.168.40.106 : ok=14 changed=9 unreachable=0 failed=0
遠程主機時間同步添加檢測
# ansible test -m command -a 'crontab -l'
192.168.40.106 | SUCCESS | rc=0 >>
#Ansible: datatime sync
*/5 * * * * /usr/sbin/ntpdate time.windows.com > /dev/null
192.168.40.105 | SUCCESS | rc=0 >>
#Ansible: datatime sync
*/5 * * * * /usr/sbin/ntpdate time.windows.com > /dev/null
遠程主機ipv6禁用檢查
# ansible test -m command -a 'ifconfig eth0'
192.168.40.106 | SUCCESS | rc=0 >>
eth0 Link encap:Ethernet HWaddr 00:0C:29:4A:CB:A1
inet addr:192.168.40.106 Bcast:192.168.40.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:224726 errors:0 dropped:0 overruns:0 frame:0
TX packets:46631 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:318132245 (303.3 MiB) TX bytes:3707341 (3.5 MiB)
192.168.40.105 | SUCCESS | rc=0 >>
eth0 Link encap:Ethernet HWaddr 00:0C:29:56:9C:E1
inet addr:192.168.40.105 Bcast:192.168.40.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:221974 errors:0 dropped:0 overruns:0 frame:0
TX packets:42693 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:313230389 (298.7 MiB) TX bytes:3438676 (3.2 MiB)
# ansible test -m command -a 'ls /usr/local/'
192.168.40.106 | SUCCESS | rc=0 >>
apache-tomcat-7.0.42
bin
etc
games
include
lib
lib64
libexec
sbin
share
src
tomcat
192.168.40.105 | SUCCESS | rc=0 >>
apache-tomcat-7.0.42
bin
etc
games
include
lib
lib64
libexec
sbin
share
src
tomcat
服務也已經啓動了
# ansible test -m command -a 'netstat -tnlp'
192.168.40.105 | SUCCESS | rc=0 >>
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 5777/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 5858/master
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 7086/java
tcp 0 0 :::8080 :::* LISTEN 7086/java
tcp 0 0 :::22 :::* LISTEN 5777/sshd
tcp 0 0 ::1:25 :::* LISTEN 5858/master
192.168.40.106 | SUCCESS | rc=0 >>
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 5777/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 5858/master
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 7080/java
tcp 0 0 :::8080 :::* LISTEN 7080/java
tcp 0 0 :::22 :::* LISTEN 5777/sshd
tcp 0 0 ::1:25 :::* LISTEN 5858/master
剛開始解壓和建立軟鏈接都是在shell腳本中實現的,執行時發現有問題
改成用tasks了
錯誤信息以下:
執行報錯,輸出詳細信息:加-vvv
fatal: [192.168.40.105]: FAILED! => {
"changed": true,
"failed": true,
"invocation": {
"module_args": {
"_raw_params": "/usr/local/src/roles/app/files/tomcat.sh"
},
"module_name": "script"
},
"rc": 2,
"stderr": "Shared connection to 192.168.40.105 closed.\r\n",
"stdout": "sed: can't read /usr/local/tomcat/conf/server.xml: No such file or directory\r\nsed: can't read /usr/local/tomcat/conf/server.xml: No such file or directory\r\n",
"stdout_lines": [
"sed: can't read /usr/local/tomcat/conf/server.xml: No such file or directory",
"sed: can't read /usr/local/tomcat/conf/server.xml: No such file or directory"
]
}
fatal: [192.168.40.106]: FAILED! => {
"changed": true,
"failed": true,
"invocation": {
"module_args": {
"_raw_params": "/usr/local/src/roles/app/files/tomcat.sh"
},
"module_name": "script"
},
"rc": 2,
"stderr": "Shared connection to 192.168.40.106 closed.\r\n",
"stdout": "sed: can't read /usr/local/tomcat/conf/server.xml: No such file or directory\r\nsed: can't read /usr/local/tomcat/conf/server.xml: No such file or directory\r\n",
"stdout_lines": [
"sed: can't read /usr/local/tomcat/conf/server.xml: No such file or directory",
"sed: can't read /usr/local/tomcat/conf/server.xml: No such file or directory"
]
}
to retry, use: --limit @/usr/local/src/app.retry
PLAY RECAP *********************************************************************
192.168.40.105 : ok=10 changed=1 unreachable=0 failed=1
192.168.40.106 : ok=10 changed=1 unreachable=0 failed=1
解決方法:
拆解腳本,把tomcat解壓和軟鏈接經過yaml來實現
roles/app/files/tomcat.sh
文件中註釋下面內容
#for file in $(ls ${PATH_DIR})
#do
# if [ "${file}" == "${TOMCAT_VERSION}.tar.gz" ];then
# tar xf ${PATH_DIR}/${TOMCAT_VERSION}.tar.gz -C ${DEST_DIR}
# ln -sv ${DEST_DIR}/${TOMCAT_VERSION} ${DEST_DIR}/tomcat
# break
# fi
#done
roles/app/tasks/main.yml 添加了
- name: copy unarchive tomcat to remote host
unarchive: src=/usr/local/src/roles/app/files/apache-tomcat-7.0.42.tar.gz dest=/usr/local copy=yes
- name: create soft link
file: src=/usr/local/apache-tomcat-7.0.42 dest=/usr/local/tomcat state=link
參考連接:
https://my.oschina.net/u/2456754/blog/505626
ansible的使用介紹連接: