Asible學(xué)習(xí)筆記--常用模塊(一)

Ansible常用模塊

本節(jié)包括的模塊:

創(chuàng)新互聯(lián)專(zhuān)注為客戶(hù)提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站建設(shè)、做網(wǎng)站、東城網(wǎng)絡(luò)推廣、小程序開(kāi)發(fā)、東城網(wǎng)絡(luò)營(yíng)銷(xiāo)、東城企業(yè)策劃、東城品牌公關(guān)、搜索引擎seo、人物專(zhuān)訪(fǎng)、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供東城建站搭建服務(wù),24小時(shí)服務(wù)熱線(xiàn):13518219792,官方網(wǎng)址:muchs.cn

(1)shell和command

(2)復(fù)制模塊copy

(3)template模塊

(4)文件模塊file

(5)拉取文件模塊fetch

(6)rsync模塊synchronize

可以從ansible-doc -l | grep module_name來(lái)找出想要的模塊。再使用ansible-doc -s module_name來(lái)查看此模塊的用法。

官方模塊列表和說(shuō)明: https://docs.ansible.com/ansible/latest/modules_by_category.html

關(guān)于模塊的使用方法,需要注意的是state。很多模塊都會(huì)有該選項(xiàng),且其值幾乎都包含有presentabsent, 表示肯定和否定的意思。

ansible絕大多數(shù)模塊都天然具有 冪等 特性,只有極少數(shù)模塊如shell和command模塊不具備冪等性。所謂的冪等性是指多次執(zhí)行同一個(gè)操作不會(huì)影響最終結(jié)果。例如,ansible的yum模塊安裝rpm包時(shí),如果待安裝的包已經(jīng)安裝過(guò)了,則再次或多次執(zhí)行安裝操作都不會(huì)真正的執(zhí)行下去。

再例如,copy模塊拷貝文件時(shí),如果目標(biāo)主機(jī)上已經(jīng)有了完全相同的文件,則多次執(zhí)行copy模塊不會(huì)真正的拷貝。ansible具有冪等性的模塊在執(zhí)行時(shí),都會(huì)自動(dòng)判斷是否要執(zhí)行。

shell和command

默認(rèn)ansible使用的模塊是command,即可以執(zhí)行一些shell命令。shell和command的用法基本一樣,實(shí)際上shell模塊執(zhí)行命令的方式是在遠(yuǎn)程使用/bin/sh來(lái)執(zhí)行的,如/bin/sh ping。

command不能解析變量如$HOME和某些操作符"<", ">", "|", ";"以及"&",所以明確要使用這些不可解析的操作符時(shí),使用shell模塊來(lái)代替command。

shell--Ansible官方使用說(shuō)明文檔

command--Ansible官方使用說(shuō)明文檔

ansible-doc -s shell
- name: Execute commands in nodes.
       action: shell
    chdir          # 在執(zhí)行命令前,先cd到指定的目錄下
    creates        # 用于判斷命令是否要執(zhí)行。如果指定的文件(可以使用通配符)存在,則不執(zhí)行。
    removes        # 用于判斷命令是否要執(zhí)行。如果指定的文件(可以使用通配符)不存在,則不執(zhí)行。
    executable     # 不再使用默認(rèn)的/bin/sh解析并執(zhí)行命令,而是使用此處指定的命令解析。
                   # 例如使用expect解析expect腳本。必須為絕對(duì)路徑。

在ansible中使用shell或command模塊一定要注意,它們默認(rèn)不滿(mǎn)足冪等性,很多操作會(huì)重復(fù)執(zhí)行,但有些操作是不允許重復(fù)執(zhí)行的。例如MySQL的初始化命令mys ql_ins t all_db,它只能在第一次配置的過(guò)程中初始化一次,其他任何時(shí)候如非需要?jiǎng)t不允許執(zhí)行。這時(shí)候要實(shí)現(xiàn)冪等性,可以通過(guò)模塊的createsremoves選項(xiàng)進(jìn)行判斷,但無(wú)論如何,在執(zhí)行這兩個(gè)模塊的時(shí)候都需要考慮要執(zhí)行的命令是否應(yīng)該實(shí)現(xiàn)冪等性。

