2019-04-24 Day38

SSH批量管理項(xiàng)目如何一鍵一秒鐘完成:一秒完成寻定。

1.ssh-keygen非交互式創(chuàng)建秘鑰對:

具體命令:ssh-keygen -f ~/.ssh/id_rsa? -P '' -q

參數(shù)講解:

ssh-keygen:密鑰對創(chuàng)建工具

[-P old_passphrase]? 密碼

[-f output_keyfile]? 輸出的秘鑰文件

? ? [-q]? ? ? 不輸出信息? ? ?

[-t dsa ]? 指定秘鑰類型。

2.ssh-copy-id不需要提示yes/no分發(fā)秘鑰

具體命令:ssh-copy-id -f -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no 172.16.1.8

參數(shù)講解:

ssh-copy-id? -f? -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no root172.16.1.8

ssh-copy-id [-f] [-i [identity_file]] [-p port] [[-o <ssh -o options>] ...] [user@]hostname

說明:

-f: force mode 強(qiáng)制

[-i [identity_file]] 指定秘鑰文件

[[-o <ssh -o options>] ...] 指定ssh參數(shù)選項(xiàng)精耐。

3.sshpass工具:指定密碼非人工交互分發(fā)秘鑰

sshpass -p123456 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.7

[root@web02 ~]# sshpass -help

Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters

sshpass -p123456 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.7

sshpass [-f|-d|-p|-e] [-hV] command parameters

參數(shù)講解:

-p password? Provide password as argument (security unwise)? ? #指定用戶密碼操作

4.一鍵配置實(shí)踐

把web02作為分發(fā)服務(wù)器:

web02(8)-->m01(61)

web02(8)-->web01(7)

ssh-keygen -f ~/.ssh/id_rsa? -P '' -q

ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.7

sshpass -p123456 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.7

#!/bin/bash

#yum install sshpass -y

ssh-keygen -f ~/.ssh/id_rsa? -P '' -q

for ip in 7 61

do

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

done

#test

ssh 172.16.1.7 "ifconfig eth0"

ssh 172.16.1.61 "ifconfig eth0"

老男孩Linux58期-ansible自動(dòng)化管理實(shí)踐 ? ?

1.ansible介紹

ssh秘鑰認(rèn)證+腳本批量管理狼速,特點(diǎn):簡單、實(shí)用卦停,

但是看起來比較LOW向胡,需要人工寫腳本,類似實(shí)時(shí)復(fù)制的inotify工具惊完。

2013以前這種方式很普遍僵芹。

MySQL高可用 MHA集群,要求所有機(jī)器互相秘鑰認(rèn)證小槐。

大數(shù)據(jù)集群也需要拇派。

2.批量管理工具歷史

SSH+腳本 CFEngine、Puppet本股、saltstack? ? ? ? ? ? ? 攀痊、ansible

08年以前 07-08年? 10-13年? 14-17(python開發(fā))? ? ? 16-(python開發(fā))

? ? ? ? sina網(wǎng)

3.為什么用ansible?

簡單拄显、方便苟径、容易學(xué)習(xí)、功能同樣強(qiáng)大躬审。

ansible有配置文件棘街,可以多線程直接實(shí)現(xiàn)。不需要寫腳本承边,類似實(shí)時(shí)復(fù)制的sersync遭殉。

4.什么是ansible?

Ansible是一個(gè)用來遠(yuǎn)程管理服務(wù)器的工具軟件博助。

Ansible是一個(gè)用來批量部署遠(yuǎn)程主機(jī)上服務(wù)的工具险污。這里“遠(yuǎn)程主機(jī)(Remote Host)”是指任

何可以通過SSH登錄的主機(jī),所以它既可以是遠(yuǎn)程虛擬機(jī)或物理機(jī),也可以是本地主機(jī)蛔糯。

Ansible通過SSH協(xié)議實(shí)現(xiàn)管理節(jié)點(diǎn)與遠(yuǎn)程節(jié)點(diǎn)之間的通信拯腮。理論上來說,只要能通過SSH登錄到

遠(yuǎn)程主機(jī)來完成的操作,都可以通過Ansible實(shí)現(xiàn)批量自動(dòng)化操作。

