第1章 ssh+key實(shí)現(xiàn)基于密鑰連接(ansible使用前提)

第1章 ssh+key實(shí)現(xiàn)基于密鑰連接(ansible使用前提)

說(shuō)明:

?? ansible其功能實(shí)現(xiàn)基于SSH遠(yuǎn)程連接服務(wù)

?? 使用ansible需要首先實(shí)現(xiàn)ssh密鑰連接

1.1 部署ssh key

1.1.1 第一個(gè)里程碑: 創(chuàng)建密鑰對(duì)

ssh-keygen

-t 指定密鑰類型? rsa1 dsa(常用) ecdsa

語(yǔ)法:

SYNOPSIS

? ? ssh-keygen [-q] [-b bits] -t type [-N new_passphrase] [-C comment]

? ? ? ? ? ? ? ? [-f output_keyfile]

? ? ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile]

? ? ssh-keygen -i [-f input_keyfile]

? ? ssh-keygen -e [-f input_keyfile]

? ? ssh-keygen -y [-f input_keyfile]

? ? ssh-keygen -c [-P passphrase] [-C comment] [-f keyfile]

? ? ssh-keygen -l [-f input_keyfile]

? ? ssh-keygen -B [-f input_keyfile]

? ? ssh-keygen -D pkcs11

? ? ssh-keygen -F hostname [-f known_hosts_file] [-l]

? ? ssh-keygen -H [-f known_hosts_file]

? ? ssh-keygen -R hostname [-f known_hosts_file]

? ? ssh-keygen -r hostname [-f input_keyfile] [-g]

? ? ssh-keygen -G output_file [-v] [-b bits] [-M memory] [-S start_point]

? ? ssh-keygen -T output_file -f input_file [-v] [-a num_trials]

? ? ? ? ? ? ? ? [-W generator]

? ? ssh-keygen [-n] [-D smartcard]

? ? ssh-keygen -s ca_key -I certificate_identity [-h] [-Z principals]

? ? ? ? ? ? ? ? [-O option] [-V validity_interval] [-z serial_number] file ...

? ? ssh-keygen -L [-f input_keyfile]

創(chuàng)建密鑰的過(guò)程

[root@m01 ~]# ssh-keygen -t dsa

Generating public/private dsa key pair.

Enter file in which to save the key (/root/.ssh/id_dsa):? #私鑰創(chuàng)建后保存的路徑

Created directory '/root/.ssh'.

Enter passphrase (empty for no passphrase):? ? ? ? ? ? #私鑰需不需進(jìn)行加密犀盟,設(shè)置密碼

Enter same passphrase again:? #私鑰需不需進(jìn)行加密其做,再次輸入密碼確認(rèn)

Your identification has been saved in /root/.ssh/id_dsa.

Your public key has been saved in /root/.ssh/id_dsa.pub.

The key fingerprint is:

31:4a:4f:9f:97:b0:b6:ca:4c:53:78:70:89:83:5f:16 root@m01

The key's randomart image is:

+--[ DSA 1024]----+

|? ? ? ? ? E? ? ? |

|? ? ? . . o? ? |

|? ? ? o B *? ? ? |

|? ? . = @ + .? |

|? ? ? . S B o? ? |

|? ? ? ? + o? ? |

|? ? ? ? o .? ? ? |

|? ? ? + o? ? ? |

|? ? ? ? +? ? ? ? |

+-----------------+

創(chuàng)建出來(lái)的文件:

[root@m01 ~]# ll /root/.ssh/

total 8

-rw------- 1 root root 668 Oct 17 18:55 id_dsa? ? ? #創(chuàng)建出來(lái)的私鑰

-rw-r--r-- 1 root root 598 Oct 17 18:55 id_dsa.pub? #創(chuàng)建出來(lái)的公鑰

1.1.2 第二個(gè)里程碑: 分發(fā)公鑰文件

[root@m01 ~]# man ssh-copy-id

ssh-copy-id? -? install? your? public? key in a remote machine’s autho-rized_keys

注意:密鑰分發(fā)命令屬于openssh-clients軟件包

[root@nfs01 ~]# rpm -qf `which ssh-copy-id`

openssh-clients-5.3p1-122.el6.x86_64

語(yǔ)法格式

ssh-copy-id [-i [identity_file]] [user@]machine

-i? ? ? 指定要分發(fā)的公鑰文件以及路徑信息

[user@] 以什么用戶身份進(jìn)行分發(fā)

machine 將公鑰分發(fā)到哪臺(tái)主機(jī)上一膨,遠(yuǎn)程主機(jī)IP地址

[root@m01 ~]# ssh-copy-id? -i /root/.ssh/id_dsa.pub? root@172.16.1.41

The authenticity of host '172.16.1.41 (172.16.1.41)' can't be established.

RSA key fingerprint is d3:41:bb:0d:43:88:da:a3:2c:e8:36:91:11:c9:e4:9c.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '172.16.1.41' (RSA) to the list of known hosts.

root@172.16.1.41's password:

Now try logging into the machine, with "ssh 'root@172.16.1.41'", and check in

? .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.?

1.1.3 第三個(gè)里程碑: 基于密鑰登陸測(cè)試

[root@m01 ~]# ssh 172.16.1.41

Last login: Tue Oct 17 18:38:47 2017 from 10.0.0.1

[root@backup ~]#

?? 基于密鑰登陸方式成功↑

[root@m01 ~]# ssh root@172.16.1.41 "hostname -i"

172.16.1.41

不用的登陸到遠(yuǎn)程主機(jī)直接執(zhí)行命令,返回輸出結(jié)果↑

說(shuō)明:

管理主機(jī)一旦創(chuàng)建好秘鑰對(duì)文件概行,給多個(gè)主機(jī)分發(fā)公鑰時(shí),公鑰文件相同

1.1.4 ssh服務(wù)分發(fā)公鑰實(shí)質(zhì)執(zhí)行過(guò)程

①.?管理服務(wù)器創(chuàng)建私鑰和公鑰(密鑰對(duì))

②.?將公鑰文件遠(yuǎn)程傳送復(fù)制到被管理服務(wù)器相應(yīng)用戶~(yú)/.ssh/id_dsa.pub下,并修改.ssh目錄權(quán)限為700

③.?修改公鑰文件文件名稱為authorized_keys,授權(quán)權(quán)限為600

④.?利用ssh服務(wù)配置文件的配置參數(shù)徘公,進(jìn)行識(shí)別公鑰文件authorized_keys

⑤.?進(jìn)而實(shí)現(xiàn)基于密鑰遠(yuǎn)程登錄服務(wù)器(免密碼登錄/非交互方式登錄)

1.2 默認(rèn)端口號(hào)不是22,如何分發(fā)公鑰

1.2.1 查詢ssh-copy-id命令可以得知這是個(gè)腳本文件

[root@m01 ~]# file `which ssh-copy-id `

/usr/bin/ssh-copy-id: POSIX shell script text executable

看看腳本內(nèi)容發(fā)現(xiàn)傳輸方式

[root@m01 ~]# cat `which ssh-copy-id`|grep ssh

ssh $1 "exec sh -c 'cd; umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys && (test -x /sbin/restorecon && /sbin/restorecon .ssh .ssh/authorized_keys >/dev/null 2>&1 || true)'" || exit 1

說(shuō)明:

?? 1哮针、切換用戶到家目錄下步淹,臨時(shí)設(shè)置umask值

?? 2、判斷客戶端相應(yīng)用戶中有沒(méi)有.ssh目錄诚撵,如果沒(méi)有.ssh 目錄就進(jìn)行創(chuàng)建

3、將管理端公鑰文件內(nèi)容添加到客戶端~./ssh/authorized_keys, 默認(rèn)authorized_keys文件不存在键闺,需要?jiǎng)?chuàng)建寿烟,文件權(quán)限600

1.2.2 實(shí)現(xiàn)非22端口的分發(fā)

方法一: 修改腳本內(nèi)容

ssh -p52113 $1 "exec sh -c 'cd; umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys && (test -x /sbin/restorecon && /sbin/restorecon .ssh .ssh/authorized_keys >/dev/null 2>&1 || true)'" || exit 1

說(shuō)明:根據(jù)命令腳本,修改$1傳參信息辛燥,從而實(shí)現(xiàn)根據(jù)ssh不同端口傳送公鑰文件

方法二:將傳入的參數(shù)上添加上端口信息(推薦)

