ansible

ansible官方網(wǎng)站
安裝部署軟件ansible
yum install -y ansible --- 需要依賴epel的yum源
ansible配置文件
/etc/ansible/ansible.cfg --- ansible服務配置文件
/etc/ansible/hosts --- 主機清單文件 定義可以管理的主機信息
/etc/ansible/roles --- 角色目錄

主機清單文件的使用方法
第一種基于密碼連接
主機+端口+密碼
10.0.0.3 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123456'
通過vars批量配置ansible_ssh_pass
[webservers]
10.0.0.3
10.0.0.4
[webservers:vars]
ansible_ssh_pass='123456'
多組合并
[webservers:children]
[group_name1]
[group_name2]

ansible軟件輸出顏色說明:
01. 綠色信息: 查看主機信息/對主機未做改動
02. 黃色信息: 對主機數(shù)據(jù)信息做了修改
03. 紅色信息: 命令執(zhí)行出錯了
04. 粉色信息: 忠告信息
05. 藍色信息: 顯示ansible命令執(zhí)行的過程

ansible Ad-Hoc 臨時指令使用方式

語法格式:
ansible 主機名稱/主機組名稱/主機地址信息/all -m(指定應用的模塊信息) 模塊名稱 -a(指定動作信息) "執(zhí)行什么動作"
命令 操作對象 模塊 動作
例如: ansible all -m command -a "hostname"

命令類型模塊:

command (默認模塊)在一個遠程主機上執(zhí)行一個命令

擴展用法:

  1. chdir 在執(zhí)行命令之前對目錄進行切換
    例如: ansible 172.16.1.31 -m command -a "chdir=/tmp touch test.txt"
  2. creates 如果文件存在了,不執(zhí)行命令操作
    例如: ansible 172.16.1.31 -m command -a "creates=/tmp/hosts touch test.txt"
  3. removes 如果文件存在了,這個步驟將執(zhí)行
    例如:ansible 172.16.1.31 -m command -a "removes=/tmp/hosts chdir=/tmp touch oldboy.txt"
    注意事項:
    有些符號信息無法識別: <", ">", "|", ";" and "&"

如果需要一些管道操作,則使用shell

ansible oldboy -m shell -a "ifconfig|grep eth0" -f 50

-f =forks /etc/ansible/ansible.cfg #結果返回的數(shù)量

2.script腳本模塊

編寫腳本

在本地運行模塊,等同于在遠程執(zhí)行,不需要將腳本文件進行推送目標主機執(zhí)行

例如 ansible oldboy -m script -a "/server/scripts/yum.sh"

yum安裝軟件模塊

[root@m01 ~]# ansible oldboy -m yum -a "name=httpd state=installed"
name #指定要安裝的軟件包名稱
state #指定使用yum的方法
installed,present #安裝軟件包
removed,absent #移除軟件包
latest #安裝最新軟件包

文件類型模塊:
copy – 復制文件到遠程主機
例如: ansible 172.16.0.3 -m copy -a "src=/etc/hosts dest=/etc/"
將本機/etc/hosts文件發(fā)送到 172.16.1.3的/etc/目錄
172.16.0.31 | CHANGED => { --- 對哪臺主機進行操作
"changed": true, --- 是否對主機信息進行改變
"checksum": "6ed7f68a1d6b4b36c1418338b2001e421eeba270", --- 生成一個文件校驗碼==MD5數(shù)值
"dest": "/etc/hosts", --- 顯示目標路徑信息
"gid": 0, --- 顯示復制后文件gid信息
"group": "root", --- 顯示復制后文件屬組信息
"md5sum": "7afd7b74854f0aaab646b3e932f427c0", --- 生成一個文件校驗碼==MD5數(shù)值
"mode": "0644", --- 顯示復制后文件權限信息
"owner": "root", --- 顯示復制后文件屬主信息
"size": 401, --- 顯示文件的大小信息
"src": "/root/.ansible/tmp/ansible-tmp-1557804498.23-26487341925325/source",
"state": "file", --- 顯示文件的類型信息
"uid": 0 --- 顯示復制后文件uid信息
}
擴展用法:
01. 在傳輸文件時修改文件的屬主和屬組信息
ansible 172.16.1.31 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ owner=oldboy group=oldboy"
02. 在傳輸文件時修改文件的權限信息
ansible 172.16.1.31 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ mode=1777"
03. 在傳輸數(shù)據(jù)文件信息時對遠程主機源文件進行備份
ansible 172.16.1.31 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ backup=yes"
04. 創(chuàng)建一個文件并直接編輯文件的信息
ansible 172.16.1.31 -m copy -a "content='oldboy123' dest=/etc/rsync.password"