涉及管理操作:復(fù)制文件扔罪、安裝服務(wù)秃臣、服務(wù)啟動(dòng)停止管理、配置管理等等。

5.為什么要用批量管理工具運(yùn)維?

提高效率,百度幾萬臺(tái)服務(wù)器哼丈,阿里幾十萬臺(tái)服務(wù)器。

如何省錢凸椿?

SSD+SATA 熱點(diǎn)存儲(chǔ) 15 15 7


6.Ansible特點(diǎn)

Ansible基于Python語言實(shí)現(xiàn)削祈,由Paramiko和PyYAML兩個(gè)關(guān)鍵模塊構(gòu)建。

Shell脑漫、Python是Linux運(yùn)維學(xué)員必會(huì)的兩門語言髓抑。

1)安裝部署過程特別簡單,學(xué)習(xí)曲線很平坦优幸。

2)不需要單獨(dú)安裝客戶端吨拍,只是利用現(xiàn)有的SSHD服務(wù)(協(xié)議)即可。

3)不需要服務(wù)端(no servers)网杆。

4)ansible playbook羹饰,采用yaml配置,提前編排自動(dòng)化任務(wù)碳却。

5)ansible功能模塊較多队秩,對于自動(dòng)化的場景支持豐富。

官方:http://docs.ansible.com

7.Ansible架構(gòu)介紹(見圖說明)

1昼浦、連接插件connectior plugins用于連接主機(jī) 用來連接被管理端

2馍资、核心模塊 core modules 連接主機(jī)實(shí)現(xiàn)操作, 它依賴于具體的模塊來做具體的事情

3关噪、自定義模塊 custom modules鸟蟹,根據(jù)自己的需求編寫具體的模塊

4、插件 plugins使兔,完成模塊功能的補(bǔ)充

5建钥、劇本 playbooks,ansible的配置文件,將多個(gè)任務(wù)定義在劇本中虐沥,由ansible自動(dòng)執(zhí)行

6熊经、主機(jī)清單 inventor,定義ansible需要操作主機(jī)的范圍

最重要的一點(diǎn)是 ansible是模塊化的 它所有的操作都依賴于模塊

(懂Python可以二次開發(fā))

8.ansible實(shí)踐環(huán)境準(zhǔn)備

61(m01)====>31(nfs01)

61(m01) ====>41(backup)

9.安裝ansible

m01管理機(jī):

yum install epel-release -y

yum install ansible -y

#如果有l(wèi)ibselinux-python就不執(zhí)行下面的命令了。

#rpm -qa |grep libselinux-python

#yum install libselinux-python -y

其他所有機(jī)器:

#rpm -qa |grep libselinux-python

#yum install libselinux-python -y

10.主機(jī)列表配置

ssh列表實(shí)現(xiàn)方法:

for n? in 7 8

do

echo 172.16.1.$n

done

/etc/ansible/hosts主機(jī)資產(chǎn)清單文件奈搜,用于定義被管理主機(jī)的認(rèn)證信息悉盆,

例如ssh登錄用戶名、密碼以及key相關(guān)信息馋吗。如何配置Inventory文件

1.主機(jī)支持主機(jī)名通配以及正則表達(dá)式,例如web[1:3].oldboy.com代表三臺(tái)主機(jī)

2.主機(jī)支持基于非標(biāo)準(zhǔn)的ssh端口秋秤,例如web1.oldboyedu.com:6666

3.主機(jī)支持指定變量宏粤,可對個(gè)別主機(jī)的特殊配置,如登陸用戶\密碼

4.主機(jī)組支持指定變量[group_name:vars]灼卢,同時(shí)支持嵌套組[game:children]

實(shí)踐:

cp /etc/ansible/hosts{,.ori}

cat >/etc/ansible/hosts<<EOF

[oldboy]

172.16.1.31

172.16.1.41

[oldgirl]

172.16.1.31

172.16.1.41

172.16.1.51

EOF

cat /etc/ansible/hosts

/etc/ansible/ansible.cfg #ansible的配置文件

11.小試牛刀

直接執(zhí)行如下命令或報(bào)錯(cuò)