[root@m01 scripts]# ssh-copy-id -i /root/.ssh/id_dsa.pub "-p 52113 znix@172.16.1.250"

Now try logging into the machine, with "ssh '-p 52113 znix@172.16.1.250'", and check in:

? .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

1.2.3 關(guān)于 /usr/bin/ssh-copy-id 腳本中 $1的說(shuō)明

1.2.3.1? 編寫(xiě)腳本shift

[root@m01 scripts]# cat shift.sh

#!/bin/bash

until [ $# -eq 0 ]

do

echo $*

shift

done

測(cè)試

[root@m01 scripts]# sh shift.sh 1 2 3 4 5 6

1 2 3 4 5 6

2 3 4 5 6

3 4 5 6

4 5 6

5 6

6

說(shuō)明:

shift命令用于對(duì)參數(shù)的移動(dòng)(左移)筛武,通常用于在不知道傳入?yún)?shù)個(gè)數(shù)的情況下依次遍歷每個(gè)參數(shù)然后進(jìn)行相應(yīng)處理(常見(jiàn)于Linux中各種程序的啟動(dòng)腳本)缝其。

ssh-copy-id -i /root/.ssh/id_dsa.pub "-p 52113 znix@172.16.1.250"

由于/usr/bin/ssh-copy-id 腳本中前面使用了兩個(gè)shift 所有原本該為$3的參數(shù)變?yōu)榱?1.

if [ "-i" = "$1" ]; then

? shift

? # check if we have 2 parameters left, if so the first is the new ID file

? if [ -n "$2" ]; then

? ? if expr "$1" : ".*\.pub" > /dev/null ; then

? ? ? ID_FILE="$1"

? ? else

? ? ? ID_FILE="$1.pub"

? ? fi

? ? shift? ? ? ? # and this should leave $1 as the target name

? fi

else

1.3 實(shí)現(xiàn)自動(dòng)分發(fā)公鑰,遠(yuǎn)程管理多臺(tái)主機(jī)

1.3.1 【預(yù)備知識(shí)】shell中三種循環(huán)

#for 循環(huán)

for n in (1..100)

do

? ? ? xxx

done

#while循環(huán):循環(huán)條件為真時(shí)徘六,一直循環(huán)内边;為假時(shí),停止循環(huán)

while [ture]

do

? ? ? xxx

done

#until 循環(huán): 循環(huán)條件為假時(shí)待锈,一直循環(huán)漠其;為真時(shí),停止循環(huán)

until [ture]

do

? xxx

done


1.3.2 實(shí)現(xiàn)自動(dòng)分發(fā)公鑰竿音,遠(yuǎn)程管理多臺(tái)主機(jī)的阻礙因素和屎?

01.創(chuàng)建秘鑰對(duì)需要進(jìn)行交互

  a.需要確認(rèn)秘鑰保存路徑

  b.需要確認(rèn)密碼信息

02.分發(fā)公鑰時(shí)需要進(jìn)行交互

  a.需要進(jìn)行確認(rèn)yes|no

  b.第一次分發(fā)公鑰需要進(jìn)行密碼認(rèn)證

1.3.3 解決阻礙因素

1.自動(dòng)保存路徑,并且不密碼

ssh-keygen -t rsa -f ~/.ssh/id_rsa -N "" -q

參數(shù)說(shuō)明:

-f filename? ? Specifies the filename of the key file.

? ? ? ? ? ? ? ? ? 指定密鑰文件保存的路徑信息(免交互)

-P passphrase? ? ? Provides the (old) passphrase.

? ? ? ? ? ? ? ? ? ? 提供一個(gè)密碼信息

-N new_passphrase? ? ? Provides the new passphrase.

? ? ? -P -N? ? ? ? 都是免交互方式指定密碼信息

-q 安靜的? ? 不輸出信息春瞬,減少信息輸出

2.解決分發(fā)公鑰時(shí)需要進(jìn)行的交互

sshpass -p123456 ssh-copy-id -i ~/.ssh/id_rsa.pub " root@172.16.1.$ip? -o StrictHostKeyChecking=no "

參數(shù)說(shuō)明:

-o? option? 選擇 (man 手冊(cè)中可以查到有很多選項(xiàng))

StrictHostKeyChecking=no 對(duì)詢問(wèn)的回應(yīng)(不進(jìn)行對(duì)密鑰檢查)

要實(shí)現(xiàn)免密碼柴信,需要一款軟件 sshpass ?該軟件就是為ssh提供密碼使用的

[root@m01 ~]# yum install? sshpass? -y

注意:密碼與 -p之間不能有空格

1.3.4 最終批量分發(fā)腳本內(nèi)容

[root@m01 scripts]# vim ssh-key.sh

#!/bin/bash

. /etc/rc.d/init.d/functions

# 創(chuàng)建密鑰

\rm ~/.ssh/id_rsa* -f

ssh-keygen -t rsa -f ~/.ssh/id_rsa -N "" -q

# 分發(fā)公鑰

for ip in 31 41 8

do

sshpass -p123456 ssh-copy-id -i ~/.ssh/id_rsa.pub " root@172.16.1.$ip? -o StrictHostKeyChecking=no " &>/dev/null

if [ $? -eq 0 ];then

action? "fenfa 172.16.1.$ip"? /bin/true

else

action? "fenfa 172.16.1.$ip"? /bin/false

fi

echo ""

done

腳本執(zhí)行效果:

[root@m01 scripts]# sh ssh-key.sh

fenfa 172.16.1.31? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [? OK? ]

fenfa 172.16.1.41? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [? OK? ]

fenfa 172.16.1.8? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [? OK? ]

說(shuō)明:

?? 腳本中引用 . /etc/rc.d/init.d/functions 函數(shù),可以顯示執(zhí)行結(jié)果的判斷宽气。

?? 使用if語(yǔ)句進(jìn)行判斷随常,action 執(zhí)行相應(yīng)的動(dòng)作。true/false

1.3.5 實(shí)現(xiàn)基于密鑰的批量管理腳本

[root@m01 scripts]# vim piliang_guanli.sh

#!/bin/bash

CMD=$1

for ip in 8 31 41

do

echo ========host 172.16.1.$ip=======

ssh root@172.16.1.$ip "$CMD"

echo ============END===============

echo ""

done

腳本執(zhí)行效果:

[root@m01 scripts]# sh piliang_guanli.sh? date

======172.16.1.8======

Thu Oct 19 16:25:08 CST 2017

=========END=============

======172.16.1.31======

Thu Oct 19 16:25:08 CST 2017

=========END=============

======172.16.1.41======

Thu Oct 19 16:25:08 CST 2017

=========END=============

基于密鑰登陸方式萄涯,分發(fā)的公鑰文件會(huì)識(shí)別用戶信息绪氛,所以能夠?qū)崿F(xiàn)免密碼批量管理。

第2章 ansible軟件介紹

python 語(yǔ)言是運(yùn)維人員必須會(huì)的語(yǔ)言

ansible 是一個(gè)基于python 開(kāi)發(fā)的自動(dòng)化運(yùn)維工具

其功能實(shí)現(xiàn)基于ssh遠(yuǎn)程連接服務(wù)

ansible 可以實(shí)現(xiàn)批量系統(tǒng)配置窃判,批量軟件部署钞楼,批量文件拷貝,批量運(yùn)行命令等功能

除了ansible之外袄琳,還有saltstack 等批量管理軟件

2.1 自動(dòng)化批量管理方式說(shuō)明

2.1.1 ssh+key方式的說(shuō)明

?   免密碼登錄驗(yàn)證是單向的询件,方向從私鑰(鑰匙) >==> 公鑰(鎖)

?   SSH免密碼登錄基于用戶的,最好不要跨不同的用戶

?   SSH連接慢解決唆樊;即修改sshd_config配罝文件參數(shù)信息

?   批量分發(fā)1000臺(tái)初始都需要輸入一次密碼宛琅,并且第一次連接要確認(rèn)(expect/sshpass)

?   expect批量管理服務(wù)器參考 http://oldboy.blog.51cto.com/2561410/1206238

2.1.2 企業(yè)級(jí)生產(chǎn)場(chǎng)景批量管理-自動(dòng)化管理方案

①.最簡(jiǎn)單/最常用/最強(qiáng)大的選擇是ssh key+shell/pssh方案,一般中小型企業(yè)會(huì)用(50-100臺(tái)以下規(guī)模企業(yè))

??   a.利用ssh key執(zhí)行命令逗旁,并將命令放在腳本里面

