Asible學習筆記--常用模塊(一)

Ansible常用模塊

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

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

關於模塊的使用方法,需要注意的是state。很多模塊都會有該選項,且其值幾乎都包含有presentabsent, 表示肯定和否定的意思。

ansible絕大多數模塊都天然具有 冪等 特性,只有極少數模塊如shell和command模塊不具備冪等性。所謂的冪等性是指多次執行同一個操作不會影響最終結果。例如,ansible的yum模塊安裝rpm包時,如果待安裝的包已經安裝過了,則再次或多次執行安裝操作都不會真正的執行下去。

再例如,copy模塊拷貝文件時,如果目標主機上已經有了完全相同的文件,則多次執行copy模塊不會真正的拷貝。ansible具有冪等性的模塊在執行時,都會自動判斷是否要執行。

shell和command

默認ansible使用的模塊是command,即可以執行一些shell命令。shell和command的用法基本一樣,實際上shell模塊執行命令的方式是在遠程使用/bin/sh來執行的,如/bin/sh ping。

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

shell--Ansible官方使用說明文檔

command--Ansible官方使用說明文檔

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

在ansible中使用shell或command模塊一定要注意,它們默認不滿足冪等性,很多操作會重複執行,但有些操作是不允許重複執行的。例如mysql的初始化命令mys ql_ins t all_db,它只能在第一次配置的過程中初始化一次,其他任何時候如非需要則不允許執行。這時候要實現冪等性,可以通過模塊的createsremoves選項進行判斷,但無論如何,在執行這兩個模塊的時候都需要考慮要執行的命令是否應該實現冪等性。

例子如下:

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

但建議,在參數可能產生歧義的情況下,使用args來傳遞ansible的參數。如:

- 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

複製模塊copy

copy--Ansible官方使用說明文檔

ansible-doc -l |grep copy

使用方法:

ansible-doc -s copy

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

默認情況下,ansible copy會檢查文件md5查看是否需要拷貝,相同則不會拷貝,否則會拷貝。如果設置force=yes,則當文件md5不同時(即文件內容不同)才覆蓋拷貝,設置force=no時,則只拷貝對方沒有的文件。

關於 copy 模塊的backup這裏使用幾個例子說明下:

(1)當目標機上沒有待copy的文件時,即使設置了backup=yes也是沒有什麼用處的,如:

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

執行完之後,目標機 /tmp 目錄下只有一個 copy 過去的 test.pub 文件,並沒有像 test.pub.3286.2019-11-14@11:22:34~這樣的備份文件出現~

(2)當目標機上已經有待copy的文件時,但server端與目標機上該文件是相同時(即server端該文件未更改),即使設置了backup=yes也是沒有什麼用處的。

(3)當目標機上已經有待copy的文件時,但server端與目標機上該文件是不同時(即server端該文件做了更改),設置了backup=yes,備份文件則會在目標機上目標文件路徑下出現,如:

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

# 我們登入到目標機上 /tmp 目錄下查看,會發現有一個備份文件出現,即 test.pub.3286.2019-11-14@11:22:34~ ,它的內容就是上次 server 端copy過來的文件內容~

如果拷貝的是目錄,則目標路徑必須是目錄路徑。如果使用"/"結尾,則拷貝的是目錄中的文件,如果不以斜槓結尾,則拷貝的是目錄加目錄中的文件。舉例如下:

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

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

執行使用"/"結尾,則拷貝的是目錄中的文件:

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

查看目標機 /tmp/test 目錄的內容:

# 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)情況二:不以斜槓結尾

執行不以斜槓"/"結尾,則拷貝的是目錄加目錄中的文件:

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

查看目標機 /tmp/test01 目錄的內容:

# 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模塊用法基本一致,它主要用於複製配置文件。template--Ansible官方使用說明文檔