例子如下:

tasks:
  - shell: touch helloworld.txt creates=/tmp/hello.txt

但建議,在參數(shù)可能產(chǎn)生歧義的情況下,使用args來(lái)傳遞ansible的參數(shù)。如:

- shell: touch helloworld.txt
  args:
    creates: /tmp/hello.txt
# You can use shell to run other executables to perform actions inline
- name: Run expect to wait for a successful PXE boot via out-of-band CIMC
  shell: |
    set timeout 300
    spawn ssh admin@{{ cimc_host }}
    expect "password:"
    send "{{ cimc_password }}\n"
    expect "\n{{ cimc_name }}" send "connect host\n"
    expect "pxeboot.n12" send "\n"
    exit 0
  args:
    executable: /usr/bin/expect 
  delegate_to: localhost

復(fù)制模塊copy

copy--Ansible官方使用說(shuō)明文檔

ansible-doc -l |grep copy

使用方法:

ansible-doc -s copy

ansible-doc -s copy
- name: Copy files to remote locations
  copy:
      backup=[yes|no]:   # 拷貝的同時(shí)也創(chuàng)建一個(gè)包含時(shí)間戳信息的備份文件,默認(rèn)為no
      dest:              # 目標(biāo)路徑,只能是絕對(duì)路徑,如果拷貝的文件是目錄,則目標(biāo)路徑必須也是目錄
      content:           # 直接以content給定的字符串或變量值作為文件內(nèi)容保存到遠(yuǎn)程主機(jī)上,它會(huì)替代src選項(xiàng)
      directory_mode:    # 當(dāng)對(duì)目錄做遞歸拷貝時(shí),設(shè)置了directory_mode將會(huì)使得只拷貝新建文件,
                         # 舊文件不會(huì)被拷貝。默認(rèn)未設(shè)置
      follow=[yes|no]:   # 是否追蹤到鏈接的源文件。
      force=[yes|no]:    # 設(shè)置為yes(默認(rèn))時(shí),將覆蓋遠(yuǎn)程同名文件。設(shè)置為no時(shí),忽略同名文件的拷貝
      group:             # 設(shè)置遠(yuǎn)程文件的所屬組
      owner:             # 設(shè)置遠(yuǎn)程文件的所有者
      mode:              # 設(shè)置遠(yuǎn)程文件的權(quán)限。使用數(shù)值表示時(shí)不能省略第一位,如0644。
                         # 也可以使用'u+rwx'或'u=rw,g=r,o=r'等方式設(shè)置。
      src:               # 拷貝本地源文件到遠(yuǎn)程,可使用絕對(duì)路徑或相對(duì)路徑。如果路徑是目錄,且目錄后加了
                         # 斜杠"/",則只會(huì)拷貝目錄中的內(nèi)容到遠(yuǎn)程,如果目錄后不加斜杠,則拷貝目錄本身和
                         # 目錄內(nèi)的內(nèi)容到遠(yuǎn)程。

默認(rèn)情況下,ansible copy會(huì)檢查文件md5查看是否需要拷貝,相同則不會(huì)拷貝,否則會(huì)拷貝。如果設(shè)置force=yes,則當(dāng)文件md5不同時(shí)(即文件內(nèi)容不同)才覆蓋拷貝,設(shè)置force=no時(shí),則只拷貝對(duì)方?jīng)]有的文件。

關(guān)于 copy 模塊的backup這里使用幾個(gè)例子說(shuō)明下:

(1)當(dāng)目標(biāo)機(jī)上沒(méi)有待copy的文件時(shí),即使設(shè)置了backup=yes也是沒(méi)有什么用處的,如:

ansible test -m copy -a "src=/tmp/temp/test.pub dest=/tmp backup=yes" -o -f 6

執(zhí)行完之后,目標(biāo)機(jī) /tmp 目錄下只有一個(gè) copy 過(guò)去的 test.pub 文件,并沒(méi)有像 test.pub.3286.2019-11-14@11:22:34~這樣的備份文件出現(xiàn)~