??   b.利用ssh key執(zhí)行命令嘿辟,將命令放在腳本里面,并加上相應(yīng)循環(huán)語(yǔ)句或判斷語(yǔ)句

②.sina cfengine/puppet較早的批量管理工具片效;現(xiàn)在基本上沒(méi)有企業(yè)用

③.門(mén)戶級(jí)別比較流行的红伦,puppet批量管理工具(復(fù)雜/笨重)

④.saltstack批量管理工具;特點(diǎn):簡(jiǎn)單淀衣,功能強(qiáng)大(配罝復(fù)雜>---趕集網(wǎng)/小米/一些CDN公司 批量管理路線:ssh key-->cfengine-->puppet-->saltstack/ansible

  PS:使用ansible軟件的前提是ssh key公鑰分發(fā)充成

2.1.3 如何完成成集群規(guī)模架構(gòu)一鍵自動(dòng)化實(shí)現(xiàn)(步驟說(shuō)明)

①.1臺(tái)服務(wù)器先配置好(kickstart,cobbler無(wú)人值守安裝)昙读。高級(jí)實(shí)現(xiàn)云計(jì)算(按需分配,動(dòng)態(tài)調(diào)整)(openstack,kvm)

②.linux基本優(yōu)化膨桥,包括ssh服務(wù)(可以自動(dòng)化實(shí)現(xiàn))蛮浑。

  創(chuàng)建密鑰信息(自動(dòng)化免交互創(chuàng)建)

ssh-keygen -t dsa -P "" -f ~/.ssh/id_dsa >/dev/null 2>&1

  進(jìn)行批量分發(fā)密鑰(sshpass,expect自動(dòng)化實(shí)現(xiàn))

⑤.ansible軟件安裝(可以自動(dòng)化實(shí)現(xiàn))

⑥.網(wǎng)絡(luò)服務(wù)自動(dòng)化安裝(ansible實(shí)現(xiàn))

(搭建yum倉(cāng)庫(kù)唠叛,定制rpm包)

2.2 ansible軟件特點(diǎn)概述

l 不需要單獨(dú)安裝客戶端(no agents),基于系統(tǒng)自帶的sshd服務(wù)沮稚,sshd就相當(dāng)于ansible的客戶端

l 不需要服務(wù)端(no sever)

l 需要依靠大量的模塊實(shí)現(xiàn)批量管理

l 配置文件 /etc/ansible/ansible.cfg (前期不用配置)

ansible軟件相關(guān)參考鏈接信息

http://docs.ansible.com/ansible/intro_installation.html

http://www.ansible.com.cn/

http://docs.ansible.com/modules_by_category.html

http://www.ansible.cn/docs/

2.2.1 ansible軟件中查看模塊相關(guān)信息方法

[root@m01 ~]# ansible-doc -l

列出所有模塊信息

[root@m01 ~]# ansible-doc -s cron

參看指定模塊的幫助

2.3 部署ansible軟件

2.3.1 第一個(gè)里里程碑:部署ssh+key免密碼登錄方式

參見(jiàn)第一章內(nèi)容

2.3.2 第二個(gè)里程碑:被管理端安裝ansible相關(guān)管理軟件

[root@m01 ~]# yum install libselinux-python -y

?? 該軟件是用來(lái)對(duì)selinux進(jìn)行設(shè)置的艺沼,確保即使服務(wù)器selinux服務(wù)開(kāi)啟,依舊能夠通過(guò)ansible 軟件管理蕴掏。

2.3.3 第三個(gè)里程碑:管理端安裝ansible軟件障般,配置hosts文件

[root@m01 ~]# yum install ansible -y

軟件安裝完成,進(jìn)行修改ansible下的hosts文件囚似,注意文件的路徑

[root@m01 ~]# vim /etc/ansible/hosts

[oldboy]

172.16.1.31

172.16.1.41

172.16.1.8

文件信息說(shuō)明:

1.中括號(hào)中的名字代表組名

2.主機(jī)(hosts)部分可以使用域名剩拢、主機(jī)名、IP地址表示饶唤;一般此類配置中多使用IP地址徐伐;

3.組名下的主機(jī)地址就是ansible可以管理的地址

至此ansible 服務(wù)就部署完成 ↑

2.4 查看ansible軟件相關(guān)信息

2.4.1 ansible實(shí)踐部署地址規(guī)劃

服務(wù)器名稱

網(wǎng)卡eth0

網(wǎng)卡eth1

用途說(shuō)明

m01

10.0.0.61

172.16.1.61

批量管理服務(wù)器

nfs01

10.0.0.31

172.16.1.31

nfs共享儲(chǔ)存服務(wù)器

backup

10.0.0.41

172.16.1.41

rsync備份服務(wù)器

web01

10.0.0.8

172.16.1.8

web服務(wù)器

說(shuō)明:無(wú)特殊情況,子網(wǎng)掩碼為255.255.255.0

2.4.2 ansible軟件的版本信息

[root@m01 ~]# ansible --version

ansible 2.3.2.0

? config file = /etc/ansible/ansible.cfg

? configured module search path = Default w/o overrides

? python version = 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]

2.4.3 軟件目前主要會(huì)用到的文件

[root@m01 ~]# rpm -ql ansible

/etc/ansible/hosts? ? ? ? ? ? #定義anisble軟件可以管理的主機(jī)信息

/usr/bin/ansible? ? ? ? ? ? ? #ansible執(zhí)行命令

/usr/bin/ansible-playboot? # ansible執(zhí)行劇本命令

2.4.4 /etc/ansible下的文件

[root@m01 ansible]# ll

total 28

-rw-r--r-- 1 root root 18066 Sep? 6 06:38 ansible.cfg? #ansible配置文件

-rw-r--r-- 1 root root? 1016 Sep? 6 06:38 hosts? ? ? #定義ansible可以管理的主機(jī)信息

drwxr-xr-x 2 root root? 4096 Sep? 6 06:38 roles? #主要在自動(dòng)化的時(shí)候部署多臺(tái)主機(jī)時(shí)使用

2.5 ansible軟件的使用/參數(shù)

2.5.1 ansible遠(yuǎn)程批量執(zhí)行命令

語(yǔ)法:

? ? ansible oldboy -a "uptime"

? ansible oldboy -m command -a "uptime"

? ansible 定義的組/單個(gè)ip/域名/all? -m command -a "uptime"

說(shuō)明:-m 指定使用的模塊

?? ???-a 指定使用模塊中相應(yīng)的命令參數(shù)

?? ? ?命令參數(shù)只能是基本命令募狂,并不支持管道操作

????? all ??為hosts文件中的組全部管理


2.5.2 未分發(fā)公鑰如何實(shí)現(xiàn)遠(yuǎn)程管理主機(jī)及指定ansible端口信息

配置hosts文件時(shí)配置上密碼

vim /etc/ansible/hosts

[oldboy]

172.16.1.31:52113? ansible_ssh_user=root ansible_ssh_pass=123456

172.16.1.41

172.16.1.8

IP:端口 用戶 密碼

[znix]

www.znix.top:52113 ansible_ssh_user=znix

指定端口 用戶名

? ?測(cè)試修改端口后的結(jié)果 使用ping 模塊

[root@m01 ~]# ansible znix -m ping

www.znix.top | SUCCESS => {

? ? "changed": false,

? ? "ping": "pong"

}

2.6 ansible軟件常用參數(shù)表

命令參數(shù)

參數(shù)說(shuō)明

-m? MODULE_NAME

-module-name=MODULE_NAME

module name to execute (default=command)

相應(yīng)名稱的模塊被執(zhí)行(默認(rèn)模塊為command );

-m后邊是模塊的名宇

-a MODULE_ARGS

-args=MODULE_ARGS

module arguments

模塊參數(shù)信息

-a后面是要執(zhí)行的命令办素;也可以寫(xiě)個(gè)ip ,針對(duì)臺(tái)機(jī)器來(lái)執(zhí)行命令

-C, -checks

don't make any changes, instead, try to predict some of the changes that may

occurs

不做任何改變;反而祸穷,只是嘗試預(yù)言些可能出現(xiàn)的改變

-syntax-checks

perform a syntax check on the playbook, but do not execute ii*>

執(zhí)行語(yǔ)法檢查在劇本上性穿,但是并不執(zhí)行劇本

