ansible

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

主機(jī)清單文件的使用方法
第一種基于密碼連接
主機(jī)+端口+密碼
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. 綠色信息: 查看主機(jī)信息/對(duì)主機(jī)未做改動(dòng)
02. 黃色信息: 對(duì)主機(jī)數(shù)據(jù)信息做了修改
03. 紅色信息: 命令執(zhí)行出錯(cuò)了
04. 粉色信息: 忠告信息
05. 藍(lán)色信息: 顯示ansible命令執(zhí)行的過程

ansible Ad-Hoc 臨時(shí)指令使用方式

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

命令類型模塊:

command (默認(rèn)模塊)在一個(gè)遠(yuǎn)程主機(jī)上執(zhí)行一個(gè)命令

擴(kuò)展用法:

  1. chdir 在執(zhí)行命令之前對(duì)目錄進(jìn)行切換
    例如: 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 如果文件存在了,這個(gè)步驟將執(zhí)行
    例如:ansible 172.16.1.31 -m command -a "removes=/tmp/hosts chdir=/tmp touch oldboy.txt"
    注意事項(xiàng):
    有些符號(hào)信息無法識(shí)別: <", ">", "|", ";" and "&"

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

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

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

2.script腳本模塊

編寫腳本

在本地運(yùn)行模塊,等同于在遠(yuǎn)程執(zhí)行窜管,不需要將腳本文件進(jìn)行推送目標(biāo)主機(jī)執(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 – 復(fù)制文件到遠(yuǎn)程主機(jī)
例如: ansible 172.16.0.3 -m copy -a "src=/etc/hosts dest=/etc/"
將本機(jī)/etc/hosts文件發(fā)送到 172.16.1.3的/etc/目錄
172.16.0.31 | CHANGED => { --- 對(duì)哪臺(tái)主機(jī)進(jìn)行操作
"changed": true, --- 是否對(duì)主機(jī)信息進(jìn)行改變
"checksum": "6ed7f68a1d6b4b36c1418338b2001e421eeba270", --- 生成一個(gè)文件校驗(yàn)碼==MD5數(shù)值
"dest": "/etc/hosts", --- 顯示目標(biāo)路徑信息
"gid": 0, --- 顯示復(fù)制后文件gid信息
"group": "root", --- 顯示復(fù)制后文件屬組信息
"md5sum": "7afd7b74854f0aaab646b3e932f427c0", --- 生成一個(gè)文件校驗(yàn)碼==MD5數(shù)值
"mode": "0644", --- 顯示復(fù)制后文件權(quán)限信息
"owner": "root", --- 顯示復(fù)制后文件屬主信息
"size": 401, --- 顯示文件的大小信息
"src": "/root/.ansible/tmp/ansible-tmp-1557804498.23-26487341925325/source",
"state": "file", --- 顯示文件的類型信息
"uid": 0 --- 顯示復(fù)制后文件uid信息
}
擴(kuò)展用法:
01. 在傳輸文件時(shí)修改文件的屬主和屬組信息
ansible 172.16.1.31 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ owner=oldboy group=oldboy"
02. 在傳輸文件時(shí)修改文件的權(quán)限信息
ansible 172.16.1.31 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ mode=1777"
03. 在傳輸數(shù)據(jù)文件信息時(shí)對(duì)遠(yuǎn)程主機(jī)源文件進(jìn)行備份
ansible 172.16.1.31 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ backup=yes"
04. 創(chuàng)建一個(gè)文件并直接編輯文件的信息
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ù)指定文件信息,會(huì)在本地管理端服務(wù)進(jìn)行查找
If yes it will go to the remote/target machine for the src. Default is no.
       src參數(shù)指定文件信息,會(huì)從遠(yuǎn)程主機(jī)上進(jìn)行查找

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

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

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

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