ansible oldboy -m command -a "free -m"

基于SSH秘鑰認(rèn)證的前提下:

如果沒有做SSH秘鑰認(rèn)證绍哎,可以把用戶名密碼寫到

/etc/ansible/hosts

[oldboy_pass]

172.16.1.7 ansible_ssh_user=root ansible_ssh_pass=123456

172.16.1.8 ansible_ssh_user=root ansible_ssh_pass=123456

ansible oldboy -m command -a "free -m" 要想成功,先解決yes/no的問題鞋真。

1崇堰、ssh連接一遍。

2涩咖、ssh -o 參數(shù)

上述命令就是sshpass的封裝

==========================================

ansible關(guān)閉ssh首次連接時(shí)yes/no提示

使用ssh連接時(shí)海诲,可以使用-o參數(shù)將StrictHostKeyChecking設(shè)置為no,

避免使用ssh連接時(shí)避免首次連接時(shí)讓輸入yes/no部分的提示檩互。

方法1:修改 /etc/ansible/ansible.cfg中的374行

369 [ssh_connection]

370

371 # ssh arguments to use

372 # Leaving off ControlPersist will result in poor performance, so use

373 # paramiko on older platforms rather than removing it, -C controls compression use

374 #ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s

374行改為:

ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no

方法2:修改 /etc/ansible/ansible.cfg中的71行

70 # uncomment this to disable SSH key host checking

71 #host_key_checking = False

71行的注釋取消:host_key_checking = False

==========================================

[root@m01 /server/scripts]# > ~/.ssh/known_hosts

[root@m01 /server/scripts]# ssh 172.16.1.31

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

ECDSA key fingerprint is SHA256:qZSBkrmOv7xO/63qOU1uLXkPyNVHdkqvrNAcAmXqNEk.

ECDSA key fingerprint is MD5:23:d0:cb:a9:f4:7c:0b:eb:2d:07:00:e1:a3:12:d8:33.

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

[root@m01 /server/scripts]# ansible oldboy -m command -a "free -m"

172.16.1.31 | CHANGED | rc=0 >>

? ? ? ? ? ? ? total? ? ? ? used? ? ? ? free? ? ? shared? buff/cache? available

Mem:? ? ? ? ? ? 972? ? ? ? ? 88? ? ? ? 592? ? ? ? ? 13? ? ? ? 291? ? ? ? 708

Swap:? ? ? ? ? 767? ? ? ? ? 0? ? ? ? 767

172.16.1.41 | CHANGED | rc=0 >>

? ? ? ? ? ? ? total? ? ? ? used? ? ? ? free? ? ? shared? buff/cache? available

Mem:? ? ? ? ? ? 972? ? ? ? ? 89? ? ? ? 572? ? ? ? ? 13? ? ? ? 310? ? ? ? 706

Swap:? ? ? ? ? 767? ? ? ? ? 0? ? ? ? 767

目標(biāo):ansible是不是需要免秘鑰認(rèn)證(ssh免秘鑰認(rèn)證)特幔? yes

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

[oldboy]

172.16.1.31

172.16.1.7

還原ansible.cfg配置,重啟服務(wù)器

[root@m01 ~]# ansible oldboy -m command -a "free -m"

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

ECDSA key fingerprint is SHA256:qZSBkrmOv7xO/63qOU1uLXkPyNVHdkqvrNAcAmXqNEk.

ECDSA key fingerprint is MD5:23:d0:cb:a9:f4:7c:0b:eb:2d:07:00:e1:a3:12:d8:33.

Are you sure you want to continue connecting (yes/no)? The authenticity of host '172.16.1.31 (172.16.1.31)' can't be established.

ECDSA key fingerprint is SHA256:qZSBkrmOv7xO/63qOU1uLXkPyNVHdkqvrNAcAmXqNEk.

ECDSA key fingerprint is MD5:23:d0:cb:a9:f4:7c:0b:eb:2d:07:00:e1:a3:12:d8:33.

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

解決yes/no不需要輸入問題:

修改ansible.cfg 374行:

ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no

在執(zhí)行報(bào)錯(cuò):

[root@m01 ~]# ansible oldboy -m command -a "free -m"