2.6.1 ansible命令執(zhí)行結(jié)果色彩說(shuō)明:

  綠色:表示沒(méi)有發(fā)生任何改變

  紅色:執(zhí)行命令操作出現(xiàn)異常

  黃色:執(zhí)行命令后,對(duì)受控主機(jī)產(chǎn)生影響雷滚,發(fā)生了配置改變

第3章 ansible中的模塊說(shuō)明

3.1 ping 模塊:測(cè)試連通性

[root@m01 ~]# ansible all -m ping

172.16.1.8 | SUCCESS => {

? ? "changed": false,

? ? "ping": "pong"

}

172.16.1.41 | SUCCESS => {

? ? "changed": false,

? ? "ping": "pong"

}

172.16.1.31 | SUCCESS => {

? ? "changed": false,

? ? "ping": "pong"

}

?? 連接正常返回 pong 通過(guò)幫助信息可以獲得 ↓

通過(guò) ansible-doc -v ping 可以獲得該模塊的說(shuō)明

ansible-doc -s file?? 參看模塊的具體信息

[root@m01 ~]# ansible-doc -v ping

Using /etc/ansible/ansible.cfg as config file

> PING? ? (/usr/lib/python2.6/site-packages/ansible/modules/system/ping.py)

? A trivial test module, this module always returns `pong' on successful contact. It does not make sense in playbooks, but it is useful from `/usr/bin/ansible' to verify the ability to login and that a usable? python is configured. This is NOT ICMP ping, this is just a trivial test module.

3.2 command 模塊 默認(rèn)模塊

3.2.1 command命令常用參數(shù)說(shuō)明

參數(shù)

參數(shù)說(shuō)明

chdir

在執(zhí)行命令之前需曾,通過(guò)cd命令進(jìn)入到指定目錄中

# ansible oldboy -m command -a "chdir=/tmp ls"

create

定義一個(gè)文件是否存在,如果不存在運(yùn)行相應(yīng)命令祈远;如果存在跳過(guò)此步驟

executable

改變shell使用command進(jìn)行執(zhí)行呆万,并且執(zhí)行時(shí)要使用絕對(duì)路徑

free_form

命令模塊采用自由形式命令運(yùn)行;即可以輸入任意linux命令

removes

定義一個(gè)文件是否存在车份,如果存在運(yùn)行相應(yīng)命令谋减;如果不存在跳過(guò)此步驟

warn

(added in 1.8)

如果ansible配置文件中定義了命令警告,如果參數(shù)設(shè)置了no/false扫沼,將不會(huì)警告此行命令

不指定模塊的時(shí)候默認(rèn)使用的模塊就是command? ↓

[root@m01 ~]# ansible all -a "date"

172.16.1.41 | SUCCESS | rc=0 >>

Thu Oct 19 17:12:15 CST 2017

172.16.1.31 | SUCCESS | rc=0 >>

Thu Oct 19 17:12:15 CST 2017

172.16.1.8 | SUCCESS | rc=0 >>

Thu Oct 19 17:12:15 CST 2017

使用ansible自帶模塊執(zhí)行命令 如果要用 > < | & ' ' 使用shell模塊

[root@m01 ~]# ansible all -m command -a "date"

172.16.1.8 | SUCCESS | rc=0 >>

Thu Oct 19 17:12:27 CST 2017

172.16.1.31 | SUCCESS | rc=0 >>

Thu Oct 19 17:12:28 CST 2017

172.16.1.41 | SUCCESS | rc=0 >>

Thu Oct 19 17:12:27 CST 2017

chdir參數(shù)的使用:

[root@m01 ~]# ansible oldboy -m command -a "chdir=/tmp pwd"

172.16.1.31 | SUCCESS | rc=0 >>

/tmp

172.16.1.8 | SUCCESS | rc=0 >>

/tmp

172.16.1.41 | SUCCESS | rc=0 >>

/tmp

creates 文件是否存在出爹,不存在就執(zhí)行命令

[root@m01 ~]# ansible oldboy -m command -a "creates=/etc/hosts date"

172.16.1.31 | SUCCESS | rc=0 >>

skipped, since /etc/hosts exists

removes 文件是否存在,不存在就不執(zhí)行命令缎除,

[root@m01 ~]# ansible oldboy -m command -a "removes=/etc/hosts date"

172.16.1.31 | SUCCESS | rc=0 >>

Fri Oct 20 13:32:40 CST 2017

3.3 shell模塊 萬(wàn)能模塊

執(zhí)行l(wèi)inux命令時(shí)可以用

遠(yuǎn)程節(jié)點(diǎn)執(zhí)行命令

?? 說(shuō)明: shell 模塊在遠(yuǎn)程執(zhí)行腳本時(shí)严就,遠(yuǎn)程主機(jī)上一定要有相應(yīng)的腳本

[root@m01 ~]# ansible oldboy -m shell -a "/bin/sh /server/scripts/ssh-key.sh"

172.16.1.31 | SUCCESS | rc=0 >>

fenfa 172.16.1.31 [? OK? ]

fenfa 172.16.1.41 [? OK? ]

fenfa 172.16.1.8 [? OK? ]

3.4 script 模塊 執(zhí)行腳本模塊

在本地執(zhí)行腳本時(shí),將腳本中的內(nèi)容傳輸?shù)竭h(yuǎn)程節(jié)點(diǎn)上運(yùn)行

[root@m01 ~]# ansible all -m script -a "/server/scripts/free.sh"

172.16.1.8 | SUCCESS => {

? ? "changed": true,

? ? "rc": 0,

? ? "stderr": "Shared connection to 172.16.1.8 closed.\r\n",

? ? "stdout": "? ? ? ? ? ? total? ? ? used? ? ? free? ? shared? ? buffers? ? cached\r\nMem:? ? ? ? ? 474M? ? ? 377M? ? ? ? 97M? ? ? 532K? ? ? ? 54M? ? ? 202M\r\n-/+ buffers/cache:? ? ? 120M? ? ? 354M\r\nSwap:? ? ? ? 767M? ? ? ? 0B? ? ? 767M\r\n",

? ? "stdout_lines": [

? ? ? ? "? ? ? ? ? ? total? ? ? used? ? ? free? ? shared? ? buffers? ? cached",

? ? ? ? "Mem:? ? ? ? ? 474M? ? ? 377M? ? ? ? 97M? ? ? 532K? ? ? ? 54M? ? ? 202M",

? ? ? ? "-/+ buffers/cache:? ? ? 120M? ? ? 354M",

? ? ? ? "Swap:? ? ? ? 767M? ? ? ? 0B? ? ? 767M"

? ? ]

}

說(shuō)明:

使用scripts模塊器罐,不用將腳本傳輸?shù)竭h(yuǎn)程節(jié)點(diǎn)梢为,腳本本身不用進(jìn)行授權(quán),即可利用script模塊執(zhí)行。直接執(zhí)行腳本即可抖誉,不需要使用sh

3.5 copy模塊 把本地文件發(fā)送到遠(yuǎn)端

3.5.1 copy模塊常用參數(shù)

選項(xiàng)參數(shù)

選項(xiàng)說(shuō)明

backup(重要參數(shù))

在覆蓋遠(yuǎn)端服務(wù)器文件之前,將遠(yuǎn)端服務(wù)器源文件備份衰倦,備份文件包含時(shí)間信息袒炉。有兩個(gè)選項(xiàng):yes|no

content

用于替代"src”,可以直接設(shè)定指定文件的值

dest

必選項(xiàng)。要將源文件復(fù)制到的遠(yuǎn)程主機(jī)的絕對(duì)路徑樊零,如果源文件是一個(gè)目錄我磁,那么該路徑也必須是個(gè)目錄

directory_mode

遞歸設(shè)定目錄的權(quán)限,默認(rèn)為系統(tǒng)默認(rèn)權(quán)限

forces

如果目標(biāo)主機(jī)包含該文件驻襟,但內(nèi)容不同夺艰,如果設(shè)置為yes,則強(qiáng)制覆蓋。

如果為no,則只有當(dāng)目標(biāo)主機(jī)的目標(biāo)位置不存在該文件時(shí)沉衣,才復(fù)制郁副。默認(rèn)為yes。別名:thirsty

others

所有的file模塊里的選項(xiàng)都可以在這里使用

src

被復(fù)制到遠(yuǎn)程主機(jī)的本地文件豌习,可以是絕對(duì)路徑存谎,也可以是相對(duì)路徑。如果路徑是一個(gè)目錄肥隆,它將遞歸復(fù)制既荚。在這種情況下,如果路徑使用"/"來(lái)結(jié)尾栋艳,則只復(fù)制目錄里的內(nèi)容恰聘,如果沒(méi)有使用"/"來(lái)結(jié)尾,則包含目錄在內(nèi)的整個(gè)內(nèi)容全部復(fù)制吸占,類似于rsync晴叨。

