溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ansible自動(dòng)化運(yùn)維必備工具詳解

發(fā)布時(shí)間:2020-06-25 13:42:06 來(lái)源:網(wǎng)絡(luò) 閱讀:2864 作者:啊凱linux 欄目:建站服務(wù)器


第2章 ansible簡(jiǎn)單介紹:

2.1 ansible是什么?

  • ansible是新出現(xiàn)的 自動(dòng)化 運(yùn)維工具 , 基于Python研發(fā) 。 糅合了眾多老牌運(yùn)維工具的優(yōu)點(diǎn)實(shí)現(xiàn)了批量操作系統(tǒng)配置、批量程序的部署、批量運(yùn)行命令等功能。 僅需在管理工作站上安裝 ansible 程序配置被管控主機(jī)的 IP 信息,被管控的主機(jī)無(wú)客戶端。 ansible 應(yīng)用程序存在于 epel( 第三方社區(qū) 源,依賴于很多 python 組件

  • python語(yǔ)言是運(yùn)維人員必會(huì)的語(yǔ)言!
    ansible是一個(gè)基于Python開發(fā)的自動(dòng)化運(yùn)維工具
    ansible的功能實(shí)現(xiàn)基于SSH遠(yuǎn)程連接服務(wù)
    ansible可以實(shí)現(xiàn)批量系統(tǒng)配置,批量軟件部署,批量文件拷貝,批量運(yùn)行命令等功能

 

2.2 ansible特點(diǎn):

  • 模塊化設(shè)計(jì),調(diào)用特定的模塊來(lái)完成特定任務(wù),本身是核心組件,短小精悍:

  • 基于Python語(yǔ)言實(shí)現(xiàn),由

  • Paramiko (python 的一個(gè)可并發(fā)連接 ssh主機(jī)功能庫(kù) ) ,PyYAMLJinja2 ( 模板化 三個(gè)關(guān)鍵模塊實(shí)現(xiàn);
    1)不需要單獨(dú)安裝客戶端,基于系統(tǒng)自帶的sshd服務(wù),sshd就相當(dāng)于ansible的客戶端
    2)不需要服務(wù)端
    3)需要依靠大量的模塊實(shí)現(xiàn)批量管理
    4)配置文件/etc/ansible/ansible.cfg

  • 配置文件

  • ansible 應(yīng)用程序的主配置文件/etc/ansible/ansible.cfg

  • Host Inventory 定義管控主機(jī) /etc/ansible/hosts

遵循 INI風(fēng)格;中括號(hào)中的字符是組名;一個(gè)主機(jī)可同時(shí)屬于多個(gè)組;

[webserver]

172.16.1.7

172.16.1.8

 

[server]

172.16.1.7

  1. 3.     A collection of hosts belonging tothe'webservers' group;一批主機(jī)屬于一個(gè)組,例如定義為 'webservers' 的組

[webservers]

172.16.1.31

175.16.1.31

 

#[webservers] 組名  下面是管理的主機(jī) 可以實(shí)現(xiàn)批量管理一個(gè)組的主機(jī),前提必須是sshkey秘鑰認(rèn)證。

注意:默認(rèn)是以 root 用戶執(zhí)行,但是基于 ssh 連接操作要多次輸入密碼,為方便可以使用基于 ssh 密鑰方式進(jìn)行認(rèn)證

第3章 安裝ansible

3.1 mo1管理機(jī)安裝ansible

#安裝ansible之前需要安裝epel.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

 

#安裝ansible工具

yum -y install ansible

3.2 所有被管理端需要安裝

#管理的機(jī)器必須安裝

yum -y install libselinux-python

 

#安裝完畢以后檢查

[root@nfs01 ~]# rpm -qa | grep libselinux-python
libselinux-python-2.0.94-7.el6.x86_64

第4章 ansible命令幫助

 ansible-doc命令:獲取模塊列表,及模塊使用格式;

ansible-doc -l :獲取列表查看所有的模塊