自行研究: remote_src  directory_mode local_follow
If no, it will search for src at originating/master machine.
       src參數(shù)指定文件信息,會在本地管理端服務進行查找
If yes it will go to the remote/target machine for the src. Default is no.
       src參數(shù)指定文件信息,會從遠程主機上進行查找

PS: ansible軟件copy模塊復制目錄信息
ansible 172.16.1.31 -m copy -a "src=/oldboy dest=/oldboy"  
src后面目錄沒有/: 將目錄本身以及目錄下面的內(nèi)容都進行遠程傳輸復制
ansible 172.16.1.31 -m copy -a "src=/oldboy/ dest=/oldboy"  
src后面目錄有/:   只將目錄下面的內(nèi)容都進行遠程傳輸復制 

file – 設置文件屬性信息
基本用法:
ansible 172.16.1.31 -m file -a "dest=/etc/hosts owner=oldboy group=oldboy mode=666"

擴展用法:
1. 可以利用模塊創(chuàng)建數(shù)據(jù)信息 (文件 目錄 鏈接文件)

state 參數(shù)
=absent --- 缺席/刪除數(shù)據(jù)信息
=directory --- 創(chuàng)建一個目錄信息
=file --- 檢查創(chuàng)建的數(shù)據(jù)信息是否存在 綠色存在 紅色不存在
=hard --- 創(chuàng)建一個硬鏈接文件
=link --- 創(chuàng)建一個軟鏈接文件
=touch --- 創(chuàng)建一個文件信息

創(chuàng)建目錄信息:
ansible 172.16.1.31 -m file -a "dest=/oldboy/ state=directory"
創(chuàng)建文件信息:
ansible 172.16.1.31 -m file -a "dest=/oldboy/oldboy.txt state=touch"
創(chuàng)建鏈接文件信息:
ansible 172.16.1.31 -m file -a "src=/oldboy/oldboy.txt dest=/oldboy/oldboy_hard.txt state=hard"
ansible 172.16.1.31 -m file -a "src=/oldboy/oldboy.txt dest=/oldboy/oldboy_link.txt state=link"
刪除數(shù)據(jù)信息
ansible 172.16.1.31 -m file -a "dest=/oldboy/oldboy.txt state=absent"
ansible 172.16.1.31 -m file -a "dest=/oldboy/ state=absent"

service模塊: 管理服務器的運行狀態(tài) 停止 開啟 重啟
name: --- 指定管理的服務名稱
state: --- 指定服務狀態(tài)
started 啟動
restarted 重啟
stopped 停止
enabled --- 指定服務是否開機自啟動
ansible 172.16.1.31 -m service -a "name=nfs state=started enabled=yes"

cron模塊: 批量設置多個主機的定時任務信息

minute:設置分鐘信息( 0-59, *, */2, etc )
hour:設置小時信息
day:設置日期信息
month:設置月份信息
weekday:周信息

基本用法:
ansible 172.16.1.31 -m cron -a "minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'" 

擴展用法:
01. 給定時任務設置注釋信息
ansible 172.16.1.31 -m cron -a "name='time sync' minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'"
02. 刪除指定定時任務
ansible 172.16.1.31 -m cron -a "name='time sync01' state=absent"
PS: ansible可以刪除的定時任務,只能是ansible設置好的定時任務
03. 批量注釋定時任務
ansible 172.16.1.31 -m cron -a "name='time sync' job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1' disabled=yes"

