天天看點

Ansible劇本編寫ansible劇本組成部分ansible劇本編寫規範ansible劇本主機規劃ansible劇本主機清單ansible劇本編寫實踐ansible劇本常見錯誤ansible劇本擴充功能ansible劇本角色資訊

目錄

  • ansible劇本組成部分
  • ansible劇本編寫規範
  • ansible劇本主機規劃
  • ansible劇本主機清單
  • ansible劇本編寫實踐
    • ad-hoc部署rsync服務
    • playbook部署rsync服務
  • ansible劇本常見錯誤
  • ansible劇本擴充功能
    • 變量
    • 注冊
    • 判斷
    • 循環
    • 标簽
    • 觸發
    • 忽略錯誤
    • 整合劇本
  • ansible劇本角色資訊
    • 規範目錄結構
    • roles目錄下建立檔案
    • 編寫主劇本檔案

ansible劇本組成部分

Ansible劇本編寫ansible劇本組成部分ansible劇本編寫規範ansible劇本主機規劃ansible劇本主機清單ansible劇本編寫實踐ansible劇本常見錯誤ansible劇本擴充功能ansible劇本角色資訊

ansible劇本編寫規範

劇本編寫規範:pyyaml

  1. 合理的資訊縮進:yaml使用固定的縮進風格表示資料層結構關系,編寫ansible-playbook檔案一定不能使用tab鍵進行縮進。
  2. 冒号的使用方法:使用冒号時後面一定要有空格資訊,以冒号結尾或冒号資訊出現在注釋說明中,其後不需要加空格。
  3. 短橫線:表示清單,使用一個短橫線加一個空格。多個項使用同樣的縮進級别作為同一個清單的一部分。

ansible劇本主機規劃

外網IP 内網IP 主機名 功能 系統版本
10.0.0.61 172.16.1.61 m01 管理主機 CentOS 7.x
10.0.0.41 172.16.1.41 backup 被管理主機 CentOS 7.x
10.0.0.31 172.16.1.31 nfs01 被管理主機 CentOS 7.x
10.0.0.7 172.16.1.7 web01 被管理主機 CentOS 7.x

ansible劇本主機清單

主機清單配置檔案: /etc/ansible/hosts

  1. 分組配置

    [web] — ansible web -a … 統一操作web組的主機

    172.16.1.7

    172.16.1.8

    172.16.1.9

    [data] — ansible data -a … 統一操作data組的主機

    172.16.1.31

    172.16.1.41

  2. 主機名符号比對配置

    [web]

    172.16.1.[7:9] 通過IP位址比對配置

    web[01:03] 通過主機名比對配置(注意:通過主機名比對需要在/etc/hosts檔案中有主機名和IP的映射)

  3. 加上非标準遠端端口(如ssh端口變為52113)

    [web]

    web01:52113

    172.16.1.7:52113

  4. 主機使用特殊的變量

    [web]

    172.16.1.7 ansible_ssh_port=52113 ansible_ssh_user=root ansible_ssh_pass=123456

    [web]

    web01 ansible_ssh_host=172.16.1.7 ansible_ssh_port=52113 ansible_ssh_user=root ansible_ssh_pass=123456

  5. 主機組名嵌入配置

    [rsync:children] — 嵌入子組資訊

    rsync_server

    rsync_client

    [rsync_server] — 子組

    172.16.1.41

    [rsync_client] — 子組

    172.16.1.31

    172.16.1.7

    [web:vars] — 嵌入式變量資訊

    ansible_ssh_host=172.16.1.7 — 變量

    ansible_ssh_port=52113 — 變量

    ansible_ssh_user=root — 變量

    ansible_ssh_pass=123456 — 變量

    [web] — 該組調用以上變量

    web01

主機清單官方配置方法

ansible劇本編寫實踐

ad-hoc部署rsync服務