172.16.1.7 | UNREACHABLE! => {

? ? "changed": false,

? ? "msg": "Failed to connect to the host via ssh: Warning: Permanently added '172.16.1.7' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,password).",

? ? "unreachable": true

}

172.16.1.31 | UNREACHABLE! => {

? ? "changed": false,

? ? "msg": "Failed to connect to the host via ssh: Warning: Permanently added '172.16.1.31' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",

? ? "unreachable": true

}

解決公鑰問題:

ssh-keygen -f ~/.ssh/id_rsa? -P '' -q

for ip in 7

do

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

done

#test

ssh 172.16.1.7 "ifconfig eth0"

執(zhí)行ansible命令:

[root@m01 ~]# ansible oldboy -m command -a "free -m"

172.16.1.31 | UNREACHABLE! => {

? ? "changed": false,

? ? "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",

? ? "unreachable": true

}

172.16.1.7 | CHANGED | rc=0 >>

? ? ? ? ? ? ? total? ? ? ? used? ? ? ? free? ? ? shared? buff/cache? available

Mem:? ? ? ? ? ? 972? ? ? ? ? 69? ? ? ? 807? ? ? ? ? 7? ? ? ? ? 95? ? ? ? 771

Swap:? ? ? ? ? 767? ? ? ? ? 0? ? ? ? 767

for ip in 31

do

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

done

#test

ssh 172.16.1.31 "ifconfig eth0"

[root@m01 ~]# ansible oldboy -m command -a "free -m"

172.16.1.7 | CHANGED | rc=0 >>

? ? ? ? ? ? ? total? ? ? ? used? ? ? ? free? ? ? shared? buff/cache? available

Mem:? ? ? ? ? ? 972? ? ? ? ? 69? ? ? ? 807? ? ? ? ? 7? ? ? ? ? 95? ? ? ? 771

Swap:? ? ? ? ? 767? ? ? ? ? 0? ? ? ? 767

172.16.1.31 | CHANGED | rc=0 >>

? ? ? ? ? ? ? total? ? ? ? used? ? ? ? free? ? ? shared? buff/cache? available

Mem:? ? ? ? ? ? 972? ? ? ? ? 71? ? ? ? 804? ? ? ? ? 7? ? ? ? ? 96? ? ? ? 768

Swap:? ? ? ? ? 767? ? ? ? ? 0? ? ? ? 767

驗(yàn)證:刪除公鑰闸昨,是不是不可以了蚯斯?

重啟后,不行饵较,重啟前可以

[root@m01 ~]# ansible oldboy -m command -a "free -m"

172.16.1.7 | UNREACHABLE! => {

? ? "changed": false,

? ? "msg": "Failed to connect to the host via ssh: Warning: Permanently added '172.16.1.7' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,password).",

? ? "unreachable": true

}

172.16.1.31 | UNREACHABLE! => {

? ? "changed": false,

? ? "msg": "Failed to connect to the host via ssh: Could not create directory '/root/.ssh'.\r\nWarning: Permanently added '172.16.1.31' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",

? ? "unreachable": true

}

修改Host增加用戶和密碼:

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

#[oldboy]

#172.16.1.31

#172.16.1.7

[oldboy_pass]

172.16.1.31 ansible_ssh_user=root ansible_ssh_pass=123456

172.16.1.7 ansible_ssh_user=root ansible_ssh_pass=123456

結(jié)果:

[root@m01 ~]# ansible oldboy_pass -m command -a "free -m"

172.16.1.31 | CHANGED | rc=0 >>

? ? ? ? ? ? ? total? ? ? ? used? ? ? ? free? ? ? shared? buff/cache? available

Mem:? ? ? ? ? ? 972? ? ? ? ? 79? ? ? ? 791? ? ? ? ? 7? ? ? ? 101? ? ? ? 758

Swap:? ? ? ? ? 767? ? ? ? ? 0? ? ? ? 767

172.16.1.7 | CHANGED | rc=0 >>

? ? ? ? ? ? ? total? ? ? ? used? ? ? ? free? ? ? shared? buff/cache? available

Mem:? ? ? ? ? ? 972? ? ? ? ? 69? ? ? ? 806? ? ? ? ? 7? ? ? ? ? 95? ? ? ? 771