mount: 批量進行掛載操作
       src:  需要掛載的存儲設備或文件信息
       path: 指定目標掛載點目錄
       fstype: 指定掛載時的文件系統(tǒng)類型
       state
       present/mounted     --- 進行掛載
       present: 不會實現(xiàn)立即掛載,修改fstab文件,實現(xiàn)開機自動掛載
       mounted: 會實現(xiàn)立即掛載, 并且會修改fstab文件,實現(xiàn)開機自動掛載 *****
       
       absent/unmounted    --- 進行卸載
       absent:     會實現(xiàn)立即卸載, 并且會刪除fstab文件信息,禁止開機自動掛載
       unmounted:  會實現(xiàn)立即卸載, 但是不會會刪除fstab文件信息  *****

user模塊: 實現(xiàn)批量創(chuàng)建用戶
基本用法:
ansible 172.16.1.31 -m user -a "name=oldboy01"

擴展用法:
1) 指定用戶uid信息
ansible 172.16.1.31 -m user -a "name=oldboy02 uid=6666"

2) 指定用戶組信息
ansible 172.16.1.31 -m user -a "name=oldboy03 group=oldboy02"
ansible 172.16.1.31 -m user -a "name=oldboy04 groups=oldboy02"

3) 批量創(chuàng)建虛擬用戶
ansible 172.16.1.31 -m user -a "name=rsync create_home=no  shell=/sbin/nologin"

4) 給指定用戶創(chuàng)建密碼
PS: 利用ansible程序user模塊設置用戶密碼信息,需要將密碼明文信息轉(zhuǎn)換為密文信息進行設置
生成密文密碼信息方法:
方法1:

ansible all -i localhost, -m debug -a "msg={{ '123456' | password_hash('sha512', 'oldboy') }}"
方法2:
yum install -y python-pip
pip install passlib
python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"
Password:
6rJJeiIerQ8p2eR82$uE2701X7vY44voF4j4tIQuUawmTNHEZhs26nKOL0z39LWyvIvZrHPM52Ivu9FgExlTFgz1VTOCSG7KhxJ9Tqk.