mode

定義文件或目錄的權(quán)限;注意:是4位

owner

修改屬主

group

修改屬組

說(shuō)明: src和content不能同時(shí)使用

3.5.2 copy常用命令參數(shù)測(cè)試

使用copy 模塊旬昭,將/etc/hosts 文件 傳輸?shù)礁鱾€(gè)服務(wù)器送篙螟,權(quán)限修改為0600 屬主屬組為oldboy

[root@m01 ~]# ansible oldboy -m copy -a "src=/etc/hosts dest=/tmp/ mode=0600 owner=oldboy group=oldboy "

172.16.1.8 | SUCCESS => {

? ? "changed": true,

? ? "checksum": "b3c1ab140a1265cd7f6de9175a962988d93c629b",

? ? "dest": "/tmp/hosts",

? ? "gid": 500,

? ? "group": "oldboy",

? ? "md5sum": "8c2b120b4742a806dcfdc8cfff6b6308",

? ? "mode": "0600",

? ? "owner": "oldboy",

? ? "size": 357,

? ? "src": "/root/.ansible/tmp/ansible-tmp-1508410846.63-224022812989166/source",

? ? "state": "file",

? ? "uid": 500

}

……

檢查結(jié)果

[root@m01 ~]# ansible all -m shell -a "ls -l /tmp/hosts"

172.16.1.31 | SUCCESS | rc=0 >>

-rw------- 1 oldboy oldboy 357 Oct 19 19:00 /tmp/hosts

172.16.1.41 | SUCCESS | rc=0 >>

-rw------- 1 oldboy oldboy 357 Oct 11 15:12 /tmp/hosts

172.16.1.8 | SUCCESS | rc=0 >>

-rw------- 1 oldboy oldboy 357 Oct 19 19:00 /tmp/hosts

移動(dòng)遠(yuǎn)程主機(jī)上的文件 remote_src=true 參數(shù)

[root@m01 ~]# ansible oldboy -m copy -a " src=/server/scripts/ssh-key.sh? dest=/tmp/ remote_src=true"

172.16.1.41 | SUCCESS => {

? ? "changed": true,

? ? "checksum": "d27bd683bd37e15992d2493b50c9410e0f667c9c",

? ? "dest": "/tmp/ssh-key.sh",

? ? "gid": 0,

? ? "group": "root",

? ? "md5sum": "dc88a3a419e3657bae7d3ef31925cbde",

? ? "mode": "0644",

? ? "owner": "root",

? ? "size": 397,

? ? "src": "/server/scripts/ssh-key.sh",

? ? "state": "file",

? ? "uid": 0

}

定義文件中的內(nèi)容 content=oldboyedu.com 默認(rèn)沒(méi)有換行

[root@m01 ~]# ansible oldboy -m copy -a "content=oldboyedu.com dest=/tmp/oldboy666.txt"

172.16.1.8 | SUCCESS => {

? ? "changed": true,

? ? "checksum": "291694840cd9f9c464263ea9b13421d8e74b7d00",

? ? "dest": "/tmp/oldboy666.txt",

? ? "gid": 0,

? ? "group": "root",

? ? "md5sum": "0a6bb40847793839366d0ac014616d69",

? ? "mode": "0644",

? ? "owner": "root",

? ? "size": 13,

? ? "src": "/root/.ansible/tmp/ansible-tmp-1508466752.1-24733562369639/source",

? ? "state": "file",

? ? "uid": 0

}

3.6 file模塊 設(shè)置文件屬性

3.6.1 file模塊常用參數(shù)

參數(shù)

參數(shù)說(shuō)明

owner

設(shè)置復(fù)制傳輸后的數(shù)據(jù)屬主信息

group

設(shè)置復(fù)制傳輸后的數(shù)據(jù)屬組信息

mode

設(shè)置文件數(shù)據(jù)權(quán)限信息

dest

要?jiǎng)?chuàng)建的文件或目錄命令,以及路徑信息

src

指定要?jiǎng)?chuàng)建軟鏈接的文件信息

state

state參數(shù)信息

directory

創(chuàng)建目錄

file

創(chuàng)建文件

link

創(chuàng)建軟鏈接

hard

創(chuàng)建出硬鏈接

absent

目錄將被遞歸刪除以及文件问拘,而鏈接將被取消鏈接

touch

創(chuàng)建文件遍略;如果路徑不存在將創(chuàng)建一個(gè)空文件

注意:重命名和創(chuàng)建多級(jí)目錄不能同時(shí)實(shí)現(xiàn)

3.6.2 常用參數(shù)測(cè)試

創(chuàng)建目錄

[root@m01 ~]# ansible oldboy -m file -a "dest=/tmp/oldboy_dir state=directory"

172.16.1.41 | SUCCESS => {

? ? "changed": true,

? ? "gid": 0,

? ? "group": "root",

? ? "mode": "0755",

? ? "owner": "root",

? ? "path": "/tmp/oldboy_dir",

? ? "size": 4096,

? ? "state": "directory",

? ? "uid": 0

}

創(chuàng)建文件

[root@m01 ~]# ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch"

172.16.1.8 | SUCCESS => {

? ? "changed": true,

? ? "dest": "/tmp/oldboy_file",

? ? "gid": 0,

? ? "group": "root",

? ? "mode": "0644",

? ? "owner": "root",

? ? "size": 0,

? ? "state": "file",

? ? "uid": 0

}

創(chuàng)建軟連接

[root@m01 ~]# ansible oldboy -m file -a "src=/tmp/oldboy_file dest=/tmp/oldboy_file_link state=link"

172.16.1.41 | SUCCESS => {

? ? "changed": true,

? ? "dest": "/tmp/oldboy_file_link",

? ? "gid": 0,

? ? "group": "root",

? ? "mode": "0777",

? ? "owner": "root",

? ? "size": 16,

? ? "src": "/tmp/oldboy_file",

? ? "state": "link",

? ? "uid": 0

}

刪除目錄文件信息

[root@m01 ~]# ansible oldboy -m file -a "dest=/tmp/oldboy_dir state=absent"