Swap:? ? ? ? ? 767? ? ? ? ? 0? ? ? ? 767

結(jié)論:使用SSH連接:

密碼認(rèn)證 host里主機(jī)后面加密碼? ? ? ? Paramiko模塊 重點(diǎn):

秘鑰認(rèn)證:提前發(fā)公鑰拍嵌,才能用ansible.? SSHPASS工具

http://docs.ansible.com/ansible/latest/intro_inventory.html#list-of-behavioral-inventory-parameters

特殊端口:

[oldboy]

172.16.1.7? ansible_port=52113? ansible_user=root ansible_ssh_pass=123456

172.16.1.8? ansible_port=52113? ansible_user=root ansible_ssh_pass=123456

12.基于SSH秘鑰認(rèn)證的實(shí)踐

一鍵創(chuàng)建及分發(fā)秘鑰:

#!/bin/bash

ssh-keygen -f ~/.ssh/id_rsa? -P '' -q

for ip in 7 8 41 31

do

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

done

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

[oldboy]

172.16.1.31

172.16.1.41

172.16.1.7

[root@m01 ~]# ansible oldboy -m command -a "free -m" #和前面sh cmd.sh "free -m"

11.ansible命令參數(shù)

-m MODULE_NAME, 模塊名字,默認(rèn)command

-a MODULE_ARGS, 模塊參數(shù)

-f FORKS ? ? 并發(fā)進(jìn)程數(shù)循诉,默認(rèn)5個(gè)横辆。

-i INVENTORY(default=/etc/ansible/hosts)指定主機(jī)列表文件

===================

ansible

ansible-doc

ansible-playbook

ansible-galaxy

===================

12.ansible模塊查看和幫助*****

查找模塊

ansible-doc -l? ? ? ? ? #模塊就Linux命令了。

查看某個(gè)模塊的具體參數(shù)幫助

ansible-doc -s command? #Linux命令參數(shù)

12.1 command模塊 *****

1)功能說明:

command? Executes a command on a remote node

功能說明:執(zhí)行一個(gè)命令在遠(yuǎn)程節(jié)點(diǎn)上

操作實(shí)踐:

ansible oldboy -m command -a "free -m"

ansible oldboy -m command -a "df -h"

ansible oldboy -m command -a "ls /root"

ansible oldboy -m command -a "cat redhat-release"

ansible oldboy -m command -a "cat /etc/redhat-release"

最通用的功能打洼。

[root@m01 ~]# ansible oldboy -m command -a "cat /etc/redhat-release"

172.16.1.7 | CHANGED | rc=0 >>

CentOS Linux release 7.6.1810 (Core)

172.16.1.31 | CHANGED | rc=0 >>

CentOS Linux release 7.6.1810 (Core)

172.16.1.41 | CHANGED | rc=0 >>

CentOS Linux release 7.6.1810 (Core)

[root@m01 ~]# cat /server/scripts/cmd.sh

for n in 31 41

do

? echo "=====172.16.1.$n======"

? ssh 172.16.1.$n "$1"

done

[root@m01 ~]# sh /server/scripts/cmd.sh "cat /etc/redhat-release"

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

CentOS Linux release 7.6.1810 (Core)

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

CentOS Linux release 7.6.1810 (Core)

特殊:不支持的東西龄糊,例如 > < | &等 $HOME,替代方案用shell模塊

ansible oldboy -m shell -a "ps -ef|grep ssh"

ansible oldboy -m shell -a "echo oldboy >/tmp/a.log"

2)常用參數(shù)說明及實(shí)踐

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

- name: Executes a command on a remote node

? command:

? ? ? argv:? ? ? ? ? ? ? ? ? # Allows the user to provide the command as a list vs. a string.? Only the

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? string or the list form can be provided, not

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? both.? One or the other must be provided.

? ? ? chdir:? ? ? ? ? ? ? ? # Change into this directory before running the command.

? ? ? creates:? ? ? ? ? ? ? # A filename or (since 2.0) glob pattern. If it already exists, this step

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *won't* be run.

