1、playbookspython
若是用模塊形式通常有冪等性,若是用shell或者command沒有冪等性shell
playbooks至關因而shell腳本,能夠把要執行的任務寫到文件當中,一次執行,方便調用vim
tasks:一個task至關因而一個play服務器
varibles:變量,必定定義,多處調用ssh
template:模板,能夠區分不一樣主機的特色測試
handlers:觸發器,依賴於前一個任務,前一個任務若是執行改變,那麼就會觸發handlersui
2、yaml介紹及格式spa
yaml是一個可讀性高的用來表達資料序列的格式,yaml參考了其餘多種語言,包括:xml,c語言,python,perl以及電子郵件格式RFC2822等,ClarkEvans在2001年在首次發表了這種語言。調試
特色:rest
(1)yaml的可讀性好
(2)yaml和腳本語言的交互性好
(3)yaml使用實現語言的數據類型
(4)yaml有一個一致的信息模型
(5)yaml易於實現
(6)yaml能夠基於流程來處理
(7)yaml表達能力強,擴展性好
編寫說明:
(1)縮進:yaml 的縮進要求比較嚴格。必定不能使用tab鍵
(2)冒號:每一個冒號後面必定要有一個空格
注意:1. 以冒號結尾不須要空格
2. 表示文件路徑的模版能夠不須要空格
(3)短橫線-:想要表示列表項,使用一個短橫槓加一個空格。多個項使用一樣的縮進級別做爲同一個列表的一部分
注意:
1. 嚴格控制空格編寫劇本的時候
2. 劇本編寫不支持tab
書寫規範注意點:
一、腳本名:***.yml 【不是.yml也行】
二、註釋: 「#」
三、tasks: (後不能加任何字符-能夠加註釋#)
四、- name: 一個 - name: 下不能有兩條功能(行)
.每一個-和:即冒號以後要有一個空格,task除外。
劇本格式:
--- ### 劇本的開頭,能夠不寫
- hosts: all <- 處理全部服務器,找到全部服務器; -(空格)hosts:(空格)all
tasks: <- 劇本所要乾的事情; (空格)(空格)task:
- command: (空格)(空格)-(空格)模塊名稱:(空格)模塊中對應的功能
測試劇本命令後面能夠跟多個-v進行調試檢查
經常使用命令:
一、對劇本語法檢測
ansible-playbook --syntax-check /root/ansible/httpd.yaml
二、-C模擬執行劇本
ansible-playbook -C /root/ansible/httpd.yaml
三、執行劇本
ansible-playbook /root/ansible/httpd.yaml
3、ansible劇本實例
實例一:基礎(編寫一個rehttpd.yaml劇本)
第一步:編寫劇本
[root@ren5 ~]# vim rehttpd.yaml
1 - hosts: test_ren1 2 remote_user: root 3 tasks: 4 - name: copy httpd.conf 5 copy: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf 6 - name: restart httpd 7 service: name=httpd state=restarted
第二步:語法檢測
[root@ren5 ~]# ansible-playbook --syntax-check rehttpd.yaml
playbook: rehttpd.yaml
第三步:模擬執行劇本
[root@ren5 ~]# ansible-playbook -C rehttpd.yaml
第四步:真正執行劇本
[root@ren5 ~]# ansible-playbook rehttpd.yaml
第五步:檢查是否已經啓動成功
[root@ren5 ~]# ansible test_ren1 -m shell -a "ss -tnl |grep 80"
實例二:打標籤
-t:執行指定tag標籤任務
--skip-tags:執行--skip-tags之外的任務
執行:ansible-playbook xxx.yaml -t TAG
ansible-playbook xxx.yaml --skip-tags TAG
定義帶tag標籤的yaml文件:
1 - hosts: test_ren1 2 remote_user: root 3 tasks: 4 - name: "touch 123.txt" 5 shell: echo {{ ansible_all_ipv4_addresses }} > 123.txt 6 tags: 7 - tag1 8 - name: "date" 9 shell: date >> 123.txt 10 tags: 11 - tag2 12 - tag3
[root@ren5 ~]# ansible-playbook tag.yaml -t tag1 #只執行tag1標籤的任務
[root@ren5 ~]# ansible all -a "cat 123.txt" #查看文件內容
實例三:include
include:若是有多個任務要執行,寫在一個yaml文件裏就顯得太臃腫了,並且閱讀性也比較低,這個時候能夠用include關鍵字把多個任務聯合到1個yaml文件裏,方便調用
好比有2個文件,a.yaml和b.yaml,那麼咱們能夠把a.yaml,b.yaml聯合到一個exec.yaml文件中執行
a.yaml的內容以下:
1 - name: "touch abc.txt" 2 shell: echo 'hello' >> abc.txt
b.yaml的內容以下:
1 - name: "touch abc.txt" 2 shell: echo 'world' >> abc.txt
exec.yaml的內容以下:
1 - hosts: test_ren1 2 remote_user: root 3 tasks: 4 - include_tasks: a.yaml 5 - include_tasks: b.yaml
[root@ren5 ~]# ansible-playbook --syntax-check exec.yaml
[root@ren5 ~]# ansible-playbook exec.yaml
[root@ren5 ~]# ansible test_ren1 -m shell -a "cat abc.txt"
實例四:劇本中使用變量
變量的引用:{{變量名}}
1、自定義變量的實現方式:
第一種:直接寫在yaml文件中,在文件中聲明變量(聲明變量只須要聲明1次,定義變量能夠定義屢次)
vars和tasks同級,並要寫在tasks前面
vars:
- 變量名: 變量值
vars:
- file: httpd.conf #file就是一個變量
1 - hosts: 192.168.11.4 2 remote_user: root 3 vars: 4 - packages: tree 5 tasks: 6 - name: yum install tree 7 yum: name={{ packages }} state=present
第二種:在inventory file文件中定義
vim /etc/ansible/hosts #定義主機組
[test_ren1:vars]
file=httpd.conf #變量名=變量值
packages=tree #file和packages是兩個變量
第三種:執行playbook文件時候給與變量 --extra-vars
ansible-playbook test.yaml --extra-vars "touch_file=test.txt" #--extra-vars能夠出現一次或者屢次
2、註冊變量
register註冊變量:把date命令輸出的結果賦予給date_output
1 - hosts: 192.168.254.10 2 remote_user: root 3 tasks: 4 - name: get date 5 command: date 6 register: date_output 7 - name: echo date_output 8 shell: "echo {{date_output.stdout}}>/tmp/a.txt"
1 --- 2 - hosts: test1 3 remote_user: root 4 tasks: 5 - name: get users_info 6 command: "tail /etc/passwd" 7 register: users_info 8 - name: echo users_info 9 shell: "echo '{{ users_info.stdout }}' > /root/ab.txt"
#若變量值(變量內容)包含較多內容時,需用引號引發來
實例5、劇本中使用判斷
劇本中可使用when來進行判斷
when條件語句:能夠根據setup顯示出客戶端信息爲依據來判斷
一、根據系統信息判斷
1 --- 2 - hosts: test_ren1 3 remote_user: root 4 tasks: 5 - name: touch file 6 shell: "touch /tmp/a.txt" 7 when: ansible_distribution=='CentOS' and ansible_distribution_major_version=='8'
二、根據域名判斷
第一步:查看主機的域名(ansible_fqdn是一個變量)
[root@ren5 ~]# ansible 192.168.11.4 -m setup |grep ansible_fqdn
"ansible_fqdn": "ren4",
第二步:編寫劇本
在編寫劇本的時候ren4即主機名須要加上雙引號或者單引號,不然會報錯
1 --- 2 - hosts: all 3 tasks: 4 - name: useradd liqin 5 user: name=liqin uid=1210 system=yes 6 when: ansible_fqdn == "ren4"
第三步:語法檢測
[root@ren5 ~]# ansible-playbook --syntax-check name.yaml
第四步:模擬執行
[root@ren5 ~]# ansible-playbook -C name.yaml
實例六:劇本中使用循環(迭代)
劇本中的循環使用with_items,使用item引用變量(item不可變)
變量的引用格式是 {{ item }}
第一種:
1 --- 2 - hosts: 192.168.11.6 3 remote_user: root 4 tasks: 5 - name: add many users 6 user: name={{ item }} state=present 7 with_items: 8 - user01 9 - user02 10 - uesr11 11 - user12
第二種:Key-Value,鍵值對的形式實現了傳多個變量的需求
1 --- 2 - hosts: 192.168.11.6 3 remote_user: root 4 tasks: 5 - name: add many users 6 user: name={{ item.name }} groups={{ item.groups }} state=present 7 with_items: 8 - { name: 'testuser1', groups: 'root' } 9 - { name: 'testuser2', groups: 'ren6' }
實例七:異常處理
ignore_errors:若是任務出錯,直接跳過,不會影響其餘任務
1 --- 2 - hosts: 192.168.11.4 3 remote_user: root 4 tasks: 5 - name: error 6 command: make a.txt 7 ignore_errors: yes
實例八:使用處理器(觸發器)notify
handlers:若是執行的任務被改變那麼會觸發handlers的任務,notify下面的 - 引導的列表能夠爲一個或者多個,相似於流程控制語句
劇本說明:
- hosts: all 指定主機組,能夠理解爲這個最大,頂個寫
tasks: 指定下面一系列的動做,這個是第二,須要有兩個空格
- name: 指定名稱,排行第三,須要有三到四個空格
yum: 模塊名 排行第四,須要有四到五個空格
notify:
- name: 和handlers下面的name名稱對應
handlers:指定處理器(觸發器),排行第二,須要與tasks對齊,必須寫在最後
1 --- 2 - hosts: test_ren1 3 remote_user: root 4 tasks: 5 - name: copy httpd.conf 6 copy: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf 7 notify: 8 - restarted httpd service 9 handlers: 10 - name: restarted httpd service 11 service: name=httpd state=restarted
執行過程當中間有一步RUNNING HANDLER,說明已經觸發了處理器進行了重啓操做
1 --- 2 - hosts: test1 3 remote_user: root 4 tasks: 5 - name: get users_info 6 command: "tail /etc/passwd" 7 register: users_info 8 notify: #執行屢次獲報錯,可以使用觸發器或者ignore_errors 9 - echo users_info 10 handlers: 11 - name: echo users_info 12 shell: "echo '{{ users_info.stdout }}' > /root/a.txt"
實例九:模板拷貝(templates)
template,用來區分不一樣客戶端上的特性
1.copy模塊替換成template(template自己就有copy的做用,也能夠識別變量)
vim test.yaml
1 --- 2 - hosts: test1 3 remote_user: root 4 tasks: 5 - name: copy httpd.conf 6 template: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf 7 notify: 8 - restarted httpd service 9 handlers: 10 - name: restarted httpd service 11 service: name=httpd state=restarted
2.vim httpd.conf編輯要拷貝的文件,把不一樣的地方定義成變量形式{{變量名}}
vim /etc/httpd/conf/httpd.conf
1 42 Listen {{ port }}
3.vim /etc/ansible/hosts 在主機後面定義變量的值:變量名=變量值
1 [test1] 2 192.168.11.5 ansible_ssh_user=root ansible_ssh_pass=root ansible_ssh_port=22 port=81 3 192.168.11.4 ansible_ssh_user=root ansible_ssh_pass=root ansible_ssh_port=22 port=82
ansible-playbook test.yaml,執行後查看結果
[root@ren6 ~]# ansible all -m shell -a "ss -tnl" 192.168.11.4 | CHANGED | rc=0 >> State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::82 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* 192.168.11.5 | CHANGED | rc=0 >> State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::81 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::*