服務端部署

  1. 确認軟體安裝

    ansible 172.16.1.41 -m yum -a “name=rsync state=installed”

  2. 編寫檔案

    ansible 172.16.1.41 -m copy -a “src=/etc/ansible/server_file/rsync_server/rsyncd.conf dest=/etc/”

  3. 建立使用者

    ansible 172.16.1.41 -m user -a “name=rsync create_home=no shell=/sbin/nologin”

  4. 建立目錄

    ansible 172.16.1.41 -m file -a “dest=/backup state=directory owner=rsync group=rsync”

  5. 建立密碼檔案

    ansible 172.16.1.41 -m copy -a “content=‘rsync_backup:redhat’ dest=/etc/rsync.password mode=600”

  6. 啟動服務

    ansible 172.16.1.41 -m service -a “name=rsyncd state=started enabled=yes”

用戶端部署

  1. 确認軟體安裝

    ansible 172.16.1.41,172.16.1.7 -m yum -a “name=rsync state=installed”

  2. 建立密碼檔案

    ansible 172.16.1.31,172.16.1.7 -m copy -a “content=‘redhat’ dest=/etc/rsync.password mode=600”

  3. 測試

    ansible 172.16.1.31,172.16.1.7 -m file -a “dest=/tmp/test.txt state=touch”

    ansible 172.16.1.31,172.16.1.7 -m shell -a "rsync -avz /tmp/test.txt rsyn[email protected]::backup --password-file=/etc/rsync.password

playbook部署rsync服務

建立playbook目錄

進入playbook目錄

建立編輯rsync劇本(劇本檔案擴充名盡量寫為yaml,友善識别檔案是一個劇本檔案,且檔案編寫時會有顔色提示)

[[email protected] ansible-playbook]# vim rsync_server.yaml

- hosts: 172.16.1.41
  tasks:
    - name: 01-install rsync
      yum: name=rsync state=installed
    - name: 02-push rsyncd.conf
      copy: src=../server_file/rsync_server/rsyncd.conf dest=/etc/
    - name: 03-create user
      user: name=rsync create_home=no shell=/sbin/nologin
    - name: 04-create backup directory
      file: dest=/backup state=directory owner=rsync group=rsync
    - name: 05-create password file
      copy: content=rsync_backup:redhat dest=/etc/rsync.password mode=600
    - name: 06-start rsync service
      service: name=rsyncd state=started enabled=yes    # 配置檔案改變,不重新開機服務不會生效?

- hosts: 172.16.1.31,172.16.1.7
  tasks:
    - name: 01-install rsync
      yum: name=rsync state=installed
    - name: 02-create password file
      copy: content=redhat dest=/etc/rsync.password mode=600
    - name: 03-create test file  
      file: dest=/tmp/test.txt state=touch 
    - name: 04-test      shell: rsync -avz /tmp/test.txt [email protected]::backup --password-file=/etc/rsync.password
           

如何執行劇本?

  1. 檢查劇本的文法格式
  2. 模拟執行劇本(彩排)
  3. 正式執行劇本(實幹)

ansible劇本常見錯誤

  • 劇本文法規範錯誤(空格、冒号、短橫線);
  • 劇本子產品使用是否正确;
  • 劇本中一個name辨別下隻能寫一個子產品任務;
  • 劇本中盡量不要大量使用shell子產品。

劇本執行出現錯誤排查思路/步驟:

1)找到劇本中出現問題關鍵點;

2)将劇本中的操作轉換成單條ad-hoc指令操作;

3)将子產品的功能操作轉換成linux指令;

4)本地管理主機上執行指令測試;

5)遠端被管理主機上執行指令測試。

ansible劇本擴充功能

變量

變量名由字母、數字、下劃線組成,變量名需要以字母開頭,ansible内置關鍵字不能作為變量名。

  1. 在劇本檔案中編寫

    在劇本中定義變量,借助vars關鍵字

    vars:

    backupdir: /backup

    passfile: rsync.password

    使用{{ 變量名 }}可以引用對應的變量
  2. 在指令行中指定(臨時設定)
    ansible-playbook -e backupdir=/backup -e passfile=rsync.password rsync_server.yaml
  3. 在主機清單中編寫

    vim /etc/ansible/hosts

    [rsync_server:vars]

    backupdir: /backup

    passfile: rsync.password