ansible-doc -s  module_name :獲取指定模塊的使用信息

ansible-doc -s service 查看指定模塊用法

 

ansible 命令常用參數(shù)

-C, --check           don't make any changes; instead, tryto predict some

Connection Options: #檢查不做任何改動(dòng);相反, 嘗試預(yù)測(cè)一些

-v    顯示輸出信息   -vvvv  最多可以4v

 

#ansible 命令幫助參數(shù)詳解

[root@m01 /]# ansible --help

Usage: ansible <host-pattern> [options]

 

Options:

  -a MODULE_ARGS, --args=MODULE_ARGS

 

                       module arguments

 --ask-vault-pass      ask forvault password

  -BSECONDS, --background=SECONDS

                        run asynchronously,failing after X seconds

                        (default=N/A)

  -C, --check           don't make any changes; instead, tryto predict some

                        of the changes that mayoccur

  -D,--diff            when changing (small)files and templates, show the

                        differences in thosefiles; works great with --check

  -eEXTRA_VARS, --extra-vars=EXTRA_VARS

                        set additional variablesas key=value or YAML/JSON

  -fFORKS, --forks=FORKS

                        specify number ofparallel processes to use

                        (default=5)

  -h,--help            show this help messageand exit

  -iINVENTORY, --inventory-file=INVENTORY

                        specify inventory hostpath

                       (default=/etc/ansible/hosts) or comma separated host

                        list.

  -lSUBSET, --limit=SUBSET

                        further limit selectedhosts to an additional pattern

 --list-hosts          outputs alist of matching hosts; does not execute

                        anything else

  -m MODULE_NAME,--module-name=MODULE_NAME

                        module name to execute(default=command)

  -M MODULE_PATH,--module-path=MODULE_PATH

                        specify path(s) tomodule library (default=None)

 --new-vault-password-file=NEW_VAULT_PASSWORD_FILE

                        new vault password filefor rekey

  -o,--one-line        condense output

 --output=OUTPUT_FILE  output filename for encrypt or decrypt; use - for

                        stdout

  -PPOLL_INTERVAL, --poll=POLL_INTERVAL

                        set the poll intervalif using -B (default=15)

 --syntax-check        perform asyntax check on the playbook, but do not

                        execute it

  -tTREE, --tree=TREE  log output to thisdirectory

 --vault-password-file=VAULT_PASSWORD_FILE

                        vault password file

  -v,--verbose         verbose mode (-vvv formore, -vvvv to enable

                        connection debugging)

 --version             showprogram's version number and exit

 

 Connection Options:

   control as whom and how to connect to hosts

 

    -k,--ask-pass      ask for connectionpassword

   --private-key=PRIVATE_KEY_FILE, --key-file=PRIVATE_KEY_FILE

                        use this file toauthenticate the connection

    -uREMOTE_USER, --user=REMOTE_USER

                        connect as this user(default=None)

    -cCONNECTION, --connection=CONNECTION

                        connection type to use(default=smart)

    -TTIMEOUT, --timeout=TIMEOUT

                        override the connectiontimeout in seconds

                        (default=10)

   --ssh-common-args=SSH_COMMON_ARGS

                       specify common arguments topass to sftp/scp/ssh (e.g.

                        ProxyCommand)

   --sftp-extra-args=SFTP_EXTRA_ARGS

                        specify extra argumentsto pass to sftp only (e.g. -f,

                        -l)

   --scp-extra-args=SCP_EXTRA_ARGS

                        specify extra argumentsto pass to scp only (e.g. -l)

   --ssh-extra-args=SSH_EXTRA_ARGS

                        specify extra argumentsto pass to ssh only (e.g. -R)

 

 Privilege Escalation Options:

    controlhow and which user you become as on target hosts

 

    -s,--sudo          run operations with sudo(nopasswd) (deprecated, use

                        become)

    -USUDO_USER, --sudo-user=SUDO_USER

                        desired sudo user(default=root) (deprecated, use

                        become)

    -S,--su            run operations with su(deprecated, use become)

    -RSU_USER, --su-user=SU_USER

                        run operations with suas this user (default=root)

                        (deprecated, usebecome)

    -b,--become        run operations withbecome (does not imply password

                        prompting)

   --become-method=BECOME_METHOD

                        privilege escalationmethod to use (default=sudo),

                        valid choices: [ sudo |su | pbrun | pfexec | doas |

                        dzdo | ksu | runas ]

   --become-user=BECOME_USER

                        run operations as thisuser (default=root)

   --ask-sudo-pass     ask for sudopassword (deprecated, use become)

   --ask-su-pass       ask for supassword (deprecated, use become)

    -K,--ask-become-pass

                        ask for privilegeescalation password