ansible 172.16.1.31 -m user -a 'name=oldboy08 password=$6$oldboy$MVd3DevkLcimrBLdMICrBY8HF82Wtau5cI8D2w4Zs6P1cCfMTcnnyAmmJc7mQaE9zuHxk8JFTRgYMGv9uKW7j1'
  1. 劇本的編寫方法
    劇本的作用: 可以一鍵化完成多個任務
    自動化部署rsync服務:
    服務端的操作
    第一個歷程安裝軟件:
    ansible 172.16.1.41 -m yum -a "name=rsync state=installed"

    第二個歷程編寫文件:
    ansible 172.16.1.41 -m copy -a "src=/xxx/rsyncd.conf dest=/etc/"

    第三個歷程創(chuàng)建用戶
    ansible 172.16.1.41 -m user -a "name=rsync create_home=no shell=/sbin/nologin"

    第四個歷程創(chuàng)建目錄
    ansible 172.16.1.41 -m file -a "dest=/backup state=directory owner=rsync group=rsync"

    第五個歷程創(chuàng)建密碼文件
    ansible 172.16.1.41 -m copy -a "content='rsync_backup:oldboy123' dest=/etc/rsync.password mode=600"

    第六個歷程啟動服務
    ansible 172.16.1.41 -m service -a "name=rsyncd state=started enabled=yes"

    客戶端的操作:
    第一個歷程: 創(chuàng)建密碼文件
    ansible 客戶端地址 -m copy -a "content='rsync_backup:oldboy123' dest=/etc/rsync.password mode=600"

    劇本的做成部分:
    演員信息: 男一號 hosts
    干的事情: 吻戲 tasks

    演員信息: 男二號
    干的事情: 看著

    劇本編寫規(guī)范: pyyaml -- 三點要求

    1. 合理的信息縮進 兩個空格表示一個縮進關系
      標題一
      標題二
      標題三
      PS: 在ansible中一定不能用tab進行縮進

    2. 冒號的使用方法
      hosts: 172.16.1.41
      tasks:
      yum: name=xx
      PS: 使用冒號時后面要有空格信息
      以冒號結尾,冒號信息出現(xiàn)在注釋說明中,后面不需要加上空格

    3. 短橫線應用 -(列表功能)

      • 張三

        • 打游戲
        • 運動
      • 李四

        學習
        湖南

      • 王五

        運動
        深圳
        PS: 使用短橫線構成列表信息,短橫線后面需要有空格

    開始編寫劇本
    mkdir /etc/ansible/ansible-playbook
    vim rsync_server.ymal
    說明: 劇本文件擴展名盡量寫為yaml

    1. 方便識別文件是一個劇本文件
    2. 文件編寫時會有顏色提示
  • hosts: 172.16.1.41
    tasks:
    yum: name=rsync state=installed
    copy: src=/tmp/rsyncd.conf dest=/etc/

    如何執(zhí)行劇本:
    第一個步驟: 檢查劇本的語法格式
    ansible-playbook --syntax-check rsync_server.yaml
    第二個步驟: 模擬執(zhí)行劇本
    ansible-playbook -C rsync_server.yaml
    第三個步驟: 直接執(zhí)行劇本
    ansible-playbook rsync_server.yaml

  • hosts: 172.16.1.41
    tasks:

    • name: 01-install rsync
      yum: name=rsync state=installed
    • name: 02-push conf file
      copy: src=/tmp/rsyncd.conf dest=/etc/
  1. 利用劇本完成服務一鍵化部署:
    rsync 服務部署
    nfs 服務部署
    sersync 服務部署
    全網(wǎng)備份項目

    rsync服務劇本編寫:
    準備工作:

    1. 熟悉軟件部署流程
    2. 熟悉ansible軟件模塊使用
    3. 熟悉ansible劇本編寫規(guī)范
      ansible:
      ad-hoc 臨時實現(xiàn)批量管理功能(模塊) --- 命令
      playbook 永久實現(xiàn)批量管理功能(劇本) --- 腳本

    劇本編寫常見錯誤:

    1. 劇本語法規(guī)范是否符合(空格 冒號 短橫線)
    2. 劇本中模塊使用是否正確
    3. 劇本中一個name標識下面只能寫一個模塊任務信息
    4. 劇本中盡量不要大量使用shell模塊