三種方式優先級:指令行變量設定>劇本變量設定>主機清單變量設定

注冊

注冊功能可以在執行劇本時,輸出指令結果。

- hosts: rsync_server
  tasks:
    - name: check server port
      shell: netstat -lntup 
      register: get_server_port
    
    - name: display port info
      debug: msg={{ get_server_port.stdout_lines }}
           

判斷

指定判斷條件

(ansible_hostname == “nfs01”)

(ansible_hostname == “web01”)

例如

- hosts: rsync_server
  remote_user: root
  tasks:
    - name: Check File
      file: path=/tmp/this_is_{{ ansible_hostname }}_file state=touch
      when: (ansible_hostname == "nfs") or (ansible_hostname == "backup")	
           

setup子產品顯示被管理主機系統的詳細資訊

ansible rsync_server -m setup

setup子產品擷取被管理主機的内置變量資訊

ansible rsync_server -m setup -a “filter=xxx”

常見主機資訊

參數 作用
ansible_all_ipv4_addresses 僅顯示ipv4的資訊
ansible_devices 僅顯示磁盤裝置資訊
ansible_distribution 顯示是什麼系統,例:centos,suse等
ansible_distribution_major_version 顯示是系統主版本
ansible_distribution_version 僅顯示系統版本
ansible_machine 顯示系統類型,例:32位,還是64位
ansible_eth0 僅顯示eth0的資訊
ansible_hostname 僅顯示主機名
ansible_kernel 僅顯示核心版本
ansible_lvm 顯示lvm相關資訊
ansible_memtotal_mb 顯示系統總記憶體
ansible_memfree_mb 顯示可用系統記憶體
ansible_memory_mb 詳細顯示記憶體情況
ansible_swaptotal_mb 顯示總的swap記憶體
ansible_swapfree_mb 顯示swap記憶體的可用記憶體
ansible_mounts 顯示系統磁盤挂載情況
ansible_processor 顯示cpu個數(具體顯示每個cpu的型号)
ansible_processor_vcpus 顯示cpu個數(隻顯示總的個數)

循環

一個name下隻能執行一個ad-hoc指令,如果想要執行多條,可以使用循環。

- hosts: all
  remote_user: root
  tasks:
    - name: Add Users
      user: name={{ item.name }} groups={{ item.groups }} state=present
      with_items: 
        - { name: 'testuser1', groups: 'bin' }
        - { name: 'testuser2', groups: 'root' }
           

标簽

指定執行标簽任務: ansible-playbook --tags=t2 test.yml

跳過指定标簽任務: ansible-playbook --skip-tags=t2 test.yml

- hosts: all
  ignore_errors: yes
  remote_user: root
  tasks:
    - name: Check File
      file: path=/tmp/this_is_{{ ansible_hostname }}_file state=touch
      when: (ansible_hostname == "nfs01") or (ansible_hostname == "backup")
      tags: t1
    
    - name: install httpd
      yum: name=httpd state=installed
      when: (ansible_all_ipv4_addresses == ["172.16.1.7","10.0.0.7"])
      tags: t2
    
    - name: install httpd2
      yum: name=httpd2 state=installed
      when: (ansible_distribution == "ubuntu")
      tags: t3
           

觸發

- hosts: backup
  remote_user: root
  tasks:
    - name: 01 Install rsync
      yum: name=rsync state=present
        
    - name: 02 push config file
      copy: src=./file/{{ item.src }} dest=/etc/{{ item.dest }} mode={{ item.mode }} 
      with_items:
        - { src: "rsyncd.conf", dest: "rsyncd.conf", mode: "0644" }
        - { src: "rsync.password", dest: "rsync.password", mode: "0600" }
      notify: restart rsync server

  handlers:    # 當notify發出時,handlers起作用
    - name: restart rsync server
      service: name=rsyncd state=restarted   
           