4.1 ansible語(yǔ)法格式

 

.ansible 命令格式

ansible <host-pattern>  [-m module_name]  [-a args]

命令     主機(jī)模式      指定模塊默認(rèn)(command d

<host-pattern>

指明管控主機(jī),以模式形式表示或者直接給定 IP,必須事先定義在文件中; all 設(shè)置所有;主機(jī)組;

[-m module_name]

使用那種模塊管理操作,所有的操作都需要通過(guò)模塊來(lái)指定

默認(rèn)不指定模塊是command

[-a args]

指明模塊專用參數(shù); args 一般為 key=value 格式

注意:command模塊的參數(shù)非為kv格式,而是直接給出要執(zhí)行的命令即可;  如:-a  "hostname"   

注意: <host-pattern> 默認(rèn)讀取 /etc/ansible/hosts ,也可以指明自定義文件路徑

-iPATH, --inventory=PATH:指明使用的host inventory文件路徑;

 

 

4.2 ☆常用模塊 (module_name) 

4.3 重要模塊功能總結(jié)

ansible重要模塊功能總結(jié)
    command(重要模塊)執(zhí)行命令模塊,ansible命令執(zhí)行默認(rèn)模塊
    shell
(重要模塊)執(zhí)行shell腳本模塊
    script
(重要模塊)把腳本發(fā)到客戶端,然后執(zhí)行;執(zhí)行腳本命令在遠(yuǎn)端服務(wù)器
    copy
(重要模塊)把本地文件發(fā)送到遠(yuǎn)端
    cron    
(重要模塊)編寫定時(shí)任務(wù)的模塊

4.3.1 command(命令模塊)

默認(rèn)模塊,可省略。在遠(yuǎn)程主機(jī)上進(jìn)行操作命令。 -a "hostname"

注意: comand 模塊的參數(shù)非 key=value 格式,直接給出要執(zhí)行的命令

 

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

-a  "name= state=  minute=  hour= day= month=  weekday= job="

4.3.3 cron常用參數(shù)詳解

4.3.4 定時(shí)實(shí)戰(zhàn)操作

#通過(guò)ansible 給主機(jī)172.16.1.41添加一個(gè)定時(shí)任務(wù) 描述信息為 By Kaile uptime 每隔一個(gè)小時(shí)五分鐘更新一次系統(tǒng)時(shí)間

 

#首先可以管理機(jī)器上進(jìn)行測(cè)試

ansible 172.16.1.41 -C -m cron -a "name='ByKaile uptime' minute=5 job='/usr/sbin/ntpdate

time.nist.gov >/dev/null 2>&1'"

 

#172.16.1.4主機(jī)創(chuàng)建定時(shí)任務(wù)  

[root@m01 /]# ansible 172.16.1.41 -m cron -a"name='By Kaile uptime' minute=5 job='/usr/sbin/ntpdate time.nist.gov>/dev/null 2>&1'"

172.16.1.41 | SUCCESS => {

   "changed": true,

    "envs": [],

   "jobs": [

       "By Kaile uptime"

    ]

}

 

#檢查定時(shí)任務(wù)

[root@m01 /]# ansible 172.16.1.41 -a"crontab -l"

172.16.1.41 | SUCCESS | rc=0 >>

#Ansible: By Kaile uptime

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

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

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

 

root@m01 /]# ansible  172.16.1.31 -m cron -a "name='timecrontab'  disabled='yes'job='/usr/sbin/ntpdate  time.nist.gov>/dev/null 2>&1'"