(2)當(dāng)目標(biāo)機(jī)上已經(jīng)有待copy的文件時(shí),但server端與目標(biāo)機(jī)上該文件是相同時(shí)(即server端該文件未更改),即使設(shè)置了backup=yes也是沒(méi)有什么用處的。

(3)當(dāng)目標(biāo)機(jī)上已經(jīng)有待copy的文件時(shí),但server端與目標(biāo)機(jī)上該文件是不同時(shí)(即server端該文件做了更改),設(shè)置了backup=yes,備份文件則會(huì)在目標(biāo)機(jī)上目標(biāo)文件路徑下出現(xiàn),如:

# 我們更改下server端 test.pub 文件的內(nèi)容,然后執(zhí)行ansible
ansible test -m copy -a "src=/tmp/temp/test.pub dest=/tmp backup=yes" -o -f 6

# 我們登入到目標(biāo)機(jī)上 /tmp 目錄下查看,會(huì)發(fā)現(xiàn)有一個(gè)備份文件出現(xiàn),即 test.pub.3286.2019-11-14@11:22:34~ ,它的內(nèi)容就是上次 server 端copy過(guò)來(lái)的文件內(nèi)容~

如果拷貝的是目錄,則目標(biāo)路徑必須是目錄路徑。如果使用"/"結(jié)尾,則拷貝的是目錄中的文件,如果不以斜杠結(jié)尾,則拷貝的是目錄加目錄中的文件。舉例如下:

(1)情況一:使用"/"結(jié)尾

## server端

# pwd
/tmp/temp
# ll
-rw-r--r-- 1 root root    0 11月 13 11:18 a.log
-rwxr-xr-x 1 root root  338 11月 13 11:31 auto_sshcopyid.exp
-rw------- 1 root root 1679 11月 13 10:31 id_rsa
-rw-r--r-- 1 root root  395 11月 13 10:31 id_rsa.pub
-rw-r--r-- 1 root root  416 11月 13 11:27 sshkey.sh
-rw------- 1 root root 1679 11月 13 10:34 test
-rw-r--r-- 1 root root  342 11月  5 10:48 test01.py
-rw-r--r-- 1 root root   75 11月  5 14:23 test02.py
-rw-r--r-- 1 root root  299 11月  5 16:18 test03.py
-rw-r--r-- 1 root root  371 11月  5 18:52 test04.py
-rw-r--r-- 1 root root  217 11月  5 22:26 test05.py
-rw-r--r-- 1 root root   60 11月  5 22:26 test06.py
-rw-r--r-- 1 root root  406 11月 14 11:22 test.pub

執(zhí)行使用"/"結(jié)尾,則拷貝的是目錄中的文件:

ansible test -m copy -a "src=/tmp/temp/ dest=/tmp/test" -o -f 6

查看目標(biāo)機(jī) /tmp/test 目錄的內(nèi)容:

# pwd
/tmp/test
# ll
總用量 48
-rw-r--r-- 1 root root    0 11月 14 11:39 a.log
-rw-r--r-- 1 root root  338 11月 14 11:39 auto_sshcopyid.exp
-rw-r--r-- 1 root root 1679 11月 14 11:39 id_rsa
-rw-r--r-- 1 root root  395 11月 14 11:39 id_rsa.pub
-rw-r--r-- 1 root root  416 11月 14 11:39 sshkey.sh
-rw-r--r-- 1 root root 1679 11月 14 11:39 test
-rw-r--r-- 1 root root  342 11月 14 11:39 test01.py
-rw-r--r-- 1 root root   75 11月 14 11:39 test02.py
-rw-r--r-- 1 root root  299 11月 14 11:39 test03.py
-rw-r--r-- 1 root root  371 11月 14 11:39 test04.py
-rw-r--r-- 1 root root  217 11月 14 11:39 test05.py
-rw-r--r-- 1 root root   60 11月 14 11:39 test06.py
-rw-r--r-- 1 root root  406 11月 14 11:39 test.pub

(2)情況二:不以斜杠結(jié)尾

執(zhí)行不以斜杠"/"結(jié)尾,則拷貝的是目錄加目錄中的文件:

ansible test -m copy -a "src=/tmp/temp dest=/tmp/test01 backup=yes" -o -f 6

查看目標(biāo)機(jī) /tmp/test01 目錄的內(nèi)容:

# pwd
/tmp/test01
# ll
總用量 0
drwxr-xr-x 2 root root 224 11月 14 11:40 temp
# cd temp
# pwd
/tmp/test01/temp
# ll
總用量 48
-rw-r--r-- 1 root root    0 11月 14 11:40 a.log
-rw-r--r-- 1 root root  338 11月 14 11:40 auto_sshcopyid.exp
-rw-r--r-- 1 root root 1679 11月 14 11:40 id_rsa
-rw-r--r-- 1 root root  395 11月 14 11:40 id_rsa.pub
-rw-r--r-- 1 root root  416 11月 14 11:40 sshkey.sh
-rw-r--r-- 1 root root 1679 11月 14 11:40 test
-rw-r--r-- 1 root root  342 11月 14 11:40 test01.py
-rw-r--r-- 1 root root   75 11月 14 11:40 test02.py
-rw-r--r-- 1 root root  299 11月 14 11:40 test03.py
-rw-r--r-- 1 root root  371 11月 14 11:40 test04.py
-rw-r--r-- 1 root root  217 11月 14 11:40 test05.py
-rw-r--r-- 1 root root   60 11月 14 11:40 test06.py
-rw-r--r-- 1 root root  406 11月 14 11:40 test.pub

template模塊

template模塊用法和copy模塊用法基本一致,它主要用于復(fù)制配置文件。template--Ansible官方使用說(shuō)明文檔

ansible-doc -s template
- name: Template a file out to a remote server
  template:
      backup:   # 拷貝的同時(shí)也創(chuàng)建一個(gè)包含時(shí)間戳信息的備份文件,默認(rèn)為no
      dest:     # 目標(biāo)路徑
      force:    # 設(shè)置為yes (默認(rèn))時(shí),將覆蓋遠(yuǎn)程同名文件。設(shè)置為no時(shí),忽略同名文件的拷貝
      group:    # 設(shè)置遠(yuǎn)程文件的所屬組
      owner:    # 設(shè)置遠(yuǎn)程文件的所有者
      mode:     # 設(shè)置遠(yuǎn)程文件的權(quán)限。使用數(shù)值表示時(shí)不能省略第一位,如0644。
                # 也可以使用'u+rwx' or 'u=rw,g=r,o=r'等方式設(shè)置
      src:      # ansible控制器上Jinja2格式的模板所在位置,可以是相對(duì)或絕對(duì)路徑
      validate: # 在復(fù)制到目標(biāo)主機(jī)后但放到目標(biāo)位置之前,執(zhí)行此選項(xiàng)指定的命令。
                # 一般用于檢查配置文件語(yǔ)法,語(yǔ)法正確則保存到目標(biāo)位置。
                # 如果要引用目標(biāo)文件名,則使用%s,下面的示例中的%s即表示目標(biāo)機(jī)器上的/etc/nginx/nginx.conf。

示例如下:

ansible centos -m template -a "src=/tmp/nginx.conf.j2 dest=/etc/nginx/nginx.conf mode=0770 owner=root group=root backup=yes validate='nginx -t -c %s'" -o -f 6

雖然template模塊可以按需求修改配置文件內(nèi)容來(lái)復(fù)制模板到被控主機(jī)上,但是有一種情況它是不能解決的:不同被控節(jié)點(diǎn)所需的配置文件差異很大,并非修改幾個(gè)變量就可以滿(mǎn)足。例如在centos 6和centos 7上通過(guò)yum安裝的 nginx,它們的配置文件內(nèi)容相差非常大,且centos 6上的nginx的默認(rèn)就有一個(gè)/etc/nginx/conf.d/default.conf。 如果直接復(fù)制同一個(gè)模板的nginx配置文件到centos 6和centos 7上,很可能導(dǎo)致某一版本的nginx不能啟動(dòng)。