172.16.1.41 | SUCCESS => {

? ? "changed": true,

? ? "path": "/tmp/oldboy_dir",

? ? "state": "absent"

[root@m01 ~]# ansible oldboy -m file -a "dest=/tmp/oldboy_file state=absent"

172.16.1.31 | SUCCESS => {

? ? "changed": true,

? ? "path": "/tmp/oldboy_file",

? ? "state": "absent"

創(chuàng)建多級(jí)目錄

[root@m01 ~]# ansible oldboy -m copy -a "src=/etc/hosts dest=/tmp/01/0/0/0/0/0/0/0/"

172.16.1.31 | SUCCESS => {

? ? "changed": true,

? ? "checksum": "b3c1ab140a1265cd7f6de9175a962988d93c629b",

? ? "dest": "/tmp/01/0/0/0/0/0/0/0/hosts",

? ? "gid": 0,

? ? "group": "root",

? ? "md5sum": "8c2b120b4742a806dcfdc8cfff6b6308",

? ? "mode": "0644",

? ? "owner": "root",

? ? "size": 357,

? ? "src": "/root/.ansible/tmp/ansible-tmp-1508466973.39-99676412390473/source",

? ? "state": "file",

? ? "uid": 0

}

注意:重命名和創(chuàng)建多級(jí)目錄不能同時(shí)實(shí)現(xiàn)

3.7 fetch 模塊? 拉取文件

3.7.1 fetch常用參數(shù)說(shuō)明

參數(shù)

參數(shù)說(shuō)明

dest

將遠(yuǎn)程主機(jī)拉取過(guò)來(lái)的文件保存在本地的路徑信息

src

指定從遠(yuǎn)程主機(jī)要拉取的文件信息,只能拉取文件

flat

默認(rèn)設(shè)置為no骤坐,如果設(shè)置為yes绪杏,將不顯示172.16.1.8/etc/信息

3.7.2 常用參數(shù)實(shí)例

從遠(yuǎn)程拉取出來(lái)文件

[root@m01 cp]# ansible oldboy -m fetch -a "dest=/tmp/backup src=/etc/hosts"

172.16.1.8 | SUCCESS => {

? ? "changed": true,

? ? "checksum": "b3c1ab140a1265cd7f6de9175a962988d93c629b",

? ? "dest": "/tmp/backup/172.16.1.8/etc/hosts",

? ? "md5sum": "8c2b120b4742a806dcfdc8cfff6b6308",

? ? "remote_checksum": "b3c1ab140a1265cd7f6de9175a962988d93c629b",

? ? "remote_md5sum": null

}

[root@m01 cp]# tree /tmp/backup/

/tmp/backup/

├── 172.16.1.31

│? └── etc

│? ? ? └── hosts

├── 172.16.1.41

│? └── etc

│? ? ? └── hosts

└── 172.16.1.8

? ? └── etc

? ? ? ? └── hosts

?? flat 參數(shù),拉去的時(shí)候不創(chuàng)建目錄(同名文件會(huì)覆蓋)

[root@m01 tmp]# ansible oldboy -m fetch -a "dest=/tmp/backup/ src=/etc/hosts flat=yes"

172.16.1.8 | SUCCESS => {

? ? "changed": false,

? ? "checksum": "b3c1ab140a1265cd7f6de9175a962988d93c629b",

? ? "dest": "/tmp/backup/hosts",

? ? "file": "/etc/hosts",

? ? "md5sum": "8c2b120b4742a806dcfdc8cfff6b6308"

3.8 mount模塊 配置掛載點(diǎn)模塊

3.8.1 mount模塊常用參數(shù)

參數(shù)

參數(shù)說(shuō)明

fstype

指定掛載文件類型 -t nfs == fstype=nfs

opts?

設(shè)定掛載的參數(shù)選項(xiàng)信息 -o ro? == opts=ro

path?

掛載點(diǎn)路徑????????? path=/mnt

src??

要被掛載的目錄信息? src=172.16.1.31:/data

state?

state狀態(tài)參數(shù)

unmounted

加載/etc/fstab文件 實(shí)現(xiàn)卸載

absent??

在fstab文件中刪除掛載配置

present?

在fstab文件中添加掛載配置

mounted

1.將掛載信息添加到/etc/fstab文件中

2.加載配置文件掛載

3.8.2 mount參數(shù)實(shí)例

掛載

[root@m01 tmp]# ansible 172.16.1.8 -m mount -a "fstype=nfs opts=rw path=/mnt/? src=172.16.1.31:/data/ state=mounted"

172.16.1.8 | SUCCESS => {

? ? "changed": true,

? ? "dump": "0",

? ? "fstab": "/etc/fstab",

? ? "fstype": "nfs",

? ? "name": "/mnt/",

? ? "opts": "rw",

"passno": "0",

? "src": "172.16.1.31:/data/"

}


卸載

[root@m01 tmp]# ansible 172.16.1.8 -m mount -a "fstype=nfs opts=rw path=/mnt/? src=172.16.1.31:/data/ state=unmounted"

172.16.1.8 | SUCCESS => {

? "changed": true,

? ? "dump": "0",

? ? "fstab": "/etc/fstab",

? ? "fstype": "nfs",

? ? "name": "/mnt/",

? ? "opts": "rw",

? ? "passno": "0",

? ? "src": "172.16.1.31:/data/"

}

3.9 cron模塊 定時(shí)任務(wù)

3.9.1 cron模塊常用參數(shù)

參數(shù)

參數(shù)說(shuō)明

minute? 分

Minute when the job should run ( 0-59, *, */2, etc )

hour??? 時(shí)

Hour when the job should run ( 0-23, *, */2, etc )

day???? 日

Day of the month the job should run ( 1-31, *, */2, etc )

month?? 月

Month of the year the job should run ( 1-12, *, */2, etc )

weekday 周

Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc )

job?????

工作 ;要做的事情

name????

定義定時(shí)任務(wù)的描述信息

disabled

注釋定時(shí)任務(wù)

state???

state 狀態(tài)參數(shù)

absent

刪除定時(shí)任務(wù)

present

創(chuàng)建定時(shí)任務(wù)

默認(rèn)為present

3.9.2 cron模塊參數(shù)實(shí)踐

添加定時(shí)任務(wù)

[root@m01 ~]# ansible oldboy -m cron -a "minute=0 hour=0 job='/bin/sh? /server/scripts/hostname.sh &>/dev/null' name=oldboy01"

172.16.1.8 | SUCCESS => {

? ? "changed": true,

? ? "envs": [],

? ? "jobs": [

? ? "oldboy01"

? ? ]

}

刪除定時(shí)任務(wù)

[root@m01 ~]# ansible oldboy -m cron -a "minute=00 hour=00 job='/bin/sh? /server/scripts/hostname.sh &>/dev/null' name=oldboy01 state=absent"

172.16.1.8 | SUCCESS => {

? ? "changed": true,

? ? "envs": [],

? ? "jobs": []

}

只用名字就可以刪除

[root@m01 ~]# ansible oldboy -m cron -a "name=oldboy01? state=absent"

172.16.1.31 | SUCCESS => {

? ? "changed": true,

? ? "envs": [],

? ? "jobs": []

}

注釋定時(shí)任務(wù)

?? 注意: 注釋定時(shí)任務(wù)的時(shí)候必須有job的參數(shù)

[root@m01 ~]# ansible oldboy -m cron -a "name=oldboy01 job='/bin/sh? /server/scripts/hostname.sh &>/dev/null'? disabled=yes"

172.16.1.31 | SUCCESS => {

? ? "changed": true,

? ? "envs": [],

? ? "jobs": [

? ? "oldboy01"

? ? ]

}

取消注釋

[root@m01 ~]# ansible oldboy -m cron -a "name=oldboy01 job='/bin/sh? /server/scripts/hostname.sh &>/dev/null'? disabled=no"

172.16.1.41 | SUCCESS => {

? ? "changed": true,

? ? "envs": [],

? "jobs": [

? ? ? "oldboy01"

? ? ]

}

3.10 ?yum 模塊

3.10.1 yum 模塊常用參數(shù)

參數(shù)

參數(shù)說(shuō)明

name=name????

指定安裝的軟件

state=installed

安裝

3.10.2 yum模塊參數(shù)實(shí)踐

[root@m01 ~]# ansible oldboy -m yum -a "name=nmap state=installed? "

172.16.1.31 | SUCCESS => {

? ? "changed": true,

? ? "msg": "",

? ? "rc": 0,

? ? "results": [

? ? ? ? "Loaded plugins: fastestmirror, security\nSetting up Install Process\nLoading mirror speeds from cached hostfile\n * base: mirrors.aliyun.com\n * epel: mirrors.aliyun.com\n * extras: mirrors.aliyun.com\n * updates: mirrors.aliyun.com\nResolving Dependencies\n--> Running transaction check\n---> Package nmap.x86_64 2:5.51-6.el6 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package? ? ? ? Arch? ? ? ? ? ? Version? ? ? ? ? ? ? ? ? Repository? ? ? Size\n================================================================================\nInstalling:\n nmap? ? ? ? ? x86_64? ? ? ? ? 2:5.51-6.el6? ? ? ? ? ? ? base? ? ? ? ? 2.8 M\n\nTransaction Summary\n================================================================================\nInstall? ? ? 1 Package(s)\n\nTotal download size: 2.8 M\nInstalled size: 9.7 M\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r? Installing : 2:nmap-5.51-6.el6.x86_64? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1/1 \n\r? Verifying? : 2:nmap-5.51-6.el6.x86_64? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1/1 \n\nInstalled:\n? nmap.x86_64 2:5.51-6.el6? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? \n\nComplete!\n"

? ? ]

}

3.11 service模塊 服務(wù)管理

3.11.1 service模塊常用參數(shù)說(shuō)明

參數(shù)

參數(shù)說(shuō)明

name=service name

服務(wù)的名稱

state=參數(shù)

停止服務(wù) 服務(wù)狀態(tài)信息為過(guò)去時(shí)

stared/stoped/restarted/reloaded

enabled=yes?

設(shè)置開(kāi)機(jī)自啟動(dòng)

說(shuō)明 :service 管理的服務(wù)必須存在在/etc/init.d/下有的服務(wù)腳本

3.11.2 service 模塊參數(shù)實(shí)踐

重啟定時(shí)任務(wù)

[root@m01 ~]# ansible oldboy -m service -a "name=crond state=restarted"

172.16.1.8 | SUCCESS => {

? ? "changed": true,

? ? "name": "crond",

? ? "state": "started"

}

3.12 ansible中的常用模塊

常用模塊

模塊說(shuō)明

command ?(重要模塊)

執(zhí)行命令模塊纽绍,ansible命令執(zhí)行默認(rèn)模塊

shell ???(重要模塊)?

行shell腳本模塊

script ??(重要模塊)

把腳本發(fā)到客戶端蕾久,然后執(zhí)行;執(zhí)行腳本命令在遠(yuǎn)端服務(wù)器上

copy ?????(重要模塊)??

把本地文件發(fā)送到遠(yuǎn)端

file??????????????

設(shè)定文件屬性模塊

services??????????

系統(tǒng)服務(wù)管理模塊

cron??????????????

計(jì)劃任務(wù)管理模塊

yum??????????????

yum軟件包安裝管理模塊

synchronize???????

使用rsync同步文件模塊

mount?????????????

掛載模塊

3.13 其他模塊補(bǔ)充

3.13.1 hostname 修改主機(jī)名模塊

[root@m01 ~]# ansible 172.16.1.8 -m hostname -a "name=web01"

172.16.1.8 | SUCCESS => {

? ? "ansible_facts": {

? ? ? ? "ansible_domain": "etiantian.org",

? ? ? ? "ansible_fqdn": "www.etiantian.org",

? ? ? "ansible_hostname": "web01",

? ? ? ? "ansible_nodename": "web01"

? ? },

? ? "changed": false,

? ? "name": "web01"

}

3.13.2 selinux 管理模塊

[root@m01 ~]# ansible 172.16.1.8 -m selinux -a "state=disabled"

172.16.1.8 | SUCCESS => {

? ? "changed": false,

? ? "configfile": "/etc/selinux/config",

? ? "msg": "",

? ? "policy": "targeted",

? ? "state": "disabled"

}

3.13.3 get_url 模塊 == 【wget】

[root@m01 ~]# ansible 172.16.1.8 -m get_url -a "url=http://lan.znix.top/RDPWrap-v1.6.1.zip dest=/tmp/"

172.16.1.8 | SUCCESS => {

? ? "changed": true,

? ? "checksum_dest": null,

? ? "checksum_src": "ad402705624d06a6ff4b5a6a98c55fc2453b3a70",

? ? "dest": "/tmp/RDPWrap-v1.6.1.zip",

? ? "gid": 0,

? ? "group": "root",

? ? "md5sum": "b04dde546293ade71287071d187ed92d",

? ? "mode": "0644",

? ? "msg": "OK (1567232 bytes)",

? ? "owner": "root",

? ? "size": 1567232,

? ? "src": "/tmp/tmp4X4Von",

? ? "state": "file",

? ? "status_code": 200,

? ? "uid": 0,

? ? "url": "http://lan.znix.top/RDPWrap-v1.6.1.zip"

}

?? url= 下載文件的地址 dest 下載到哪里

?? timeout 超時(shí)時(shí)間

?? url_password? ?密碼

?? url_username ?用戶名

第4章 ansible-playbook 劇本

4.1 ansible基礎(chǔ)知識(shí)部分補(bǔ)充

4.1.1 ansible軟件特點(diǎn):

· 可以實(shí)現(xiàn)批量管理

· 可以實(shí)現(xiàn)批量部署

· ad-hoc(批量執(zhí)行命令)---針對(duì)臨時(shí)性的操作

?   ansible oldboy -m command -a "hostname"?? <- 批量執(zhí)行命令舉例

· 編寫(xiě)劇本-腳本(playbook)---針對(duì)重復(fù)性的操作

4.1.2 ansible核心功能:

pyYAML-----用于ansible編寫(xiě)劇本所使用的語(yǔ)言格式(saltstack---python)?

rsync-ini語(yǔ)法? sersync-xml語(yǔ)法 ?ansible-pyYAML語(yǔ)法

paramiko---遠(yuǎn)程連接與數(shù)據(jù)傳輸??

Jinja2-----用于編寫(xiě)ansible的模板信息?


4.2 ansible劇本編寫(xiě)規(guī)則說(shuō)明

4.2.1 pyYAML語(yǔ)法規(guī)則:

?? 規(guī)則一:縮進(jìn)

??? yaml使用一個(gè)固定的縮進(jìn)風(fēng)格表示數(shù)據(jù)層結(jié)構(gòu)關(guān)系拌夏,Saltstack需要每個(gè)縮進(jìn)級(jí)別由兩個(gè)空格組成僧著。一定不能使用tab鍵

??? 注意:編寫(xiě)yaml文件履因,就忘記鍵盤(pán)有tab


??? 規(guī)則二:冒號(hào)

??? CMD="echo"

??? yaml:

??? mykey:

??? 每個(gè)冒號(hào)后面一定要有一個(gè)空格(以冒號(hào)結(jié)尾不需要空格,表示文件路徑的模版可以不需要空格)


??? 規(guī)則三:短橫線

??? 想要表示列表項(xiàng)盹愚,使用一個(gè)短橫杠加一個(gè)空格栅迄。多個(gè)項(xiàng)使用同樣的縮進(jìn)級(jí)別作為同一個(gè)列表的一部分

?? ?核心規(guī)則:有效的利用空格進(jìn)行劇本的編寫(xiě),劇本編寫(xiě)是不支持tab的

4.3 劇本書(shū)寫(xiě)格式

### 劇本的開(kāi)頭皆怕,可以不寫(xiě)

- hosts: all? ? ? ? <- 處理所有服務(wù)器毅舆,找到所有服務(wù)器;? -(空格)hosts:(空格)all

tasks:? ? ? ? ? ? <- 劇本所要干的事情;? ? ? ? ? ? ? ? (空格)(空格)task:

- command: echo hello oldboy linux.?

? (空格)(空格)空格)(空格)-(空格)模塊名稱:(空格)模塊中對(duì)應(yīng)的功能

ansible all -m command -a "echo hello oldboy linux"? ? ?

??? 劇本編寫(xiě)內(nèi)容擴(kuò)展:劇本任務(wù)定義名稱

- hosts: 172.16.1.7? <- 處理指定服務(wù)器? ? ? ? ? ? ? ? ? -(空格)hosts:(空格)all

task:? ? ? ? ? ? ? ? <- 劇本所要干的事情;? ? ? ? ? ? ? ? (空格)(空格)task:

- name:

command: echo hello oldboy linux.? ? ? ? ? ? ? ? ?

(空格)(空格)空格)(空格)-(空格)模塊名稱:(空格)模塊中對(duì)應(yīng)的功能