172.16.1.31 | SUCCESS => {

   "changed": true,

   "envs": [],

   "jobs": [

       "time crontab"

    ]

}

[root@m01 /]# ansible  172.16.1.31 -a "crontab -l"

172.16.1.31 | SUCCESS | rc=0 >>

#crond-id-001:time sync  By Kaile

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

 

#-002:time backup

00 00 * * * /bin/sh /server/scripts/nfs_back.sh>/dev/null 2>&1

#Ansible: time crontab

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

 

定時(shí)任務(wù)開啟注釋信息

[root@m01 /]# ansible  172.16.1.31 -m cron -a "name='timecrontab'  disabled='no'job='/usr/sbin/ntpdate  time.nist.gov>/dev/null 2>&1'"

172.16.1.31 | SUCCESS => {

   "changed": true,

   "envs": [],

   "jobs": [

       "time crontab"

    ]

}

[root@m01 /]# ansible  172.16.1.31 -a "crontab -l"

172.16.1.31 | SUCCESS | rc=0 >>

#crond-id-001:time sync  By Kaile

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

 

#-002:time backup

00 00 * * * /bin/sh /server/scripts/nfs_back.sh>/dev/null 2>&1

#Ansible: time crontab

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

 

4.4 環(huán)境準(zhǔn)備IP列表

服務(wù)器說(shuō)明

外網(wǎng)IP

內(nèi)網(wǎng)IP

主機(jī)名

nginx web

10.0.0.8/24

172.16.1.7/24

web02

NFS存儲(chǔ)服務(wù)器

10.0.0.31/24

172.16.1.31/24

nfs01

rsync備份服務(wù)器

10.0.0.41/24

172.16.1.41/24

backup

管理服務(wù)器

10.0.0.61/24

172.16.1.61/24

m01

第5章 ansible基礎(chǔ)配置

5.1 ansibile命令語(yǔ)法格式

5.2 常用參數(shù)模塊

5.3 ansible的配置文件:

[root@m01 ~]# tree /etc/ansible/

/etc/ansible/

├── ansible.cfg     #ansible配置文件

├── hosts           #ansible管理的主機(jī)名單(分組)

└── roles          

 

1 directory, 2 files

5.4 編輯ansible的主機(jī)配置文件hosts,添加主機(jī)組zhaokai

[root@m01 ~]# cp /etc/ansible/hosts{,.bak}  #改前備份可是個(gè)好習(xí)慣

[root@m01 ~]# tail -6 /etc/ansible/hosts

[guanli]

172.16.1.31

172.16.1.41

172.16.1.61

172.16.1.7

5.5 進(jìn)行測(cè)試

 

如果設(shè)置了ssh密鑰連接的話,hosts文件到這里就算配置完畢了。但是我們還沒有設(shè)置,因此還需要對(duì)ansible的主機(jī)映射文件/etc/ansible/hosts繼續(xù)加工

[root@m01 ~]# tail -6 /etc/ansible/hosts

[zhaokai]

172.16.1.31 ansible_ssh_user=root ansible_ssh_pass=登錄密碼

172.16.1.41 ansible_ssh_user=root ansible_ssh_pass=登錄密碼

172.16.1.61 ansible_ssh_user=root ansible_ssh_pass=登錄密碼

172.16.1.7 ansible_ssh_user=root ansible_ssh_pass=登錄密碼

172.16.1.8 ansible_ssh_user=root ansible_ssh_pass=登錄密碼

 

命令說(shuō)明:

ansible_ssh_user:ssh連接的用戶名

ansible_ssh_pass:ssh連接的密碼

注意:
如果沒有做密鑰認(rèn)證,hosts又沒有如上方式配置的話,ansible進(jìn)行遠(yuǎn)程連接是會(huì)失敗的。

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