這時(shí)就有必要在復(fù)制模板時(shí)挑選對(duì)應(yīng)發(fā)行版的模板文件進(jìn)行配對(duì)復(fù)制,例如要復(fù)制到 centos 6上的源模板是 nginx6.conf.j2,復(fù)制到centos 7上的源模板是nginx7.conf.j2。這種行為可以稱(chēng)之為"基于變量選擇文件或模板"。

---
 - tasks:
     - name: template file based var
       template: src=/templates/nginx{{ ansible_distribution_major_version }}.conf.j2 dest=/etc/nginx/nginx.conf validate="/usr/sbin/nginx -t -c %s"

還可以在文件內(nèi)容中指定jinja2的替代變量,在ansible執(zhí)行時(shí)首先會(huì)根據(jù)變量?jī)?nèi)容進(jìn)行渲染,渲染后再執(zhí)行相關(guān)模塊。例如,此處的template模塊,復(fù)制一個(gè)基于發(fā)行版本號(hào)的yum源配置文件。以下是某個(gè)repo文件模板 base.repo.j2的內(nèi)容。

[epel]
name=epel
baseurl=http://mirrors.aliyun.com/epel/{{ ansible_distribution_major_version }}Server/x86_64/ enable=1
gpgcheck=0

再?gòu)?fù)制即可。

---
 - tasks:
     - template: src=my.repo.j2 dest=/etc/yum.repos.d/my.repo

文件模塊file

管理文件、目錄的屬性,也可以創(chuàng)建文件或目錄。file--Ansible官方使用說(shuō)明文檔

ansible-doc -s file
- name: Manage files and file properties
  file:
      group:   # file/directory的所屬組
      owner:   # file/directory的所有者
      mode:    # 修改權(quán)限,格式可以是0644、'u+rwx'或'u=rw,g=r,o=r'等
      path:    # 指定待操作的文件,可使用別名'dest'或'name'來(lái)替代path
      recurse: # (默認(rèn)no)遞歸修改文件的屬性信息,要求state=directory
      src:     # 要鏈接到的文件的路徑。
               # 這只適用于state=link和state=hard。
               # 對(duì)于state=link,這也將接受一個(gè)不存在的路徑。
               # 相對(duì)路徑相對(duì)于正在創(chuàng)建的文件(路徑),這是Unix命令ln -s SRC DEST處理相對(duì)路徑的方式。
      state:   # directory:如果目錄不存在則遞歸創(chuàng)建
               # file:文件不存在時(shí),不會(huì)被創(chuàng)建(默認(rèn)值)
               # touch:touch由path指定的文件,即創(chuàng)建一個(gè)新文件,或修改其mtime和atime
               # link:修改或創(chuàng)建軟鏈接
               # hard:修改或創(chuàng)建硬鏈接
               # absent:目錄和其中的文件會(huì)被遞歸刪除,文件或鏈接將取消鏈接狀態(tài)

需要注意的是,file模塊可以遞歸創(chuàng)建目錄,但是不能在不存在的目錄中創(chuàng)建文件,只能先創(chuàng)建目錄,再在此目錄中創(chuàng)建文件。我們做個(gè)測(cè)試驗(yàn)證下:

# 被控制機(jī)上并不存在/root/test這個(gè)目錄
# 使用ansible在被控制機(jī)上/root/test目錄下創(chuàng)建foo.conf
ansible test -m file -a "path=/root/test/foo.conf owner=duser group=duser mode='0644' state=touch"

ansible執(zhí)行結(jié)果是:

192.168.246.187 | FAILED! => {
    "changed": false,    ## 失敗
    "msg": "Error, could not touch target: [Errno 2] 沒(méi)有那個(gè)文件或目錄: b'/root/test/foo.conf'",
    "path": "/root/test/foo.conf"
}
# 現(xiàn)在我們?cè)诒豢刂茩C(jī)上創(chuàng)建/root/test目錄
# 再次執(zhí)行ansible
ansible test -m file -a "path=/root/test/foo.conf owner=duser group=duser mode='0644' state=touch"

ansible執(zhí)行結(jié)果是:

192.168.246.187 | CHANGED => {
    "changed": true,  ## 成功
    "dest": "/root/test/foo.conf",
    "gid": 1009,
    "group": "duser",
    "mode": "0644",
    "owner": "duser",
    "size": 0,
    "state": "file",
    "uid": 1009
}

創(chuàng)建目錄,并遞歸修改目錄的屬性。

ansible test -m file -a "path=/tmp/xyz/test state=directory owner=root group=root mode='0755' recurse=yes"

修改目錄/tmp/xyz/test中test的權(quán)限

ansible test -m file -a "path=/tmp/xyz/test state=directory mode='0777'"

創(chuàng)建或修改文件屬性/權(quán)限

ansible test -m file -a "path=/tmp/xyz/test/wtf.txt state=touch mode='0644'"

拉取文件模塊fetch

和copy工作方式類(lèi)似,只不過(guò)是從遠(yuǎn)程主機(jī)將文件拉取到本地端,存儲(chǔ)時(shí)使用主機(jī)名作為目錄樹(shù),且只能拉取文件不能拉取目錄!

fetch--Ansible官方使用說(shuō)明文檔

ansible-doc -s fetch
- name: Fetch files from remote nodes
  fetch:
      dest:              # 本地存儲(chǔ)拉取文件的目錄。例如dest=/data,src=/etc/fstab,
                         # 遠(yuǎn)程主機(jī)名host.exp.com,則保存的路徑為/data/host.exp.com/etc/fstab。
      fail_on_missing:   # 當(dāng)設(shè)置為yes時(shí),如果拉取的源文件不存在,則此任務(wù)失敗。默認(rèn)為no。
      flat:              # 改變拉取后的路徑存儲(chǔ)方式。如果設(shè)置為yes,且當(dāng)dest以"/"結(jié)尾時(shí),將直接把源文件
                         # 的basename存儲(chǔ)在dest下。顯然,應(yīng)該考慮多個(gè)主機(jī)拉取時(shí)的文件覆蓋情況。
      src:               # 遠(yuǎn)程主機(jī)上的源文件。只能是文件,不支持目錄。在未來(lái)的版本中可能會(huì)支持目錄遞歸拉取。
      validate_checksum: # fetch到文件后,檢查其md5和源文件是否相同。

存儲(chǔ)為/tmp/192.168.246.187/etc/fstab:

ansible test -m fetch -a "src=/etc/fstab dest=/tmp"

存儲(chǔ)為/tmp/fstab:

ansible test -m fetch -a "src=/etc/fstab dest=/tmp/ flat=yes"

存儲(chǔ)為/tmp/fstab-192.168.246.187:

ansible test -m fetch -a "src=/etc/fstab dest=/tmp/fstab-{{inventory_hostname}} flat=yes"

這里說(shuō)明一點(diǎn):

上面中的{{inventory_hostname}}指的是/etc/ansible/hosts中的主機(jī)別名,如:

# /etc/ansible/hosts如下定義:
[test]
192.168.246.187
[test:vars]
ansible_ssh_private_key_file=/root/.ssh/rsa_back/id_rsa
ansible_python_interpreter=/usr/local/python3/bin/python3

這種情況下控制端生成的文件名就是/tmp/fstab-192.168.246.187。

# /etc/ansible/hosts如下定義:
[test]
nginx ansible_ssh_host=192.168.246.187
[test:vars]
ansible_ssh_private_key_file=/root/.ssh/rsa_back/id_rsa
ansible_python_interpreter=/usr/local/python3/bin/python3

這種情況下控制端生成的文件名就是/tmp/fstab-nginx。

rsync模塊synchronize

synchronize模塊用于實(shí)現(xiàn)rsync的簡(jiǎn)單版常用功能,它無(wú)法實(shí)現(xiàn)完整版的rsync,畢竟rsync功能太多太細(xì)致。如果要使用rsync,還是應(yīng)該使用command或shell模塊來(lái)調(diào)用rsync命令。

完整的rsync功能見(jiàn)rsync命令中文手冊(cè)。