[root@m01 ansible-playbook]# cat rsync_server.yaml 
  • hosts: rsync_server
    tasks:

    • name: 01-install rsync
      yum: name=rsync state=installed
    • name: 02-push conf file
      copy: src=/etc/ansible/server_file/rsync_server/rsyncd.conf dest=/etc/
    • name: 03-create user
      user: name=rsync create_home=no shell=/sbin/nologin

      shell: useradd rsync -M -s /sbin/nologin

    • name: 04-create backup dir
      file: path=/backup state=directory owner=rsync group=rsync
    • name: 05-create password file
      copy: content=rsync_backup:oldboy123 dest=/etc/rsync.password mode=600
    • name: 06-start rsync server
      service: name=rsyncd state=started enabled=yes
  • hosts: rsync_clients
    tasks:

    • name: 01-install rsync
      yum: name=rsync state=installed
    • name: 02-create password file
      copy: content=oldboy123 dest=/etc/rsync.password mode=600
    • name: 03-create test file
      file: dest=/tmp/test.txt state=touch
    • name: 04-check test
      shell: rsync -avz /tmp/test.txt rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
  1. 如何配置主機清單
    第一種方式: 分組配置主機信息
    [web]
    172.16.1.7
    172.16.1.8
    172.16.1.9

    [data]
    172.16.1.31
    172.16.1.41
    操作過程
    [root@m01 ansible-playbook]# ansible data -a "hostname"
    172.16.1.31 | CHANGED | rc=0 >>
    nfs01

    172.16.1.41 | CHANGED | rc=0 >>
    backup

    [root@m01 ansible-playbook]# ansible web -a "hostname"
    172.16.1.7 | CHANGED | rc=0 >>
    web01

    第二種方式: 主機名符號匹配配置
    [web]
    172.16.1.[7:9]
    [web]
    web[01:03]

    第三種方式: 跟上非標準遠程端口
    [web]
    web01:52113
    172.16.1.7:52113

    第四種方式: 主機使用特殊的變量
    [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

    第五種方式: 主機組名嵌入配置
    [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

    主機清單的配置方法:
    https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html

  2. 在劇本中設置變量信息
    方式一:直接在劇本文件編寫
    vars:
    oldboy01: data01
    oldboy02: data02

    方式二:在命令行中進行指定
    ansible-playbook --extra-vars=oldboy01=data01

    方式三:在主機清單文件編寫
    [oldboy]
    oldboy01=data01
    oldboy02=data02

    三種變量設置方式都配置了,三種方式的優(yōu)先級???
    最優(yōu)先: 命令行變量設置
    次優(yōu)先: 劇本中變量設置
    最后: 主機清單變量設置

    如何全局設置變量: roles 劇本整合

  1. 在劇本中設置注冊信息

    • hosts: oldboy
      tasks:
      • name: check server port
        shell: netstat -lntup --- 端口信息
        register: get_server_port<--端口信息

      • name: display port info
        debug: msg={{ get_server_port.stdout_lines }}
        顯示進程信息,表示服務已經(jīng)正常啟動
        PS: 設置變量不能有空格信息

  2. 在劇本中設置判斷信息
    如何指定判斷條件:
    (ansible_hostname == "nfs01")
    (ansible_hostname == "web01")
    setup模塊中顯示被管理主機系統(tǒng)的詳細信息

    • hosts: oldboy
      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")

      • name: install httpd
        yum: name=httpd state=installed
        when: (系統(tǒng)情況 == "CentOS")

      • name: install httpd2
        yum: name=httpd2 state=installed
        when: (系統(tǒng)情況 == "ubuntu")

    獲取內(nèi)置變量方法:
    ansible oldboy -m setup -a "filter=ansible_hostname"
    常見主機信息:
    ansible_all_ipv4_addresses: 僅顯示ipv4的信息。
    ansible_devices: 僅顯示磁盤設備信息。
    ansible_distribution: 顯示是什么系統(tǒng),例:centos,suse等。
    ansible_distribution_major_version: 顯示是系統(tǒng)主版本。
    ansible_distribution_version: 僅顯示系統(tǒng)版本。
    ansible_machine: 顯示系統(tǒng)類型,例:32位,還是64位。
    ansible_eth0: 僅顯示eth0的信息。
    ansible_hostname: 僅顯示主機名。
    ansible_kernel: 僅顯示內(nèi)核版本。
    ansible_lvm: 顯示lvm相關信息。
    ansible_memtotal_mb: 顯示系統(tǒng)總內(nèi)存。
    ansible_memfree_mb: 顯示可用系統(tǒng)內(nèi)存。
    ansible_memory_mb: 詳細顯示內(nèi)存情況。
    ansible_swaptotal_mb: 顯示總的swap內(nèi)存。
    ansible_swapfree_mb: 顯示swap內(nèi)存的可用內(nèi)存。
    ansible_mounts: 顯示系統(tǒng)磁盤掛載情況。
    ansible_processor: 顯示cpu個數(shù)(具體顯示每個cpu的型號)。
    ansible_processor_vcpus: 顯示cpu個數(shù)(只顯示總的個數(shù))。

    獲取子信息方法:
    ansible_eth0[ipv4]

  3. 在劇本中設置循環(huán)信息
    vim test04.yml

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

    vim test05.yml

    • hosts: all
      remote_user: root
      tasks:
      • name: Installed Pkg
        yum: name={{ item }} state=present
        with_items:
        • wget
        • tree
        • lrzsz
  4. 在劇本中設置忽略錯誤
    默認playbook會檢查命令和模塊的返回狀態(tài),如遇到錯誤就中斷playbook的執(zhí)行
    可以加入ignore_errors: yes忽略錯誤
    vim test06.yml

    • 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
  5. 在劇本中設置標簽功能

    • hosts: oldboy
      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: bad thing
        command: ech 123

        ignore_errors: yes

        tags: t2

      • name: install httpd
        yum: name=httpd state=installed
        when: (ansible_all_ipv4_addresses == ["172.16.1.7","10.0.0.7"])
        tags: t3

      • name: install httpd2
        yum: name=httpd2 state=installed
        when: (ansible_distribution == "ubuntu")
        tags: t4

    指定執(zhí)行哪個標簽任務: ansible-playbook --tags=t2 test05.yml
    跳過指定標簽任務: ansible-playbook --skip-tags=t2 test05.yml

  6. 在劇本中設置觸發(fā)功能

    • 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:

      • name: restart rsync server
        service: name=rsyncd state=restarted
  1. 將多個劇本進行整合
    方式一:include_tasks: f1.yml

    • hosts: all
      remote_user: root
      tasks:
      • include_tasks: f1.yml
      • include_tasks: f2.yml

    方式二:include: f1.yml

    • include:f1.yml
    • include:f2.yml

    方式三:- import_playbook:
    [root@m01 ansible-playbook]# cat main.yml

    • import_playbook: base.yml
    • import_playbook: rsync.yml
    • import_playbook: nfs.yml
    • import_playbook: oxxx.yml
    • import_playbook: rsync.yml
    • import_playbook: nfs.yml
  1. 在劇本中設置變量信息
    方式一:直接在劇本文件編寫
    vars:
    oldboy01: data01
    oldboy02: data02

    方式二:在命令行中進行指定
    ansible-playbook --extra-vars=oldboy01=data01

    方式三:在主機清單文件編寫
    [oldboy]
    oldboy01=data01
    oldboy02=data02

    三種變量設置方式都配置了,三種方式的優(yōu)先級???
    最優(yōu)先: 命令行變量設置
    次優(yōu)先: 劇本中變量設置
    最后: 主機清單變量設置

    如何全局設置變量: roles 劇本整合

  1. 在劇本中設置注冊信息

    • hosts: oldboy
      tasks:
      • name: check server port
        shell: netstat -lntup --- 端口信息
        register: get_server_port<--端口信息

      • name: display port info
        debug: msg={{ get_server_port.stdout_lines }}
        顯示進程信息,表示服務已經(jīng)正常啟動
        PS: 設置變量不能有空格信息

  2. 在劇本中設置判斷信息
    如何指定判斷條件:
    (ansible_hostname == "nfs01")
    (ansible_hostname == "web01")
    setup模塊中顯示被管理主機系統(tǒng)的詳細信息

    • hosts: oldboy
      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")

      • name: install httpd
        yum: name=httpd state=installed
        when: (系統(tǒng)情況 == "CentOS")

      • name: install httpd2
        yum: name=httpd2 state=installed
        when: (系統(tǒng)情況 == "ubuntu")

    獲取內(nèi)置變量方法:
    ansible oldboy -m setup -a "filter=ansible_hostname"
    常見主機信息:
    ansible_all_ipv4_addresses: 僅顯示ipv4的信息。
    ansible_devices: 僅顯示磁盤設備信息。
    ansible_distribution: 顯示是什么系統(tǒng),例:centos,suse等。
    ansible_distribution_major_version: 顯示是系統(tǒng)主版本。
    ansible_distribution_version: 僅顯示系統(tǒng)版本。
    ansible_machine: 顯示系統(tǒng)類型,例:32位,還是64位。
    ansible_eth0: 僅顯示eth0的信息。
    ansible_hostname: 僅顯示主機名。
    ansible_kernel: 僅顯示內(nèi)核版本。
    ansible_lvm: 顯示lvm相關信息。
    ansible_memtotal_mb: 顯示系統(tǒng)總內(nèi)存。
    ansible_memfree_mb: 顯示可用系統(tǒng)內(nèi)存。
    ansible_memory_mb: 詳細顯示內(nèi)存情況。
    ansible_swaptotal_mb: 顯示總的swap內(nèi)存。
    ansible_swapfree_mb: 顯示swap內(nèi)存的可用內(nèi)存。
    ansible_mounts: 顯示系統(tǒng)磁盤掛載情況。
    ansible_processor: 顯示cpu個數(shù)(具體顯示每個cpu的型號)。
    ansible_processor_vcpus: 顯示cpu個數(shù)(只顯示總的個數(shù))。

    獲取子信息方法:
    ansible_eth0[ipv4]

  3. 在劇本中設置循環(huán)信息
    vim test04.yml

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

    vim test05.yml

    • hosts: all
      remote_user: root
      tasks:
      • name: Installed Pkg
        yum: name={{ item }} state=present
        with_items:
        • wget
        • tree
        • lrzsz

    劇本執(zhí)行出現(xiàn)錯誤排查思路/步驟:

    1. 找到劇本中出現(xiàn)問題關鍵點
    2. 將劇本中的操作轉(zhuǎn)換成模塊進行操作
    3. 將模塊的功能操作轉(zhuǎn)換成linux命令
      本地管理主機上執(zhí)行命令測試
      遠程被管理主機上執(zhí)行命令測試
    • name: 01-install rsync
      yum:
      name: ['rsync', 'tree', 'wget'] --- saltstack
      state: installed

    • name: xxx
      yum: name=xxx state=installed --- ansible

  1. 在劇本中設置忽略錯誤
    默認playbook會檢查命令和模塊的返回狀態(tài),如遇到錯誤就中斷playbook的執(zhí)行
    可以加入ignore_errors: yes忽略錯誤
    vim test06.yml

    • 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
  2. 在劇本中設置標簽功能

    • hosts: oldboy
      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: bad thing
        command: ech 123

        ignore_errors: yes

        tags: t2

      • name: install httpd
        yum: name=httpd state=installed
        when: (ansible_all_ipv4_addresses == ["172.16.1.7","10.0.0.7"])
        tags: t3

      • name: install httpd2
        yum: name=httpd2 state=installed
        when: (ansible_distribution == "ubuntu")
        tags: t4

    指定執(zhí)行哪個標簽任務: ansible-playbook --tags=t2 test05.yml
    跳過指定標簽任務: ansible-playbook --skip-tags=t2 test05.yml

  3. 在劇本中設置觸發(fā)功能

    • 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:

      • name: restart rsync server
        service: name=rsyncd state=restarted
  1. 將多個劇本進行整合
    方式一:include_tasks: f1.yml

    • hosts: all
      remote_user: root
      tasks:
      • include_tasks: f1.yml
      • include_tasks: f2.yml

    方式二:include: f1.yml

    • include:f1.yml
    • include:f2.yml

    方式三:- import_playbook:
    [root@m01 ansible-playbook]# cat main.yml

    • import_playbook: base.yml
    • import_playbook: rsync.yml
    • import_playbook: nfs.yml
    • import_playbook: oxxx.yml
    • import_playbook: rsync.yml
    • import_playbook: nfs.yml

09 編寫NFS服務劇本
第一個歷程: 創(chuàng)建幾個目錄
[root@m01 ansible-playbook]# tree nfs-file/
nfs-file/
├── nfs-client
└── nfs-server

第二個歷程: 編寫劇本信息
主機清單:
[nfs:children]
nfs_server
nfs_client
[nfs_server]
172.16.1.31
[nfs_client]
172.16.1.7

172.16.1.8

172.16.1.9

  • hosts: nfs
    tasks:

    • name: 01-install nfs software
      yum:
      name: ['nfs-utils','rpcbind']
      state: installed
  • hosts: nfs_server

    vars:

    Data_dir: /data

    tasks:

    • name: 01-copy conf file
      copy: src=/etc/ansible/ansible-playbook/nfs-file/nfs-server/exports dest=/etc
      notify: restart nfs server
    • name: 02-create data dir
      file: path={{ Data_dir }} state=directory owner=nfsnobody group=nfsnobody

      path: ['data01','data02','data03']

      state: directory

      owner: nfsnobody

      group: nfsnobody

    • name: 03-boot server

      service: name=rpcbind state=started enabled=yes

      service: name=nfs state=started enabled=yes

      service: name={{ item }} state=started enabled=yes
      with_items:
      • rpcbind
      • nfs

    handlers:

    • name: restart nfs server
      service: name=nfs state=restarted
  • hosts: nfs_client

    vars:

    Data_dir: /data

    tasks:

    • name: 01-mount
      mount: src=172.16.1.31:{{ Data_dir }} path=/mnt fstype=nfs state=mounted
    • name: 02-check mount info
      shell: df -h|grep /data
      register: mount_info
    • name: display mount info
      debug: msg={{ mount_info.stdout_lines }}
      第三個歷程: 進行劇本測試
  1. ansible程序roles --- 規(guī)范
    劇本編寫完問題:

    1. 目錄結構不夠規(guī)范 OK
    2. 編寫好的任務如何重復調(diào)用
    3. 服務端配置文件改動,客戶端參數(shù)信息也自動變化
    4. 匯總劇本中沒有顯示主機角色信息
    5. 一個劇本內(nèi)容信息過多,不容易進行閱讀,如何進行拆分 OK

    第一個歷程: 規(guī)范目錄結構
    cd /etc/ansible/roles
    mkdir {rsync,nfs} --- 創(chuàng)建相應角色目錄
    mkdir {nfs,rsync}/{vars,tasks,templates,handlers,files} --- 創(chuàng)建角色目錄下面的子目錄
    [root@m01 roles]# tree
    .
    ├── nfs
    │ ├── files --- 保存需要分發(fā)文件目錄
    │ ├── handlers --- 保存觸發(fā)器配置文件信息
    │ ├── tasks --- 保存要執(zhí)行的動作信息文件 ok
    │ ├── templates --- 保存需要分發(fā)模板文件 模板文件中可以設置變量信息
    │ └── vars --- 保存變量信息文件
    └── rsync
    ├── files
    ├── handlers
    ├── tasks
    ├── templates
    └── vars

    第二個歷程: 在roles目錄中創(chuàng)建相關文件
    編寫文件流程圖:

    1. 編寫tasks目錄中的main.yml文件
    • name: 01-copy conf file
      copy: src=exports dest=/etc
      notify: restart nfs server
    • name: 02-create data dir
      file: path={{ Data_dir }} state=directory owner=nfsnobody group=nfsnobody

      path: ['data01','data02','data03']

      state: directory

      owner: nfsnobody

      group: nfsnobody

    • name: 03-boot server
      service: name={{ item }} state=started enabled=yes
      with_items:
      • rpcbind
      • nfs

    vim main.yml

    • include_tasks: copy_info.yml
    • include_tasks: create_dir.yml
    • include_tasks: boot_server.yml

    vim copy_info.yml

    • name: 01-copy conf file
      copy: src=exports dest=/etc
      notify: restart nfs server

    vim create_dir.yml

    • name: 02-create data dir
      file: path={{ Data_dir }} state=directory owner=nfsnobody group=nfsnobody

    vim boot_server.yml

    • name: 03-boot server
      service: name={{ item }} state=started enabled=yes
      with_items:
      • rpcbind
      • nfs
    1. 編寫vars目錄中的main.yml文件
      [root@m01 vars]# vim main.yml
      Data_dir: /data

    2. 編寫files目錄中的文件
      [root@m01 files]# ll
      total 4
      -rw-r--r-- 1 root root 29 May 17 15:23 exports

    3. 編寫handlers目錄中的main.yml文件
      vim main.yml

    • name: restart nfs server
      service: name=nfs state=restarted

    目錄中文件編寫好匯總結構
    [root@m01 nfs]# tree
    .
    ├── files
    │ └── exports
    ├── handlers
    │ └── main.yml
    ├── tasks
    │ └── main.yml
    ├── templates
    └── vars
    └── main.yml

    第三個歷程: 編寫一個主劇本文件
    [root@m01 roles]# cat site.yml

    • hosts: nfs_server
      roles:

      • nfs-server
    • hosts: rsync_server
      roles:

      • rsync
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容