語(yǔ)法:

ansible zhaokai -m command -a 'uptime'
ansible
主機(jī)組 -m ansible內(nèi)置功能模塊名 -a 命令

ansible命令測(cè)試

示例1:獲取172.16.1.8的主機(jī)的w信息

[root@m01 ~]# ansible 172.16.1.8 -m command -a"w"

172.16.1.8 | SUCCESS | rc=0 >>

 13:44:07 up  5:323 users, load average:0.00, 0.01, 0.05

USER    TTY      FROM              LOGIN@   IDLE  JCPU   PCPU WHAT

root    tty1     -                Sat09   14:16   0.070.07s -bash

root    pts/0    172.16.1.1      Mon23   13:290.08s  0.00s -bash

root    pts/1    m01              13:44    0.000.140.00s /bin/sh -c /usr

 

[root@m01 ~]#

5.7獲取整個(gè)zhaokai主機(jī)組的對(duì)應(yīng)的“w”信息

[root@m01 ~]# ansible zhaokai -m command -a "w"

172.16.1.8 | SUCCESS | rc=0 >>

 13:45:12 up  5:333 users, load average:0.00, 0.01, 0.05

USER    TTY      FROM              LOGIN@   IDLE  JCPU   PCPU WHAT

root    tty1     -                Sat09   15:21   0.070.07s -bash

root    pts/0    172.16.1.1      Mon23   13:300.08s  0.00s -bash

root    pts/1   m01              13:45    0.000.120.00s /bin/sh -c /usr

 

172.16.1.61 | SUCCESS | rc=0 >>

 21:05:58 up  6:343 users, load average:0.00, 0.01, 0.05

USER    TTY      FROM              LOGIN@   IDLE  JCPU   PCPU WHAT

root    tty1     -                14:41    4:220.070.07s -bash

root    pts/0    172.16.1.1       16:44    2.001.110.67s /usr/bin/python

root    pts/8   m01              21:05    1.000.100.00s /bin/sh -c /usr

 

172.16.1.7 | SUCCESS | rc=0 >>

 12:05:07 up  1:052 users, load average:0.00, 0.01, 0.05

USER    TTY      FROM              LOGIN@   IDLE  JCPU   PCPU WHAT

root    tty1     -                11:00   26:21   0.09s  0.09s -bash

root    pts/0   m01              12:05    0.000.300.00s /bin/sh -c /usr

 

172.16.1.41 | SUCCESS | rc=0 >>

 22:36:51 up 18:392 users, load average:0.00, 0.01, 0.05

USER    TTY      FROM              LOGIN@   IDLE  JCPU   PCPU WHAT

root    tty1     -                03:58   58:54   0.110.11s -bash

root    pts/0   m01              22:36    0.000.320.00s /bin/sh -c /usr

 

172.16.1.31 | SUCCESS | rc=0 >>

 13:45:13 up  6:492 users, load average:0.00, 0.01, 0.05

USER    TTY      FROM              LOGIN@   IDLE  JCPU   PCPU WHAT

root    tty1     -                Mon20   13:42   0.150.15s -bash

root    pts/0   m01              13:45    0.000.300.00s /bin/sh -c /usr

 

[root@m01 ~]#

5.8調(diào)用ansible內(nèi)置的copy模塊

[root@m01 ~]# ansible 172.16.1.8 -m copy -a"src=/etc/hosts dest=/tmp"                  #輸入命令

172.16.1.8 | SUCCESS => {

    "changed": true,

    "checksum":"dba0126bf49ea8d4cdc476828f9edb37085c6afe",

    "dest":"/tmp/hosts",

    "gid":0,

    "group":"root",    #文件復(fù)制過(guò)去以后的屬組

    "md5sum":"09bad48d0c62411850fd04b68f836335",

    "mode":"0644",     #文件復(fù)制過(guò)去以后的權(quán)限

    "owner":"root",    #文件復(fù)制過(guò)去以后的屬主

    "secontext":"unconfined_u:object_r:admin_home_t:s0",

    "size":294,

    "src":"/root/.ansible/tmp/ansible-tmp-1489411262.1-267125154401179/source",

    "state":"file",

    "uid":0

}