ansible-doc -s synchronize
- name: A wrapper around rsync to make common tasks in your playbooks quick and easy
  synchronize:
      src:           # 指定待傳輸?shù)脑次募???梢允窍鄬?duì)路徑,也可以是絕對(duì)路徑。
      dest:          # 目標(biāo)路徑??梢允墙^對(duì)路徑,也可以是相對(duì)路徑。
      mode:          # 指定推(push)還是拉(pull)的傳輸模式。
                     # push時(shí),本地為sender端,pull時(shí),遠(yuǎn)程為sender端。默認(rèn)為push。
      archive:       # 等價(jià)于rsync的"-a"選項(xiàng),即使用歸檔模式。它等價(jià)于rsync的"-rtopgDl"選項(xiàng)。值為yes/no。
      times:         # 保留mtime屬性,值為yes/no。
      group:         # 保留所屬組屬性,值為yes/no。
      owner:         # 保留所有者屬性,值為yes/no。
      links:         # 拷貝鏈接文件自身,值為yes/no。
      perms:         # 保留權(quán)限屬性,值為yes/no。
      recursive:     # 遞歸到目錄中的文件,值為yes/no。
      compress:      # 傳輸過(guò)程中壓縮傳輸。應(yīng)該總是開(kāi)啟,除非遇到問(wèn)題。即rsync的"-z"選項(xiàng)。值為yes/no,默認(rèn)是yes。
      copy_links:   # 拷貝軟鏈接的文件名和其指向的文件的內(nèi)容。即a指向b文件時(shí),將在目標(biāo)端生成a普通
                     # 文件,但此文件中的內(nèi)容是b中的內(nèi)容。
      dirs:         # 非遞歸方式傳輸目錄。
      delete:       # 目標(biāo)端如果比源端文件多,則刪除這些多出來(lái)的文件,要求recursive=yes。
      checksum:     # 等價(jià)于"-c"選項(xiàng),將基于文件的checksum來(lái)判斷是否同步,而不是默認(rèn)的quick check
                     # 算法,該算法基于文件大小和最近的mtime來(lái)判斷是否要同步。該選項(xiàng)會(huì)大幅降低效率,
                     # 應(yīng)謹(jǐn)慎使用。注意,它無(wú)法影響archive,即archive仍會(huì)啟用。
      existing_only:# receiver端沒(méi)有的文件不同步。但仍會(huì)傳輸,只是臨時(shí)文件重組后不重命名而已。
      partial:      # 等價(jià)于"--partial"選項(xiàng)。默認(rèn)rsync在傳輸中斷時(shí)會(huì)刪除傳輸了一半的文件,指定該選
                     # 項(xiàng)將保留這部分不完整的文件,使得下次傳輸時(shí)可以直接從未完成的數(shù)據(jù)塊開(kāi)始傳輸。
      dest_port:    # ssh的連接端口。
      rsync_opts:   # 指定額外的rsync選項(xiàng)。使用數(shù)組的方式傳遞這些選項(xiàng)。
      rsync_path:   # 等價(jià)于"--rsync-path"選項(xiàng),目的是啟動(dòng)遠(yuǎn)程rsync。
                     # 例如可以指定[--rsync-path=rsync],甚至[--rsync-path=cd /tmp/c && rsync]。
                     # 當(dāng)不指定rsync路徑時(shí),默認(rèn)為/usr/bin/rysnc。
      rsync_timeout:# 指定rsync在多久時(shí)間內(nèi)還沒(méi)有數(shù)據(jù)傳輸就超時(shí)退出。
      verify_host:  # 對(duì)目標(biāo)主機(jī)進(jìn)行ssh的host key驗(yàn)證。

說(shuō)明

本博文是參考馬龍帥大佬文章整理生成,屬于博主讀書(shū)筆記,如有侵權(quán),請(qǐng)大佬與我聯(lián)系,立刪!

最后,感謝開(kāi)源,擁抱開(kāi)源~

當(dāng)前題目:Asible學(xué)習(xí)筆記--常用模塊(一)
網(wǎng)頁(yè)鏈接:http://muchs.cn/article44/ghcpee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、手機(jī)網(wǎng)站建設(shè)自適應(yīng)網(wǎng)站、網(wǎng)站改版網(wǎng)站制作、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名