4.3.1 劇本格式示例

[root@m01 ansible-playbook]# vim rsync_sever.yml

- hosts: 172.16.1.41

? tasks:

? ? - name: install rsync

? ? ? yum: name=rsync state=installed

4.4 劇本編寫(xiě)后檢查方法

01:ansible-playbook --syntax-check 01.yml?

?????   --- 進(jìn)行劇本配置信息語(yǔ)法檢查

02:ansible-playbook -C 01.yml?????????????

???????  ? --- 模擬劇本執(zhí)行(彩排)

4.4.1 語(yǔ)法檢查

[root@m01 ansible-playbook]# ansible-playbook --syntax-check 01.yml

playbook: 01.yml

4.4.2 模擬劇本執(zhí)行

[root@m01 ansible-playbook]# ansible-playbook -C 01.yml

PLAY [all] ****************************************************************

TASK [Gathering Facts] ****************************************************

ok: [172.16.1.41]

ok: [172.16.1.8]

ok: [172.16.1.31]

TASK [cron] ***************************************************************

ok: [172.16.1.8]

ok: [172.16.1.41]

ok: [172.16.1.31]

PLAY RECAP ****************************************************************

172.16.1.31? ? ? ? ? ? ? ? : ok=2? ? changed=0? ? unreachable=0? ? failed=0

172.16.1.41? ? ? ? ? ? ? ? : ok=2? ? changed=0? ? unreachable=0? ? failed=0

172.16.1.8? ? ? ? ? ? ? ? : ok=2? ? changed=0? ? unreachable=0? ? failed=0

4.5 劇本示例

4.5.1 劇本編寫(xiě)內(nèi)容擴(kuò)展:劇本任務(wù)編寫(xiě)多個(gè)任務(wù)

- hosts: all

? tasks:

? ? - name: restart-network

? ? ? cron: name='restart network' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'

? ? - name: sync time

? ? ? cron: name='sync time' minute=*/5 job="/usr/sbin/ntpdate pool.ntp.com >/dev/null 2>&1"