[root@m01 ~]# ansible 172.16.1.8 -m command -a "ls/tmp" #查看一下對(duì)方主機(jī)目錄下有啥

172.16.1.8 | SUCCESS | rc=0 >>     

ansible_0qlGau

hosts               #在這里呢,拷貝成功

pulse-d3qHAaSjkIhZ

pulse-PbcqlrG9QxEK

virtual-root.yrc60j

yum.log

 

[root@m01 ~]# ssh root@172.16.1.8 "ls /tmp" #ssh再看一下

`root@172.16.1.8's password: `

hosts               #在這里呢拷貝成功

pulse-d3qHAaSjkIhZ

pulse-PbcqlrG9QxEK

virtual-root.yrc60j

yum.log

[root@m01 ~]#

 

 

 

命令說(shuō)明:

-m:調(diào)用ansible內(nèi)置模塊   copy  拷貝模塊

-a:接命令。由于調(diào)用了copy模塊,命令格式發(fā)生改變。src=本地文件路徑 dest=目的地所在路徑

示例4:調(diào)用copy模塊實(shí)現(xiàn)保存文件的屬性改變

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

172.16.1.8 | SUCCESS => {

    "changed": true,

    "checksum":"dba0126bf49ea8d4cdc476828f9edb37085c6afe",

    "dest":"/tmp/hosts",

    "gid":502,

    "group":"zhaokai",

    "mode":"0600",

    "owner":"zhaokai",

    "path":"/tmp/hosts",

    "secontext":"unconfined_u:object_r:admin_home_t:s0",

    "size":294,

    "state":"file",

    "uid":502

}

[root@m01 ~]# ssh root@172.16.1.8 "ls -l/tmp/hosts"

`root@172.16.1.8's password: `

-rw-------. 1 zhaokai zhaokai 2943月  1414:00 /tmp/hosts

 

命令說(shuō)明:
對(duì)于ansible內(nèi)置模塊的使用,大家參考命令執(zhí)行后的返回信息里看就可以了。寫的很清楚。

備注:
copy
模塊,如果復(fù)制的對(duì)方主機(jī)路徑下沒有目錄,那么會(huì)遞歸創(chuàng)建

特別提示:

ansible的部分模塊并不支持
1
)管道符 “|”
2
)重定向 “> < >> <<”
3)
類似top,tail -f這種不能即刻返回明確信息的命令
4
*

5.9利用ansible遠(yuǎn)程執(zhí)行各類腳本

5.10先將腳本分發(fā)到各個(gè)機(jī)器上去

[root@m01 ~]# echo "echo '測(cè)試成功!'" >>/server/scripts/test.sh

ansible zhaokai -m copy -a"src=/server/scripts/yum.sh dest=/server/scripts/ mode=0755backup=yes"

注意:dest路徑的寫法,若是不存在的目錄,結(jié)尾要加斜線(/server/scripts/),否則默認(rèn)不會(huì)創(chuàng)建目標(biāo)目錄

5.11遠(yuǎn)程批量執(zhí)行腳本

ansible zhaokai -m shell -a"/server/scripts/yum.sh"

示例:

[root@m01 ~]# ansible zhaokai -m command -a "sh/server/scripts/test.sh"

172.16.1.41 | SUCCESS | rc=0 >>

測(cè)試成功!

 

172.16.1.31 | SUCCESS | rc=0 >>

測(cè)試成功!

 

172.16.1.7 | SUCCESS | rc=0 >>

測(cè)試成功!

 

172.16.1.8 | SUCCESS | rc=0 >>

測(cè)試成功!

 

172.16.1.61 | SUCCESS | rc=0 >>

測(cè)試成功!

5.12 常用模塊:每個(gè)模塊就是一個(gè)功能

模塊名

作用

command

執(zhí)行命令模塊(重要)

copy