忽略錯誤

預設playbook會檢查指令和子產品的傳回狀态,如遇到錯誤就中斷playbook的執行,可以加入ignore_errors: yes忽略錯誤。

- hosts: all
  remote_user: root
  tasks:
    - name: Ignore False
      command: /bin/false
      ignore_errors: yes
    - name: touch new file
      file: path=/tmp/oldboy_ignore state=touch	
           

整合劇本

  1. include_tasks: playbook.yaml
    [[email protected] ansible-playbook]# cat main.yml
    - hosts: all(host與include_tasks劇本中的host沖突)
      tasks:
        - include_tasks: rsync-server.yml
        - include_tasks: nfs-server.yml
               
  2. include: playbook.yml(設定gather_facts: no可提高執行速度)
    [[email protected] ansible-playbook]# cat main.yml
    - include:rsync-server.yml	
    - include:nfs-server.yml
               
  3. - import_playbook(主要使用該方法進行彙總)
    [[email protected] ansible-playbook]# vim main.yml 
    - import_playbook: rsync.yml    
    - import_playbook: nfs.yml      
               

ansible劇本角色資訊

待解決問題:

  1. 目錄結構不夠規範?
  2. 編寫好的任務如何重複調用?
  3. 服務端配置檔案改動,用戶端參數資訊如何自動變化?
  4. 彙總劇本中如何顯示主機角色資訊?
  5. 一個劇本内容資訊過多,不容易進行閱讀,如何進行拆分?

規範目錄結構

建立相應角色目錄

[[email protected] ~]# cd /etc/ansible/roles/
[[email protected] roles]# mkdir {rsync,nfs-server,nfs-client}
           

建立角色子目錄

檢視目錄結構

[[email protected] roles]# tree /etc/ansible/roles/
/etc/ansible/roles/
|-- nfs-server
|   |-- files    -- 儲存需要分發的檔案
|   |-- handlers    -- 儲存觸發器配置檔案
|   |-- tasks    -- 儲存要執行的動作資訊檔案
|   |-- templates    -- 儲存需要分發的模闆檔案,模闆檔案中可以設定變量(調取var目錄中的變量值)
|   `-- vars    -- 儲存變量資訊檔案
......
           

roles目錄下建立檔案

以部署NFS服務端為例:

  1. 編寫tasks目錄中main.yml檔案
    [[email protected] tasks]# vim main.yml
    - name: 01-copy nfs conf file    
      copy: src=exports dest=/etc/ - name: 02-create data dir    -- 自動去往file目錄尋找exports檔案
      file: path={{ Data_dir }} state=directory owner=nfsnobody group=nfsnobody
      notify: restart nfs server 
    - name: 03-start server
      service: name={{ item }} state=started enabled=yes
      with_items:    
        - rpcbind
        - nfs
               
  2. 編寫vars目錄中main.yml檔案
    [[email protected] tasks]# cd ../vars
    [[email protected] vars]# vim main.yml
    Data_dir: /data
               
  3. 編寫files目錄中需要分發的檔案
    [[email protected] vars]# cd ../files/
    [[email protected] files]# echo '/data172.16.1.0/24(rw,sync)' > exports
               
  4. 編寫handlers目錄中main.yml檔案
    [[email protected] tasks]# cd ../handlers/
    [[email protected] handlers]# vim main.yml
    [[email protected] handlers]# cat main.yml 
    - name: restart nfs server
      service: name=nfs state=restarted
               
  5. 編寫好的目錄結構
    [[email protected] nfs-server]# tree
    .
    |-- files
    |   `-- exports
    |-- handlers
    |   `-- main.yml
    |-- tasks
    |   `-- main.yml
    |-- templates
    `-- vars
        `-- main.yml
    
    5 directories, 4 files
               

編寫主劇本檔案

- hosts: nfs_server
  roles:
    - nfs-server

- hosts: nfs_client
  roles:
    - nfs-client