? ? ? free_form:? ? ? ? ? ? # (required) The command module takes a free form command to run.? There is no

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? parameter actually named 'free form'. See the

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? examples!

? ? ? removes:? ? ? ? ? ? ? # A filename or (since 2.0) glob pattern. If it already exists, this step *will*? be run.

? ? ? stdin:? ? ? ? ? ? ? ? # Set the stdin of the command directly to the specified value.

? ? ? warn:? ? ? ? ? ? ? ? ? # If command_warnings are on in ansible.cfg, do not warn about this particular

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? line if set to `no'.




參數(shù):chdir=/tmp配置相當(dāng)于cd /tmp

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

ansible oldboy? -m shell -a "cd /etc/;pwd"

參數(shù):creates=/etc? 相當(dāng)于條件測試? [ -e /etc ]||pwd 和下面removes相反

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

參數(shù):removes=/root 相當(dāng)于條件測試 [ -e /root ]&&ls /root

ansible oldboy? -m command -a "ls /root removes=/root"

ansible oldboy? -m shell -a "[ -d /etc ]||pwd"

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

參數(shù):warn=False 忽略警告

[root@m01 ~]# ansible oldboy? -m command -a "chmod 000 /etc/hosts warn=False"

更多官方鏈接:http://docs.ansible.com/ansible/latest/command_module.html或ansible-doc -s command

=====================================================================================

項(xiàng)目實(shí)踐:

rsync服務(wù)器端:31募疮、41

rsync客戶端:7炫惩、8

實(shí)現(xiàn):從7推送文件到31,從8推送文件到41.

shell模塊:實(shí)現(xiàn)上面

12.2 script模塊功能說明:

功能說明:在遠(yuǎn)程節(jié)點(diǎn)上運(yùn)行本地腳本

官方鏈接:http://docs.ansible.com/ansible/latest/script_module.html

遠(yuǎn)端可以沒有腳本阿浓,本地有就行:

[root@m01 /server/scripts]# cat setup.sh

pwd

ls /root

for n in {1..100}

do

? echo $n >>/tmp/oldboy.log

done

執(zhí)行:

ansible oldboy? -m script -a "/server/scripts/setup.sh"

項(xiàng)目實(shí)踐作業(yè):

寫好rsync一鍵客戶端配置他嚷,一鍵服務(wù)端配置。

12.3 copy模塊功能說明:

copy模塊功能說明:

功能說明:復(fù)制文件到遠(yuǎn)程主機(jī)

官方鏈接:http://docs.ansible.com/ansible/latest/copy_module.html

ansible oldboy? -m copy -a "src=/server dest=/ mode=ugo+x? group=root owner=root"

ansible oldboy? -m copy -a "src=/server/scripts/setup dest=/server/scripts mode=ugo+x? group=root owner=root backup=yes"

12.4 shell模塊功能說明:

功能說明:執(zhí)行一個(gè)命令在遠(yuǎn)程節(jié)點(diǎn)上

官方鏈接:http://docs.ansible.com/ansible/latest/shell_module.html

ansible oldboy? -m shell -a "free -m|grep buffer"

遠(yuǎn)程執(zhí)行腳本:腳本必須在遠(yuǎn)端存在

ansible oldboy? -m shell -a "/bin/bash /server/scripts/setup.sh"

項(xiàng)目實(shí)踐作業(yè):

1、寫好rsync一鍵客戶端配置筋蓖,一鍵服務(wù)端配置卸耘。

2、寫好nfs一鍵服務(wù)端端配置粘咖,一鍵客戶端掛載蚣抗,并且加到自啟動(dòng)文件里(/etc/rc.local,/etc/fstab)。

步驟:

1瓮下、遠(yuǎn)端命令行非交互實(shí)現(xiàn)

echo 123456|passwd --stdin oldboy

2翰铡、所有步驟放在腳本里實(shí)現(xiàn)。

3讽坏、管理機(jī)上遠(yuǎn)程執(zhí)行锭魔。

ssh /bin/sh /server/scripts/ins.sh

知識(shí)----能力-----價(jià)值-----金錢

學(xué)習(xí)方法:

學(xué)習(xí)能力

解決思路

任何問題有方法。

12.5 file模塊功能說明:

功能說明:設(shè)置文件屬性

官方鏈接:http://docs.ansible.com/ansible/latest/file_module.html

================================================================

替代方案:

ansible oldboy? -m command -a "chmod 777 /etc/hosts warn=false"

ansible oldboy? -m command -a "chmod 644 /etc/hosts warn=false"

ansible oldboy? -m command -a "chown oldboy /etc/hosts warn=false"

ansible oldboy? -m command -a "chown root /etc/hosts warn=false"

創(chuàng)建目錄:mkdir /tmp/oldboy_dir

ansible oldboy -m file -a "dest=/tmp/oldboy_dir state=directory"

遞歸設(shè)置權(quán)限:

ansible oldboy -m file -a "dest=/tmp/oldboy_dir state=directory mode=644 recurse=yes"

創(chuàng)建文件:touch /tmp/oldboy_file

ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch"

刪除文件:rm -f /tmp/oldboy_file

ansible oldboy -m file -a "dest=/tmp/oldboy_file state=absent"

創(chuàng)建鏈接文件:ln -s /etc/hosts /tmp/link_file

ansible oldboy -m file -a "src=/etc/hosts dest=/tmp/link_file state=link"

ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch owner=oldboy group=oldboy mode=000"

ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch owner=oldboy group=oldboy mode=ugo=rwx"

12.5 yum模塊功能說明:

功能說明:yum包管理模塊

官方鏈接:http://docs.ansible.com/ansible/latest/yum_module.html

ansible oldboy -m yum -a "name=nginx state=installed"

[root@nfs01 oldboy_dir]# rpm -qa nginx

nginx-1.10.2-1.el6.x86_64

###不要用yum卸載路呜,用rpm -e卸載迷捧。

12.6 service模塊功能說明:

功能說明:啟動(dòng)停止服務(wù)

官方鏈接:http://docs.ansible.com/ansible/latest/service_module.html

#相當(dāng)于

#service crond stop|/etc/init.d/crond stop

#chkconfig crond off

ansible oldboy -m service -a "name=crond state=stop enabled=no"

#相當(dāng)于/etc/init.d/crond start

chkconfig crond on

ansible oldboy -m service -a "name=crond state=started enabled=yes"

ansible oldboy -m command -a "name=crond state=started enabled=yes"

有選擇才叫有能力。

足球場上胀葱,讓拿球隊(duì)員有選擇漠秋,就容易進(jìn)球。

不讓對方有選擇巡社,就得人盯人膛堤。

12.7 cron模塊功能說明:

功能說明:管理定時(shí)任務(wù)條目信息模塊

官方鏈接:http://docs.ansible.com/ansible/latest/cron_module.html

定時(shí)任務(wù)格式:

* * * * * CMD

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

ansible oldboy -m cron -a "name='sync time' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'"

結(jié)果:

#Ansible: sync time

00 00 * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1

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

05 03 * * * /bin/sh /server/scripts/backup.sh /server/scripts/list >/dev/null 2>&1

命令如下:

ansible oldboy -m cron -a "name='backup data' minute=05 hour=03 job='/bin/sh /server/scripts/backup.sh /server/scripts/list >/dev/null 2>&1'"

結(jié)果:

#Ansible: backup data

05 03 * * * /bin/sh /server/scripts/backup.sh /server/scripts/list >/dev/null 2>&1

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

ansible oldboy -m cron -a "name='backup data' state=absent"

12.8 playbook

把所有ansible命令放在文件里執(zhí)行就是playbook。

playbook替代方案1:

[root@m01 ~]# cat ansible.sh

ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch"

ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch owner=oldboy group=oldboy mode=ugo=rwx"

ansible oldboy -m yum -a "name=nginx state=installed"

ansible oldboy -m service -a "name=crond state=started enabled=yes"

ansible oldboy -m cron -a "name='sync time' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'"

playbook替代方案2:

[root@m01 ~]# cat ~/set.sh

touch /tmp/oldboy_file

chown oldboy.oldboy /tmp/oldboy_file

yum install nginx -y

/etc/init.d/crond start

chkconfig cornd on

echo '#sync time oldboy' >>/var/spool/cron/root

echo '00 00 * * *? /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' >>/var/spool/cron/root

執(zhí)行:

ansible oldboy -m script -a "~/set.sh"

ansible劇本編寫格式說明

ansible劇本遵循PYyaml語法規(guī)則進(jìn)行編寫晌该,ymal文件基本編寫規(guī)則如下說明:

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

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

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

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

規(guī)則三:短橫線

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

- name: This command will change the working directory to somedir/ and will only run when /path/to/database doesn't exist.

? command: /usr/bin/make_database.sh arg1 arg2

? args:

? ? chdir: somedir/

? ? creates: /path/to/database

[root@m01 ~]# cat /etc/ansible/a.yml

- hosts: oldboy

? tasks:

? ? - shell: echo hello oldboy linux. >/tmp/a.log

ansible oldboy -m command -a "echo hello oldboy linux."

=========寫成劇本

- hosts: oldboy

? task:

? ? - command: echo hello oldboy linux.

=========寫成劇本

ansible oldboy? -m command -a "pwd chdir=/etc"

- hosts: oldboy

? task:

? ? - command: echo hello oldboy linux.

用ansible完成一鍵部署rsync服務(wù)端誉帅。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市右莱,隨后出現(xiàn)的幾起案子蚜锨,更是在濱河造成了極大的恐慌,老刑警劉巖慢蜓,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件亚再,死亡現(xiàn)場離奇詭異,居然都是意外死亡晨抡,警方通過查閱死者的電腦和手機(jī)氛悬,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進(jìn)店門则剃,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人如捅,你說我怎么就攤上這事棍现。” “怎么了镜遣?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵己肮,是天一觀的道長。 經(jīng)常有香客問我悲关,道長朴肺,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任坚洽,我火速辦了婚禮,結(jié)果婚禮上西土,老公的妹妹穿的比我還像新娘讶舰。我一直安慰自己,他們只是感情好需了,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布跳昼。 她就那樣靜靜地躺著,像睡著了一般肋乍。 火紅的嫁衣襯著肌膚如雪鹅颊。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天墓造,我揣著相機(jī)與錄音堪伍,去河邊找鬼。 笑死觅闽,一個(gè)胖子當(dāng)著我的面吹牛帝雇,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蛉拙,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼尸闸,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了孕锄?” 一聲冷哼從身側(cè)響起吮廉,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎畸肆,沒想到半個(gè)月后宦芦,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡恼除,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年踪旷,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了曼氛。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,779評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡令野,死狀恐怖舀患,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情气破,我是刑警寧澤聊浅,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站现使,受9級(jí)特大地震影響低匙,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜碳锈,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一顽冶、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧售碳,春花似錦强重、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至艺智,卻和暖如春倘要,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背十拣。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工封拧, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人父晶。 一個(gè)月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓哮缺,卻偏偏與公主長得像,于是被迫代替她去往敵國和親甲喝。 傳聞我的和親對象是個(gè)殘疾皇子尝苇,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評論 2 354

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

  • 1,ansible介紹 ssh密鑰認(rèn)證+腳本批量管理埠胖,特點(diǎn):簡單糠溜,實(shí)用 但是看起來比較low,需要人工寫腳本直撤,類似...
    自律的生活無慮閱讀 281評論 0 0
  • Ansible配置管理系統(tǒng) Ansible是一個(gè)配置管理系統(tǒng)configuration management sy...
    山有木兮_8adb閱讀 271評論 0 0
  • 12.ansible模塊查看和幫助***** 查找模塊 ansible-doc -l #模塊就Linux命...
    一關(guān)_e438閱讀 324評論 0 0
  • 1.ssh-keygen非交互式創(chuàng)建秘鑰對: 具體命令:ssh-keygen -f ~/.ssh/id_rsa ...
    1220閱讀 992評論 0 0
  • 項(xiàng)目管理過程組指對項(xiàng)目管理過程進(jìn)行邏輯分組非竿,以達(dá)到項(xiàng)目的特定目標(biāo),過程組不同于項(xiàng)目階段谋竖。 項(xiàng)目管理過程可以分為以下...
    霍亮_cefe閱讀 94評論 0 0