文件拷貝模塊(重要)

shell

執(zhí)行shell腳本模塊(重要)

script

執(zhí)行shell腳本模塊(重要)

file

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

service

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

cron

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

yum

yum軟件包安裝管理模塊

synchronize

使用rsync同步文件模塊

第6章 ansible劇本開始

絕對(duì)路徑:進(jìn)入目錄執(zhí)行一次,下一次如果還需要進(jìn)入這個(gè)目錄,需要再次用絕對(duì)路徑進(jìn)入執(zhí)行命令。

執(zhí)行劇本的另一臺(tái)機(jī)器,如果在掛載中,需要檢查掛載,卸載,不然不執(zhí)行卡主。

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

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

    yaml使用一個(gè)固定的縮進(jìn)風(fēng)格表示數(shù)據(jù)層結(jié)構(gòu)關(guān)系,Saltstack/ansible需要每個(gè)縮進(jìn)級(jí)別由


兩個(gè)空格組成。一定不能使用tab

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

    yaml:

   mykey: my_value

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


格)

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

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


一部分 - hosts

   

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

     

---

### 劇本的開頭,可以不寫

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

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

- command: echo hello oldboy linux.                  (空格)(空格)空格)(空格)-(


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

          

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

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

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

- name:

command: echo hello oldboy linux.                  (空格)(空格)空格)(空格)-(


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

 

6.7     劇本編寫內(nèi)容擴(kuò)展:劇本任務(wù)編寫定時(shí)任務(wù)

# ansible all -m cron -a "name='restartnetwork' minute=00 hour=00


#job='/usr/sbin/ntpdate time.nist.gov>/dev/null 2>&1'"

       -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'

    #ansible-playbook -C /etc/ansible/network-restart.yml  -vvvx

      說(shuō)明:測(cè)試劇本命令后面可以跟多個(gè)-v進(jìn)行調(diào)試檢查

     

6.8      劇本編寫內(nèi)容擴(kuò)展:劇本任務(wù)編寫多個(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/ntpdatepool.ntp.com >/dev/null 2>&1"

 

6.9      劇本編寫內(nèi)容擴(kuò)展:劇本任務(wù)編寫多個(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/ntpdatepool.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

 

第7章 常用模塊

 

ansible oldboy -m authorized_key -a "key='公鑰文件里面的內(nèi)容,鎖頭里面的內(nèi)容'  user=root" -k

 

 

#實(shí)戰(zhàn)操作

[root@m01 .ssh]# ansible 172.16.1.41 -mauthorized_key -a "key='ssh-dss AAAAB3NzaC1kc3MAAACBAOkVRb7MpNPcYR6HbR5Q6iT2028sSgNlaKX8yXEHDp/YGuobBzYvHKvLMhokak4Eo2DE/g9n29z+J4krWGpWZz9680YaT8T8Z0qmMXorK44FuAQqcifoyvMlW97DPvF+H8ilYNHnSE1D2EY4TXU1ruNPGAeaqLaUr6xcP7enc+hVAAAAFQCv0mTqYI6uraAAwV+YqV0fwynuSwAAAIEA54se+MAC8WxsugYsVEkvCnSWhAhB/KLsBCk5k7Y8pTdirnJ/ZviXM2/GTsPYdMHw9cZFyT4L3lsflorcj9gjsavWiyK9/GDerBe1vXg6JyRXstgWax2hIpH702dzSuENXSlbTua1tGNdBGt2A+mKQouBKqGMgyz3SWoF3CEb0gkAAACBAIUnpl1V9+w4tgccRoVUcnRx7H3hkaVSGzFXz1KxNGyASWVtgBdDn9+VT4T+U9JE9hlwKx4s9ySRyYvivZnT/6kZODleFVvVckr8jEdV/ILlesu+wQ31kLETiaOli5EGCyJmTNNK6x2Uz1Dq3rK4Rlb+tBSH3rMLzFp4aYBP+i+9root@m01'  user=root" -k

SSH password:

 


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI