ansible

1.ansible批量管理服務部署
1.管理端服務器
a.安裝軟件
yum install -y ansible(要epel源)
01. /etc/ansible/ansible.cfg --- ansible服務配置文件
02. /etc/ansible/hosts --- 主機清單文件
03. /etc/ansible/roles --- 角色目錄
b.編寫主機清單文件
vim /etc/ansible/hosts
c.測試是否可以管理多個主機
ansible all -a "hostname"
d.ansible服務架構信息
1.主機清單配置
2.軟件模塊信息
3.基于秘鑰連接主機
4.主機需要關閉selinux
5.軟件劇本功能
2.ansible軟件模塊應用
ansible的官方文檔doc.ansible.com
ansible學習模塊命令:
a.列出全部模塊 : ansible-doc -l
b.指定一個模塊詳細說明: ansible-doc -s fetch
c.查詢模塊在劇本中的方法: ansible-doc fetch
ansible模塊的語法:
ansible 主機名稱/主機組名稱/主機地址信息/all -m(指定模塊) 模塊名稱 -a(指定動作) "執(zhí)行的動作"
3.第一個模塊: command(默認的模塊可不寫)
1.定義:command - Executes a command on a remote node 在一個遠程主機上執(zhí)行命令
2.基本用法: ansible all -m command -a "cat /etc/hosts"
3.擴展用法:
a. chdir: Change into this directory before running the command. ---切換目錄,默認是在/root下
命令: ansbible all -m command -a "chdir=/test touch lin.txt" 切換到/test目錄下旷余,創(chuàng)建文件lin.txt
b. creates: If it already exists, this step won't be run. --- 如果creates=/dir/file 存在,則不執(zhí)行后面動作
命令: ansible all -m command -a "chdir=/test creates=/test/lin.txt touch wen.txt" 因lin.txt存在惶看,不能touch,如果不存在則可以touch
c. removes: If it already exists, this step will be run. 與creates相反
d. free_form(required) The command module takes a free form command to run.
There is no actual parameter named 'free form'. ;即 用command模塊時,-a參數(shù)后要跟上合法的linux命令
f.PS: variables like HOME and operations like "<", ">", "|", ";" and "&" will not work. Use the shell module if you need these features. 4.第二個模塊: shell(萬能模塊) 1.定義:command - Executes command in node 在節(jié)點上執(zhí)行操作 2.基本用法: ansible all -m shell -a "cat /etc/hosts" 3.擴展用法: a. chdir: Change into this directory before running the command. ---切換目錄欺税,默認是在/root下 命令: ansbible all -m shell -a "chdir=/test touch lin.txt" 切換到/test目錄下侈沪,創(chuàng)建文件lin.txt b. creates: If it already exists, this step won't be run. --- 如果creates=/dir/file 存在,則不執(zhí)行后面動作 命令: ansible all -m shell -a "chdir=/test creates=/test/lin.txt touch wen.txt" 因lin.txt存在晚凿,不能touch亭罪,如果不存在則可以touch c. removes: If it already exists, this step will be run. 與creates相反 d. free_form(required) The command module takes a free form command to run. There is no actual parameter named 'free form'. ;即 用command模塊時,-a參數(shù)后要跟上合法的linux命令 4.利用shell執(zhí)行腳本: 1.縮寫一個腳本 2.將腳本發(fā)送到遠程主機 3.將腳本權限進行修改(添加執(zhí)行權限) 4.運行ansible命令執(zhí)行腳本 5.第三個模塊: script(腳本模塊) 1.編寫一個腳本(比shell模塊好歼秽,不用發(fā)到遠程主機上应役,在ansible主機上就可以) 2.運行ansible命令執(zhí)行腳本 3.script模塊參數(shù)功能也和command模塊類似 6.文件類型模塊(copy): 1.copy - copies files to remove locations --- 將數(shù)據(jù)信息進行批量分發(fā) 2.基本用法 : ansible 192.168.3.18 -m copy -a "src=/server/scripts/status.sh dest=/server/scrippts/" 192.168.3.18 | CHANGED => { ----對哪臺主機進行操作 "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, ----是否對主機信息進行改變 "checksum": "6d07bf461fa199c86a33766446ebcce149e92d0a", -生成文件校對碼 md5sum "dest": "/server/scripts/status.sh", ----顯示目錄路徑信息 "gid": 0, ----顯示復制后文件gid信息 "group": "root", ----文件屬組信息 "md5sum": "1e70438b1ed56eb3e26e50ccf5df3afb", "mode": "0644", ----顯示復制后文件權限 "owner": "root", ----顯示復制后文件屬主 "secontext": "system_u:object_r:default_t:s0", "size": 131, ----顯示文件大小 "src": "/root/.ansible/tmp/ansible-tmp-1580990976.09-32883763294237/source", "state": "file", ----顯示文件類型 "uid": 0 ----顯示復制后文件UID信息 } 3.ansible軟件輸出顏色說明: a.綠色信息: 查看主機信息/對主機未做改動 b.黃色信息: 對主機數(shù)據(jù)信息做了修改 c.紅色信息: 命令執(zhí)行出錯了 d.粉色信息: 忠告信息 f.藍色信息: 顯示ansible命令執(zhí)行的過程 4.擴展用法: 1.owner group 傳輸文件時修改文件的屬主和屬組信息 Name of the user that should own the file/directory, as would be fed to chown. 命令: ansible all -m copy -a "src=/server/scripts/status.sh dest=/server/scripts/ owner=lin group=lin" 2.mode : 在傳輸文件時修改文件的權限信息 命令: ansible all -m copy -a "src=/server/scripts/status.sh dest=/server/scripts/ mode=777" 3.backup : 在傳輸數(shù)據(jù)文件信息時對遠程主機源文件進行備份 命令: ansible all -m copy -a "src=/server/scripts/status.sh dest=/server/scripts/ backup=yes" 192.168.3.18 | CHANGED => { "backup_file": "/server/scripts/status.sh.16254.2020-02-06@20:30:17~", 4.content: 創(chuàng)建一個文件并直接編輯文件的信息 命令: ansible 192.168.3.18 -m copy -a "content='lin.123' dest=/server/scripts/lin.txt" 5.remote_src 將遠程的文件移到遠程的目錄下,不找ansible下面的 命令: ansible 192.168.3.18 -m copy -a "content='lin.123' dest=/server/scripts/lin.txt" PS: 多主機時燥筷,18的移動到18下箩祥,20的移動到20下 默認remote_src=no 7.文件模塊(file) 1.概念: file - sets atributes of files 設置文件屬性信息 2.基本用法 : ansible all -m file -a "dest=/server/scripts/status.sh owner=lin group=lin mode=777" 3.擴展用法: 01.可以利用模塊創(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)建一個文件信息 02.創(chuàng)建目錄信息: 一級目錄:ansible 192.168.3.18 -m file -a "dest=/test01/ state=directory" 多級目錄: ansible 192.168.3.18 -m file -a "dest=/test01/02/03 state=directory" 03.創(chuàng)建文件信息: ansible 192.168.3.18 -m file -a "dest=/test01/lin.txt state=touch" 04.創(chuàng)建鏈接信息: 硬鏈接: ansible 192.168.3.18 -m file -a "src=/test01/lin.txt dest=/test01/lin_hard.txt state=hard" 軟鏈接: ansible 192.168.3.18 -m file -a "src=/test01/lin.txt dest=/test01/lin_link.txt state=link" 05.刪除文件數(shù)據(jù)信息: ansible 192.168.3.18 -m file -a "dest=/test01/lin_hard.txt state=absent" 06.刪除目錄: ansible 192.168.3.18 -m file -a "dest=/test01t state=absent" 07.遞歸 : recurse 只有當state是directory時肆氓,才能設置recurse=yes,可改目錄下的所有文件 8.YUM模塊: 1.概念: Manages packages with the yum package manager 2.基本用法: ansible 192.168.3.20 -m yum -a "name=iotop state=installed" 3.參數(shù)命令: name ----安裝軟件名稱 state ----指定是否安裝軟件 installed ,present, latest ---安裝軟件 absent,removed ---卸載軟件 9.service模塊: 1.概念: 管理服務器的運行狀態(tài) 停止 開啟 重啟 2.基本用法: ansible 192.168.3.18 -m service -a "name=nfs state=started enabled=yes" 3.參數(shù)命令: name ----指定管理的服務名稱 state ----指定服務狀態(tài): started stopped restarted enabled----指定服務是否開機自啟動 10.cron模塊: 1.概念: 批量設置多個主機的定時任務信息 2.基本用法: ansible 192.168.3.18 -m cron -a "minute=0 hour=20 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'" 3.參數(shù)命令:minute ----設置分鐘信息 ( 0-59, *, */2, etc ) hour ----設置小時信息 ( 0-23, *, */2, etc ) day ----設置日期信息 ( 1-31, *, */2, etc ) month ----設置月份信息 ( 1-12, *, */2, etc ) weekday ----設置周信息 ( 0-6 for Sunday-Saturday, *, etc ) job ----定義定時任務需要的動作 4.擴展用法: a.給定時任務設置注釋信息: ansible 192.168.3.18 -m cron -a "name='time sync' minute=0 hour=21 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'" b.刪除ansible指定定時任務 ansible 192.168.3.18 -m cron -a "name='time sync' state=absent" PS: 只能刪除ansible指定的定時任務 c.批量注釋定時任務 ansible 192.168.3.18 -m cron -a "name='time sync' job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1' disabled=yes" PS:disabled=yes注釋任務袍祖, disabled=no時恢復定時任務 11.mount模塊 1.概念:批量進行掛載操作 2.基本用法 : ansible all -m mount -a "src=192.168.3.12:/data path=/mnt fstype=nfs state=mounted" 3.參數(shù)命令: src ----需要掛載的存儲設備或文件信息 path ----指定目標掛載點目錄(老版有name,dest有可能會修改) fstype ----掛載時的文件系統(tǒng) state ----進行掛載:present/mounted 進行卸載:absent/unmounted PS: present:不會立即掛載,修改fstab文件谢揪,實現(xiàn)開機自動掛載 *****mounted: 會立即掛載蕉陋,修改fstab文件,實現(xiàn)開機自動掛載 *****unmounted: 會立即卸載拨扶,不會刪除fstab文件信息 absent: 會立即卸載凳鬓,并且會刪除fstab文件信息 ,禁止開機自動掛載 12.USER模塊 1.概念: 批量創(chuàng)建用戶 2.基本用法: ansible all -m user -a "name=lin01" 3.擴展用法: UID ----指定UID group ---指定主組 groups---指定附屬組 a.指定用戶UID :ansible all -m user -a "name=lin02 uid=1008" b.指定用戶組信息: 1.主組信息:ansible all -m user -a "name=lin03 group=lin02" 2.主組附屬組都有: ansible all -m user -a "name=lin05 groups=lin02" c.創(chuàng)建虛擬用戶:ansible all -m user -a "name=lin06 create_home=no shell=/sbin/nologin" d.給指定用戶創(chuàng)建密碼 PS:要設置密碼患民,需要將密碼明文轉成密文 生成密文方法(ansible文檔上有): 1. mypassword=輸入的密碼 mysecretsalt=加密校驗信息 加強密碼使用 ansible all -i localhost, -m debug -a "msg={{ 'mypassword' | password_hash('sha512', 'mysecretsalt') }}" [root@nginx tmp]# ansible all -i localhost, -m debug -a "msg={{ 'lin123' | password_hash('sha512', 'wen') }}" localhost | SUCCESS => { "msg": "6wenShwRLP5IXGnTa3xcQ/4N2sj4pym3fccJBykjmlayizu97knMWnsXYmywEgNESideMnIHjhvRhYW0534PFjVvB0"
}
2.mkpasswd --method=sha512 --失敗
3. python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"
Password:
6m3BJ5CMGFWpLkcs.iJpLuJvRBB.J3CzNbSXNJyFZZyvzzvcfWoetegViTWyTU546g0M5KzN.X38Tc//Hoq2d1KRTVMTS3HyQH6w10/ 4.基本命令: ansible all -m user -a "name=lin08 password="'6m3BJ5CMGFWpLkcs.iJpLuJvRBB.J3CzNbSXNJyFZZyvzzvcfWoetegViTWyTU546g0M5KzN.X38Tc//Hoq2d1KRTVMTS3HyQH6w10/'"
PS:密文一定要加上''號缩举,因為有特殊符號在里面$
2.playbook劇本編寫
01.劇本編寫規(guī)范 yaml格式
a.縮進空格規(guī)范: 要用相同的空格數(shù)進行縮進
b.冒號規(guī)范: :后面要跟空格 (:結尾或在注釋說明中不要加空格)
c.短橫線應用- : 表示列表功能
02.劇本的執(zhí)行:
a.檢查劇本的語法格式:
ansible-playbook --syntax-check rsync.yaml格式
b.模擬執(zhí)行劇本:
ansible-playbook -C rsync.ymal
c.執(zhí)行劇本:
ansible-playbook rsync.ymal
03.rsync服務劇本編寫: vim /etc/ansible/ansible_playbook/rsync.yaml格式
a.先按安裝部署rsync流程來編寫playbook:
- hosts: 192.168.3.18
tasks:
- name: 01-install rsyncd
yum: name=rsync state=installed
- name: 02-copy configer file
copy: src=/etc/rsyncd.conf dest=/etc/
- name: 03-create rsync user
user: name=rsync create_home=no shell=/sbin/nologin
- name: 04-create backup dir
file: dest=/backup state=directory owner=rsync group=rsync
- name: 05-create password file
copy: content=rsync_backup:lin19831214 dest=/etc/rsync.password mode=600
- name: 06-start rsync server
service: name=rsyncd state=started enabled=yes

  • hosts: 192.168.3.20
    tasks:
    • name: 01-install rsync
      yum: name=rsync state=installed
    • name: 02-create password file
      copy: content=lin19831214 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@192.168.3.18::ftp --password-file=/etc/rsync.password
      b.劇本編寫常見錯誤:
      01.劇本語法規(guī)范是否符合(空格 冒號 短橫線)
      02.劇本模塊使用是否正常
      03.劇本中一個name標識下面只能寫一個模塊任務信息
      04.劇本中盡量不要大量使用shell模塊,有時會報錯匹颤,類似用shell代替user時蚁孔,當user有時,用shell會報錯
      3.主機清單配置:
      1.分組配置主機信息
      [nginx]
      192.168.3.17
      192.168.3.18
      192.168.3.19
      [data]
      192.168.3.20
      192.168.3.21
      2.主機名符號匹配配置
      a. [nginx]
      192.168.3.[17:19]
      b. [nginx]
      nginx[01:03]
      3.非標準遠程端口配置(不是22端口時)
      [nginx]
      192.168.3.17:port號(比如522)
      4.使用特殊的變量 ssh_port -- 商品 ssh_user --用戶 ssh_pass --密碼 ssh_host 主機名 不用/etc/hosts對照
      [nginx]
      192.168.3.18 ansible_ssh_port=522 ansible_ssh_user=root ansible_ssh_pass=lin123
      [nginx]
      nginx01 ansible_ssh_host=192.168.3.18 ansible_ssh_port=522 ansible_ssh_user=root ansible_ssh_pass=lin123
      5.主機組名嵌入配置
      a.[rsync:children] --- 嵌入子組信息
      rsync_server
      rsync_client
      [rsync_server]
      192.168.3.18
      [rsync_client]
      192.168.3.20
      b.[rsync:vars]
      ansible_ssh_host=192.168.3.18
      ansible_ssh_port=522
      ansible_ssh_user=root
      ansible_ssh_pass=lin123
      [rsync]
      rsync01
      6.ansible變量用法:
      1.直接在劇本文件編寫 (引用變量時要用{{ 變量 }} PS:配置文件中要修改惋嚎,不然不能成功
  • hosts: 192.168.3.18
    vars:
    backupdir: /backup
    passfile: /rsync.password
    tasks:
    • name: 01-install rsyncd
      yum: name=rsync state=installed
    • name: 02-copy configer file
      copy: src=/etc/rsyncd.conf dest=/etc/
    • name: 03-create rsync user
      user: name=rsync create_home=no shell=/sbin/nologin
    • name: 04-create backup dir
      file: dest={{ backupdir }} state=directory owner=rsync group=rsync
    • name: 05-create password file
      copy: content=rsync_backup:lin19831214 dest=/etc/{{ passfile }} state=touch mode=600
    • name: 06-start rsync server
      service: name=rsyncd state=started enabled=yes
  • hosts: 192.168.3.20
    vars:
    passfile: rsync.password
    tasks:
    • name: 01-install rsync
      yum: name=rsync state=installed
    • name: 02-create password file
      copy: content=lin19831214 dest=/etc/{{ passfile }} 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@192.168.3.18::ftp --password-file=/etc/{{ passfile }}
      2.在命令行中進行指定
      ansible-playbook -e(--extra-vars) backupdir=/backup -e passfile=rsync.password rsync_var.yaml
      3.在主機清單文件中編寫
      [rsync_server:vars]
      backupdir=/backup
      passfile=rsync.password
      [rsync_client]
      192.168.3.20
      [rsync_client:vars]
      passfile=rsync.password
      4.三個變量設置的優(yōu)先級: 命令行>劇本變量>主機清單變量
    1. 劇本設置注冊信息 ---可顯示輸出命令結果信息 register --
    • name: 06-start rsync server
      service: name=rsyncd state=started enabled=yes
    • name: 07-check server port
      shell: netstat -tlnup|grep 874 ---端口信息
      register: get_server_port ---注冊端口信息
    • name: display port info
      debug: msg={{ get_server_port.stdout_lines }} <--注冊的變量在里面
    1. 在劇本中設置判斷信息 ----用when: 來設置
      1.基本用法: - name: 03-create test file
      file: dest=/tmp/nfs.txt state=touch
      when: (ansible_hostname == "nfs")
      - name: 03-create test file
      file: dest=/tmp/mysql.txt state=touch
      when: (ansible_hostname == "mysql")
      - name: 04-check test
      shell: rsync -avz /tmp/nfs.txt rsync_backup@192.168.3.18::nfs --password-file=/etc/rsync.password
      when: (ansible_hostname == "nfs")
      - name: 04-check test
      shell: rsync -avz /tmp/mysql.txt rsync_backup@192.168.3.18::mysql --password-file=/etc/rsync.password
      when: (ansible_hostname == "mysql")
      2.setup模塊可以查看被管理主機系統(tǒng)的詳細信息 ansible 主機 -m setup
      3.獲取內置變量方法: ansible 主機 -m setup -a "filter=ansible_hostname"
      4.常見主機信息:
      "ansible_all_ipv4_addresses": ---僅顯示ipv4信息
      "ansible_devices" ---顯示磁盤設備信息
      "ansible_distribution": "CentOS" ---顯示系統(tǒng)信息
      "ansible_distribution_major_version": "7", ---顯示系統(tǒng)主版本
      "ansible_distribution_version": "7.6", ---顯示系統(tǒng)版本
      "ansible_machine": "x86_64", ---顯示系統(tǒng)類型 32位或64位
      "ansible_ens33": ---顯示ens33信息
      "ansible_hostname": ---顯示主機名
      "ansible_kernel": "3.10.0-957.el7.x86_64", ---顯示內核版本
      "ansible_lvm": ---LVM相關信息
      "ansible_memtotal_mb": 972 ---系統(tǒng)總內存
      "ansible_memfree_mb": 69, ---可用系統(tǒng)內存
      "ansible_memory_mb": ---詳細顯示內存情況
      "ansible_swapfree_mb": 2047, ---總的swap內存
      "ansible_swaptotal_mb": 2047 ---swap內存的可用內存
      "ansible_mounts" ---磁盤掛載情況
      "ansible_processor" ---顯示cpu個數(shù) (具體顯示每個cpu的型號)
      "ansible_processor_vcpus": 1, ---顯示cpu個數(shù) (只顯示總個數(shù))

      5.獲取子信息方法: (在劇本中可用,命令行有時沒效果)
      ansible 192.168.3.18 -m setup -a "filter=ansible_ens33[ipv4]"
      9.在劇本中設置循環(huán)信息:
      1.循環(huán)主要是為了合并劇本的重復模塊站刑,比如把兩個copy的模塊合并
      2.兩個模塊的命令要特別的相似才行
      3.用法:#copy: src=/etc/ansible/server_file/rsync_server/rsyncd.conf dest=/etc/ mode=644
      #copy: src=/etc/ansible/server_file/rsync_server/rsync.password dest=/etc/ mode=600
      copy: src=/etc/ansible/server_file/rsync_server/{{ item.src }} dest={{ item.dest }} mode={{ item.mode }}
      with_items:
      - { src: 'rsyncd.conf', dest: '/etc/', mode: '644' }
      - { src: 'rsync.password', dest: '/etc/', mode: '600' }
      4.用法2:yum 安裝多軟件時:
      - name: installed pkg
      yum:
      name=['rsync', 'tree', 'wget', 'lrzsz']
      state=installed
      10.劇本執(zhí)行出現(xiàn)錯誤排查
      a.找到書本出現(xiàn)問題關鍵點
      b.將劇本中的操作轉換成模塊進行操作
      c.將模塊的功能操作轉換成linux命令:
      01.本地管理主機上執(zhí)行命令測試
      02.遠程被管理主機上執(zhí)行命令測試
      d.劇本執(zhí)行卡死 ---ansible-playbook XX.ymal -vvvv(最多四個V)
      11.劇本中設置忽略錯誤信息
      1.通過在playbook劇本在另伍,在報錯的那行下面寫上

      • name: 02-copy configer file
        copy: rc=/etc/rsyncd.conf dest=/etc/
        ignore_errors: yes
        2.可以通過設置ignore_errors: yes通過忽略報錯那行,可以執(zhí)行下绞旅,是否下面有報錯有影響
        12.劇本中設置標簽功能:
        1.原因:主要是為了跳過正常的步驟摆尝,不會再執(zhí)行,浪費時間因悲,直接給報錯那行打上tags
        2.配置:- name: 02-copy configer file
        copy: rc=/etc/rsyncd.conf dest=/etc/
        tags: copy configer --在要執(zhí)行的行下面打tags不能用純數(shù)字
        3.命令:ansible-playbook --tags='copy configer' rsync_test.yaml
        13.劇本中設置觸發(fā)功能:
        1.原因:當nofity配置或文件改變時堕汞,通過handlers:來改變一些模塊
        2.配置: - name: 02-copy configer file & password file
        copy: src=/etc/ansible/server_file/rsync_server/{{ item.src }} dest={{ item.dest }} mode={{ item.mode }}
        with_items:
        - { src: 'rsyncd.conf', dest: '/etc/', mode: '644' }
        - { src: 'rsync.password', dest: '/etc/', mode: '600' }
        notify: restart rsync server
        service: name=rsyncd state=started enabled=yes
        handlers: ---與tasks對齊 當rsyncd.conf配置文件修改時,才執(zhí)行這下面的任務晃琳,restart rsyncd
        - name: restart rsync server
        service: name=rsyncd state=restarted
        14.ansible性能調優(yōu):
        1.原因:在執(zhí)行playbook時讯检,系統(tǒng)有個自動的 TASK [Gathering Facts]琐鲁,這個主要是收集系統(tǒng)的信息,架構人灼,主機類的,影響性能有時能關閉掉
        2.方法: gather_facts: no
        15.ansible的playbook劇本整合
        1.原因:把多個劇本整合在一起成為一個劇本進行執(zhí)行
        2.方法: [root@nginx ansible_playbook]# vim site.yaml
        - import_playbook: rsync.yaml
        - import_playbook: nfs.yaml
        3.命令: ansible-playbook site.yaml
        16.ansible程序roles ---規(guī)范
    2. ansible劇本編寫完問題:
      01.目錄結構不夠規(guī)范
      02.編寫好的任務如何重復調用
      03.服務端配置文件改動围段,客戶端參數(shù)信息也自動變化
      04.匯總劇本中沒有顯示主機角色信息
      05.一個劇本內容信息過多,不容易進行閱讀投放,如何進行拆分
      06.這個就是引出了role的使用奈泪,也可以不用
      2.規(guī)范目錄結構
      01.cd /etc/ansible/roles
      02.mkdir {rsync,nfs} ---創(chuàng)建相應角色目錄,可自起名字
      03.mkdir {rsync,nfs}/{vars,tasks,templates,handlers,files} ---創(chuàng)建子目錄灸芳,要和官方文檔上一樣
      04.說明: ├── nfs
      │ ├── files ---保存需要分發(fā)文件目錄
      │ ├── handlers ---保存觸發(fā)器配置文件
      │ ├── tasks ---保存要執(zhí)行的動作信息文件
      │ ├── templates ---保存需要分發(fā)模板文件(與files不同的是涝桅,能支持變量)
      │ └── vars ---保存變量信息文件
      └── rsync
      ├── files ---
      ├── handlers ---
      ├── tasks ---
      ├── templates ---
      └── vars ---
      05.每個子目錄下的編輯文件都是main.yaml除了files目錄
      [root@nginx nfs]# tree
      .
      ├── files
      │ └── exports
      ├── handlers
      │ └── main.yaml
      ├── tasks
      │ └── main.yaml
      ├── templates
      └── vars
      └── main.yaml
      a.tasks/main.yaml: - 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
      - name: 03-boot server
      service: name={{ item }} state=started enabled=yes
      with_items:
      - rpcbind
      - nfs
      b.vars/main.yaml: Data_dir: /data
      c.files/exports:
      /data 192.168.3.0/24(rw,sync)
      d.handlers/main.yaml:
      - name: restart nfs server
      service: name=nfs state=restarted
      e.在roles/ vim main.yaml
      - hosts: nfs_server
      roles:
      - nfs_server
      f.可以把各個動作分成一個yaml
      copy_conf.yaml : - name: 01-copy conf file
      copy: src=exports dest=/etc
      notify: restart nfs server
      create_dir.yaml - name: 02-create data dir
      file: path={{ Data_dir }} state=directory owner=nfsnobody group=nfsnobody
      boot-server.yaml - name: 03-boot server
      service: name={{ item }} state=started enabled=yes
      with_items:
      - rpcbind
      - nfs
      合并:vim site.yaml - import_playbook: copy_conf.yaml
      - import_playbook: create_dir.yaml
      - import_playbook: boot-server.yaml
      g.tasks/main.yaml --> rsync_server的

  • name: 01-install rsyncd
    yum: name=rsync state=installed
  • name: 02-copy configer file
    template: src=rsyncd.conf dest=/etc/ --copy變成了template 主要是加了配置文件的變量
    notify: restart rsync server
  • name: 03-create rsync user
    user: name=rsync create_home=no shell=/sbin/nologin
  • name: 04-create backup dir
    file: dest={{ Data_dir }} state=directory owner=rsync group=rsync
  • name: 05-create password file
    copy: content=rsync_backup:lin19831214 dest=/etc/rsync.password mode=600
  • name: 06-start rsync server
    service: name=rsyncd state=started enabled=yes
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市烙样,隨后出現(xiàn)的幾起案子冯遂,更是在濱河造成了極大的恐慌,老刑警劉巖误阻,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件债蜜,死亡現(xiàn)場離奇詭異,居然都是意外死亡究反,警方通過查閱死者的電腦和手機寻定,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來精耐,“玉大人狼速,你說我怎么就攤上這事∝酝#” “怎么了向胡?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長惊完。 經(jīng)常有香客問我羊壹,道長棺弊,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮电爹,結果婚禮上伟阔,老公的妹妹穿的比我還像新娘良拼。我一直安慰自己僵驰,他們只是感情好,可當我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布控嗜。 她就那樣靜靜地躺著茧彤,像睡著了一般。 火紅的嫁衣襯著肌膚如雪疆栏。 梳的紋絲不亂的頭發(fā)上曾掂,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天惫谤,我揣著相機與錄音,去河邊找鬼遭殉。 笑死石挂,一個胖子當著我的面吹牛,可吹牛的內容都是我干的险污。 我是一名探鬼主播痹愚,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼蛔糯!你這毒婦竟也來了拯腮?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤蚁飒,失蹤者是張志新(化名)和其女友劉穎动壤,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體淮逻,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡琼懊,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了爬早。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片哼丈。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖筛严,靈堂內的尸體忽然破棺而出醉旦,到底是詐尸還是另有隱情,我是刑警寧澤桨啃,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布车胡,位于F島的核電站,受9級特大地震影響照瘾,放射性物質發(fā)生泄漏匈棘。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一析命、第九天 我趴在偏房一處隱蔽的房頂上張望主卫。 院中可真熱鬧,春花似錦碳却、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至筒主,卻和暖如春关噪,著一層夾襖步出監(jiān)牢的瞬間鸟蟹,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工使兔, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留建钥,地道東北人。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓虐沥,卻偏偏與公主長得像熊经,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子欲险,可洞房花燭夜當晚...
    茶點故事閱讀 44,947評論 2 355