創(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模塊: 管理服務(wù)器的運(yùn)行狀態(tài) 停止 開啟 重啟
name: --- 指定管理的服務(wù)名稱
state: --- 指定服務(wù)狀態(tài)
started 啟動(dòng)
restarted 重啟
stopped 停止
enabled --- 指定服務(wù)是否開機(jī)自啟動(dòng)
ansible 172.16.1.31 -m service -a "name=nfs state=started enabled=yes"

cron模塊: 批量設(shè)置多個(gè)主機(jī)的定時(shí)任務(wù)信息

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

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

擴(kuò)展用法:
01. 給定時(shí)任務(wù)設(shè)置注釋信息
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. 刪除指定定時(shí)任務(wù)
ansible 172.16.1.31 -m cron -a "name='time sync01' state=absent"
PS: ansible可以刪除的定時(shí)任務(wù),只能是ansible設(shè)置好的定時(shí)任務(wù)
03. 批量注釋定時(shí)任務(wù)
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: 批量進(jìn)行掛載操作
       src:  需要掛載的存儲(chǔ)設(shè)備或文件信息
       path: 指定目標(biāo)掛載點(diǎn)目錄
       fstype: 指定掛載時(shí)的文件系統(tǒng)類型
       state
       present/mounted     --- 進(jìn)行掛載
       present: 不會(huì)實(shí)現(xiàn)立即掛載,修改fstab文件,實(shí)現(xiàn)開機(jī)自動(dòng)掛載
       mounted: 會(huì)實(shí)現(xiàn)立即掛載, 并且會(huì)修改fstab文件,實(shí)現(xiàn)開機(jī)自動(dòng)掛載 *****
       
       absent/unmounted    --- 進(jìn)行卸載
       absent:     會(huì)實(shí)現(xiàn)立即卸載, 并且會(huì)刪除fstab文件信息,禁止開機(jī)自動(dòng)掛載
       unmounted:  會(huì)實(shí)現(xiàn)立即卸載, 但是不會(huì)會(huì)刪除fstab文件信息  *****

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

擴(kuò)展用法:
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模塊設(shè)置用戶密碼信息,需要將密碼明文信息轉(zhuǎn)換為密文信息進(jìn)行設(shè)置
生成密文密碼信息方法:
方法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. 劇本的編寫方法
    劇本的作用: 可以一鍵化完成多個(gè)任務(wù)
    自動(dòng)化部署rsync服務(wù):
    服務(wù)端的操作
    第一個(gè)歷程安裝軟件:
    ansible 172.16.1.41 -m yum -a "name=rsync state=installed"

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

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

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

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

    第六個(gè)歷程啟動(dòng)服務(wù)
    ansible 172.16.1.41 -m service -a "name=rsyncd state=started enabled=yes"

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

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

    演員信息: 男二號(hào)
    干的事情: 看著

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

    1. 合理的信息縮進(jìn) 兩個(gè)空格表示一個(gè)縮進(jìn)關(guān)系
      標(biāo)題一
      標(biāo)題二
      標(biāo)題三
      PS: 在ansible中一定不能用tab進(jìn)行縮進(jìn)

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

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

      • 張三

        • 打游戲
        • 運(yùn)動(dòng)
      • 李四

        學(xué)習(xí)
        湖南

      • 王五

        運(yùn)動(dòng)
        深圳
        PS: 使用短橫線構(gòu)成列表信息,短橫線后面需要有空格

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

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

    如何執(zhí)行劇本:
    第一個(gè)步驟: 檢查劇本的語法格式
    ansible-playbook --syntax-check rsync_server.yaml
    第二個(gè)步驟: 模擬執(zhí)行劇本
    ansible-playbook -C rsync_server.yaml
    第三個(gè)步驟: 直接執(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. 利用劇本完成服務(wù)一鍵化部署:
    rsync 服務(wù)部署
    nfs 服務(wù)部署
    sersync 服務(wù)部署
    全網(wǎng)備份項(xiàng)目

    rsync服務(wù)劇本編寫:
    準(zhǔn)備工作:

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

    劇本編寫常見錯(cuò)誤:

    1. 劇本語法規(guī)范是否符合(空格 冒號(hào) 短橫線)
    2. 劇本中模塊使用是否正確
    3. 劇本中一個(gè)name標(biāo)識(shí)下面只能寫一個(gè)模塊任務(wù)信息
    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. 如何配置主機(jī)清單
    第一種方式: 分組配置主機(jī)信息
    [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

    第二種方式: 主機(jī)名符號(hào)匹配配置
    [web]
    172.16.1.[7:9]
    [web]
    web[01:03]

    第三種方式: 跟上非標(biāo)準(zhǔn)遠(yuǎn)程端口
    [web]
    web01:52113
    172.16.1.7:52113

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

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

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

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

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

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

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

    如何全局設(shè)置變量: roles 劇本整合

  1. 在劇本中設(shè)置注冊(cè)信息

    • 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ìn)程信息,表示服務(wù)已經(jīng)正常啟動(dòng)
        PS: 設(shè)置變量不能有空格信息

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

    • 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"
    常見主機(jī)信息:
    ansible_all_ipv4_addresses: 僅顯示ipv4的信息捏检。
    ansible_devices: 僅顯示磁盤設(shè)備信息取募。
    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: 僅顯示主機(jī)名填帽。
    ansible_kernel: 僅顯示內(nèi)核版本。
    ansible_lvm: 顯示lvm相關(guān)信息咙好。
    ansible_memtotal_mb: 顯示系統(tǒng)總內(nèi)存篡腌。
    ansible_memfree_mb: 顯示可用系統(tǒng)內(nèi)存。
    ansible_memory_mb: 詳細(xì)顯示內(nèi)存情況勾效。
    ansible_swaptotal_mb: 顯示總的swap內(nèi)存嘹悼。
    ansible_swapfree_mb: 顯示swap內(nèi)存的可用內(nèi)存叛甫。
    ansible_mounts: 顯示系統(tǒng)磁盤掛載情況。
    ansible_processor: 顯示cpu個(gè)數(shù)(具體顯示每個(gè)cpu的型號(hào))杨伙。
    ansible_processor_vcpus: 顯示cpu個(gè)數(shù)(只顯示總的個(gè)數(shù))合溺。

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

  3. 在劇本中設(shè)置循環(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. 在劇本中設(shè)置忽略錯(cuò)誤
    默認(rèn)playbook會(huì)檢查命令和模塊的返回狀態(tài),如遇到錯(cuò)誤就中斷playbook的執(zhí)行
    可以加入ignore_errors: yes忽略錯(cuò)誤
    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. 在劇本中設(shè)置標(biāo)簽功能

    • 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í)行哪個(gè)標(biāo)簽任務(wù): ansible-playbook --tags=t2 test05.yml
    跳過指定標(biāo)簽任務(wù): ansible-playbook --skip-tags=t2 test05.yml

  6. 在劇本中設(shè)置觸發(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. 將多個(gè)劇本進(jìn)行整合
    方式一: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. 在劇本中設(shè)置變量信息
    方式一:直接在劇本文件編寫
    vars:
    oldboy01: data01
    oldboy02: data02

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

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

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

    如何全局設(shè)置變量: roles 劇本整合

  1. 在劇本中設(shè)置注冊(cè)信息

    • 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ìn)程信息,表示服務(wù)已經(jīng)正常啟動(dòng)
        PS: 設(shè)置變量不能有空格信息

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

    • 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"
    常見主機(jī)信息:
    ansible_all_ipv4_addresses: 僅顯示ipv4的信息缀台。
    ansible_devices: 僅顯示磁盤設(shè)備信息棠赛。
    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: 僅顯示主機(jī)名怔揩。
    ansible_kernel: 僅顯示內(nèi)核版本。
    ansible_lvm: 顯示lvm相關(guān)信息脯丝。
    ansible_memtotal_mb: 顯示系統(tǒng)總內(nèi)存商膊。
    ansible_memfree_mb: 顯示可用系統(tǒng)內(nèi)存。
    ansible_memory_mb: 詳細(xì)顯示內(nèi)存情況宠进。
    ansible_swaptotal_mb: 顯示總的swap內(nèi)存晕拆。
    ansible_swapfree_mb: 顯示swap內(nèi)存的可用內(nèi)存。
    ansible_mounts: 顯示系統(tǒng)磁盤掛載情況材蹬。
    ansible_processor: 顯示cpu個(gè)數(shù)(具體顯示每個(gè)cpu的型號(hào))实幕。
    ansible_processor_vcpus: 顯示cpu個(gè)數(shù)(只顯示總的個(gè)數(shù))。

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

  3. 在劇本中設(shè)置循環(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)錯(cuò)誤排查思路/步驟:

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

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

  1. 在劇本中設(shè)置忽略錯(cuò)誤
    默認(rèn)playbook會(huì)檢查命令和模塊的返回狀態(tài)堤器,如遇到錯(cuò)誤就中斷playbook的執(zhí)行
    可以加入ignore_errors: yes忽略錯(cuò)誤
    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. 在劇本中設(shè)置標(biāo)簽功能

    • 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í)行哪個(gè)標(biāo)簽任務(wù): ansible-playbook --tags=t2 test05.yml
    跳過指定標(biāo)簽任務(wù): ansible-playbook --skip-tags=t2 test05.yml

  3. 在劇本中設(shè)置觸發(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. 將多個(gè)劇本進(jìn)行整合
    方式一: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服務(wù)劇本
第一個(gè)歷程: 創(chuàng)建幾個(gè)目錄
[root@m01 ansible-playbook]# tree nfs-file/
nfs-file/
├── nfs-client
└── nfs-server

第二個(gè)歷程: 編寫劇本信息
主機(jī)清單:
[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 }}
      第三個(gè)歷程: 進(jìn)行劇本測試
  1. ansible程序roles --- 規(guī)范
    劇本編寫完問題:

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

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

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

    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

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

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

    • hosts: nfs_server
      roles:

      • nfs-server
    • hosts: rsync_server
      roles:

      • rsync
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末昆庇,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子闸溃,更是在濱河造成了極大的恐慌整吆,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,542評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件圈暗,死亡現(xiàn)場離奇詭異掂为,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)员串,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來昼扛,“玉大人寸齐,你說我怎么就攤上這事欲诺。” “怎么了渺鹦?”我有些...
    開封第一講書人閱讀 163,912評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵扰法,是天一觀的道長。 經(jīng)常有香客問我毅厚,道長塞颁,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,449評(píng)論 1 293
  • 正文 為了忘掉前任吸耿,我火速辦了婚禮祠锣,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘咽安。我一直安慰自己伴网,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評(píng)論 6 392
  • 文/花漫 我一把揭開白布妆棒。 她就那樣靜靜地躺著澡腾,像睡著了一般。 火紅的嫁衣襯著肌膚如雪糕珊。 梳的紋絲不亂的頭發(fā)上动分,一...
    開封第一講書人閱讀 51,370評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音红选,去河邊找鬼刺啦。 笑死,一個(gè)胖子當(dāng)著我的面吹牛纠脾,可吹牛的內(nèi)容都是我干的玛瘸。 我是一名探鬼主播,決...
    沈念sama閱讀 40,193評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼苟蹈,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼糊渊!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起慧脱,我...
    開封第一講書人閱讀 39,074評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤渺绒,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后菱鸥,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體宗兼,經(jīng)...
    沈念sama閱讀 45,505評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評(píng)論 3 335
  • 正文 我和宋清朗相戀三年氮采,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了殷绍。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,841評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡鹊漠,死狀恐怖主到,靈堂內(nèi)的尸體忽然破棺而出茶行,到底是詐尸還是另有隱情,我是刑警寧澤登钥,帶...
    沈念sama閱讀 35,569評(píng)論 5 345
  • 正文 年R本政府宣布畔师,位于F島的核電站,受9級(jí)特大地震影響牧牢,放射性物質(zhì)發(fā)生泄漏看锉。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評(píng)論 3 328
  • 文/蒙蒙 一塔鳍、第九天 我趴在偏房一處隱蔽的房頂上張望伯铣。 院中可真熱鬧,春花似錦献幔、人聲如沸懂傀。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蹬蚁。三九已至,卻和暖如春郑兴,著一層夾襖步出監(jiān)牢的瞬間犀斋,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評(píng)論 1 269
  • 我被黑心中介騙來泰國打工情连, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留叽粹,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,962評(píng)論 2 370
  • 正文 我出身青樓却舀,卻偏偏與公主長得像虫几,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子挽拔,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容