ansible-doc -s template
- name: Template a file out to a remote server
  template:
      backup:   # 拷貝的同時也創建一個包含時間戳信息的備份文件,默認爲no
      dest:     # 目標路徑
      force:    # 設置爲yes (默認)時,將覆蓋遠程同名文件。設置爲no時,忽略同名文件的拷貝
      group:    # 設置遠程文件的所屬組
      owner:    # 設置遠程文件的所有者
      mode:     # 設置遠程文件的權限。使用數值表示時不能省略第一位,如0644。
                # 也可以使用'u+rwx' or 'u=rw,g=r,o=r'等方式設置
      src:      # ansible控制器上Jinja2格式的模板所在位置,可以是相對或絕對路徑
      validate: # 在複製到目標主機後但放到目標位置之前,執行此選項指定的命令。
                # 一般用於檢查配置文件語法,語法正確則保存到目標位置。
                # 如果要引用目標文件名,則使用%s,下面的示例中的%s即表示目標機器上的/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模塊可以按需求修改配置文件內容來複制模板到被控主機上,但是有一種情況它是不能解決的:不同被控節點所需的配置文件差異很大,並非修改幾個變量就可以滿足。例如在centos 6和centos 7上通過yum安裝的 nginx,它們的配置文件內容相差非常大,且centos 6上的nginx的默認就有一個/etc/nginx/conf.d/default.conf。 如果直接複製同一個模板的nginx配置文件到centos 6和centos 7上,很可能導致某一版本的nginx不能啓動。

這時就有必要在複製模板時挑選對應發行版的模板文件進行配對複製,例如要複製到 centos 6上的源模板是 nginx6.conf.j2,複製到centos 7上的源模板是nginx7.conf.j2。這種行爲可以稱之爲"基於變量選擇文件或模板"。

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

還可以在文件內容中指定jinja2的替代變量,在ansible執行時首先會根據變量內容進行渲染,渲染後再執行相關模塊。例如,此處的template模塊,複製一個基於發行版本號的yum源配置文件。以下是某個repo文件模板 base.repo.j2的內容。

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

再複製即可。

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

文件模塊file

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

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

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

# 被控制機上並不存在/root/test這個目錄
# 使用ansible在被控制機上/root/test目錄下創建foo.conf
ansible test -m file -a "path=/root/test/foo.conf owner=duser group=duser mode='0644' state=touch"

ansible執行結果是:

192.168.246.187 | FAILED! => {
    "changed": false,    ## 失敗
    "msg": "Error, could not touch target: [Errno 2] 沒有那個文件或目錄: b'/root/test/foo.conf'",
    "path": "/root/test/foo.conf"
}
# 現在我們在被控制機上創建/root/test目錄
# 再次執行ansible
ansible test -m file -a "path=/root/test/foo.conf owner=duser group=duser mode='0644' state=touch"

ansible執行結果是:

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
}

創建目錄,並遞歸修改目錄的屬性。

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

修改目錄/tmp/xyz/test中test的權限

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

創建或修改文件屬性/權限

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

拉取文件模塊fetch

和copy工作方式類似,只不過是從遠程主機將文件拉取到本地端,存儲時使用主機名作爲目錄樹,且只能拉取文件不能拉取目錄!

fetch--Ansible官方使用說明文檔

ansible-doc -s fetch
- name: Fetch files from remote nodes
  fetch:
      dest:              # 本地存儲拉取文件的目錄。例如dest=/data,src=/etc/fstab,
                         # 遠程主機名host.exp.com,則保存的路徑爲/data/host.exp.com/etc/fstab。
      fail_on_missing:   # 當設置爲yes時,如果拉取的源文件不存在,則此任務失敗。默認爲no。
      flat:              # 改變拉取後的路徑存儲方式。如果設置爲yes,且當dest以"/"結尾時,將直接把源文件
                         # 的basename存儲在dest下。顯然,應該考慮多個主機拉取時的文件覆蓋情況。
      src:               # 遠程主機上的源文件。只能是文件,不支持目錄。在未來的版本中可能會支持目錄遞歸拉取。
      validate_checksum: # fetch到文件後,檢查其md5和源文件是否相同。

存儲爲/tmp/192.168.246.187/etc/fstab:

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

存儲爲/tmp/fstab:

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

存儲爲/tmp/fstab-192.168.246.187:

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

這裏說明一點:

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

# /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模塊用於實現rsync的簡單版常用功能,它無法實現完整版的rsync,畢竟rsync功能太多太細緻。如果要使用rsync,還是應該使用command或shell模塊來調用rsync命令。

完整的rsync功能見rsync命令中文手冊

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

說明

本博文是參考馬龍帥大佬文章整理生成,屬於博主讀書筆記,如有侵權,請大佬與我聯繫,立刪!

最後,感謝開源,擁抱開源~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章