4.5.2 劇本編寫(xiě)內(nèi)容擴(kuò)展:劇本任務(wù)編寫(xiě)多個(gè)主機(jī)

- hosts: 172.16.1.7

? tasks:

? ? - name: restart-network

? ? ? cron: name='restart network' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'

? ? - name: sync time

? ? ? cron: name='sync time' minute=*/5 job="/usr/sbin/ntpdate pool.ntp.com >/dev/null 2>&1"

- hosts: 172.16.1.31

? tasks:

? ? - name: show ip addr to file

? ? ? shell: echo $(hostname -i) >> /tmp/ip.txt

4.6 劇本編寫(xiě)方式

01 多主機(jī)單任務(wù)編寫(xiě)方式

02 多主機(jī)多任務(wù)編寫(xiě)方式

03 不同主機(jī)多任務(wù)編寫(xiě)方式

第5章 常見(jiàn)錯(cuò)誤

5.1 ansible編寫(xiě)劇本排錯(cuò)思路

1. ansible-playbook編寫(xiě)完,檢査語(yǔ)法和模擬測(cè)試運(yùn)行

2. 打開(kāi)劇本愈腾,定位異常問(wèn)題原因憋活,將劇本中的內(nèi)容轉(zhuǎn)換命令執(zhí)行一次

cron: name=oldboy64 minute=ee hour=03 job='/bin/sh /server/scripts/test.sh &>/dev/null'

ansible oldboy -m cron -a "name=oldboy64 minute=00 hour=03 job='/bin/sh /server/scripts/test.sh &>/dev/null


3. 將參數(shù)中的腳本文件推送到遠(yuǎn)程屎務(wù)器,在遠(yuǎn)程服務(wù)器本地執(zhí)行腳本 sh -x test.sh

說(shuō)明:ansible執(zhí)行時(shí)虱黄,加1上-vvvv顯示ansible詳細(xì)執(zhí)行過(guò)程悦即,也可以定位異常原因!

5.1.1 排錯(cuò)邏輯

01. 劇本執(zhí)行中的錯(cuò)誤

02. 把劇本中的內(nèi)容轉(zhuǎn)換為ansible命令執(zhí)行

??? ansible oldboy -m yum -a "name=rsync state=installed"

03. 把a(bǔ)nsible服務(wù)器上執(zhí)行的命令放在被管理主機(jī)上執(zhí)行

??? yum install -y rsync

5.2 ansible 無(wú)法正常使用

5.2.1 在被控端上 root@notty 進(jìn)程一直存在

[root@backup ~]# ps -ef|grep sshd

root? ? ? 35274? ? ? 1? 0 15:25 ?? ? ? ? 00:00:00 /usr/sbin/sshd

root? ? ? 37004? 35274? 0 16:23 ?? ? ? ? 00:00:00 sshd: root@pts/2

root? ? ? 37062? 35274? 0 16:55 ?? ? ? ? 00:00:00 sshd: root@notty

root? ? ? 37154? 37006? 0 16:55 pts/2? ? 00:00:00 grep --color=auto sshd

5.2.2 解決辦法

首先礁鲁,將該進(jìn)程干掉

kill pid

5.2.3 然后使用ansible的 -vvvv 參數(shù)查看執(zhí)行的錯(cuò)誤信息

Loading callback plugin minimal of type stdout, v2.0 from /usr/lib/python2.6/site-packages/ansible/plugins/callback/__init__.pyc

META: ran handlers

Using module file /usr/lib/python2.6/site-packages/ansible/modules/system/ping.py

<172.16.1.8> ESTABLISH SSH CONNECTION FOR USER: None

<172.16.1.8> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/923ebeb605 172.16.1.8 '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''

……


找到在哪里出錯(cuò)盐欺。

5.2.4 可能的錯(cuò)誤

在 /etc/ssh/sshd_config 文件中的第132行為空,導(dǎo)致sftp 無(wú)法連接仅醇,出錯(cuò)~

133 Subsystem? ? ? sftp? ? /usr/libexec/openssh/sftp-server

5.3 常見(jiàn)問(wèn)題二:??

[root@m01 ~]# ansible? -k 172.16.1.51 -m ping

SSH password:

[WARNING]: No hosts matched, nothing to do

原因分析:

在ansible的hosts文件中冗美,沒(méi)有配置相應(yīng)主機(jī)地址信息

5.3.1 常見(jiàn)問(wèn)題三:???????

# ansible -k 172.16.1.51 -m ping

SSH password:

172.16.1.51|FAILED! => {

"failed": true,

"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.? Please add this host's fingerprint to your known_hosts file to manage this host."

}

原因分析:

因?yàn)闆](méi)有受控端的指紋信息,在known_hosts文件中

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末析二,一起剝皮案震驚了整個(gè)濱河市粉洼,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌叶摄,老刑警劉巖属韧,帶你破解...
    沈念sama閱讀 216,591評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異蛤吓,居然都是意外死亡宵喂,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)会傲,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)锅棕,“玉大人,你說(shuō)我怎么就攤上這事淌山÷懔牵” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,823評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵泼疑,是天一觀的道長(zhǎng)德绿。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么移稳? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,204評(píng)論 1 292
  • 正文 為了忘掉前任蕴纳,我火速辦了婚禮,結(jié)果婚禮上个粱,老公的妹妹穿的比我還像新娘袱蚓。我一直安慰自己,他們只是感情好几蜻,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,228評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著体斩,像睡著了一般梭稚。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上絮吵,一...
    開(kāi)封第一講書(shū)人閱讀 51,190評(píng)論 1 299
  • 那天弧烤,我揣著相機(jī)與錄音,去河邊找鬼蹬敲。 笑死暇昂,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的伴嗡。 我是一名探鬼主播急波,決...
    沈念sama閱讀 40,078評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼瘪校!你這毒婦竟也來(lái)了澄暮?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 38,923評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤阱扬,失蹤者是張志新(化名)和其女友劉穎泣懊,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體麻惶,經(jīng)...
    沈念sama閱讀 45,334評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡馍刮,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,550評(píng)論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了窃蹋。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片卡啰。...
    茶點(diǎn)故事閱讀 39,727評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖脐彩,靈堂內(nèi)的尸體忽然破棺而出碎乃,到底是詐尸還是另有隱情,我是刑警寧澤惠奸,帶...
    沈念sama閱讀 35,428評(píng)論 5 343
  • 正文 年R本政府宣布梅誓,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏梗掰。R本人自食惡果不足惜嵌言,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,022評(píng)論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望及穗。 院中可真熱鬧摧茴,春花似錦、人聲如沸埂陆。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,672評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)焚虱。三九已至购裙,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間鹃栽,已是汗流浹背躏率。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,826評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留民鼓,地道東北人薇芝。 一個(gè)月前我還...
    沈念sama閱讀 47,734評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像丰嘉,于是被迫代替她去往敵國(guó)和親夯到。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,619評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 1.ssh-keygen非交互式創(chuàng)建秘鑰對(duì): 具體命令:ssh-keygen -f ~/.ssh/id_rsa ...
    1220閱讀 992評(píng)論 0 0
  • 1饮亏,ansible介紹 ssh密鑰認(rèn)證+腳本批量管理黄娘,特點(diǎn):簡(jiǎn)單,實(shí)用 但是看起來(lái)比較low克滴,需要人工寫(xiě)腳本逼争,類似...
    自律的生活無(wú)慮閱讀 281評(píng)論 0 0
  • SSH批量管理項(xiàng)目如何一鍵一秒鐘完成:一秒完成 1.ssh-keygen非交互式創(chuàng)建秘鑰對(duì): 具體命令: 參數(shù)講解...
    藏鋒1013閱讀 608評(píng)論 0 3
  • 運(yùn)維核心工作: 操作系統(tǒng)安裝(物理機(jī)、虛擬機(jī))--> 應(yīng)用程序包部署(安裝劝赔、配置誓焦、服務(wù)啟動(dòng) )--> 批量操作 -...
    Net夜風(fēng)閱讀 1,525評(píng)論 0 4
  • 前段時(shí)間聽(tīng)一個(gè)朋友講,她的寫(xiě)作進(jìn)入了瓶頸期着帽,當(dāng)時(shí)我就有點(diǎn)納悶杂伟,什么樣的情況下就進(jìn)入瓶頸期了呢。這不仍翰,剛說(shuō)完一段時(shí)間...
    Extreme123閱讀 217評(píng)論 0 0