Playbook
一、語法特性以下:
(1)"---"首行頂格開始
(2)#號註釋
(3)縮進統一,不一樣的縮進表明不一樣的級別,縮進要對齊,空格和tab不能混用
(4)區別大小寫,鍵值對k/v的值也大小寫敏感
(5)k/v的值同行使用":"分開,換行寫需以"-"分隔
二、格式
---
- name: test
hosts: test
gather_facts: no
vars:
http: 8080
https: 1443
vars_files:
- vars/server_vars.yml
remote_user: root
sudo: yes
tasks:
- name: test1
copy: src=/opt/apps/web/* dest=/opt/apps/web/
notify:
- restart apache
- name: test2
yum: name={{ item }} state=present
with_items:
- httpd
- httpd-devel
- name: test3
shell: "echo $TEST"
environment:
- TEST: abc
handlers:
- name: restart apache
service: name=httpd state=restarted
說明:
hosts:test #表示test組的節點主機
gather_facts: no #跳過收集facts信息,默認是yes
vars: #定義變量,引用變量{{vars}}
vars_files: #使用文件定義變量,文件中也是用k/v形式定義變量
remote_user: root #遠程使用root用戶
sudo:yes #使用sudo
tasks: #指定一系列將要運行的任務
name: test1 #test1任務,copy模塊
notify: #與下面handlers對應,上一步copy以後文件發生改變,則經過name匹配handlers中的任務,這裏匹配restart appche的service任務
name: test2 #test2任務,yum模塊
with_items: #定義列表變量,直接用{{item}}引用。該關鍵字也能夠用來定義數組。
with_dict: #定義字典
三、正式運行Playbook以前,能夠--check或-C來檢測,輸出的結果和實際執行如出一轍,但不會有實際影響。
四、ansible-playbook
-i 指定inventory文件
-v 詳細輸出,-vvv更詳細,-vvvv更更詳細
-f 併發數
-e 定義Playbook中使用的變量,格式"key=value,key=value"
--remote-user #遠程用戶
--ask-pass #遠程用戶密碼
--sudo #使用sudo
--sudo-user #sudo的用戶,默認root
--ask-sudo-pass #sudo密碼
擴展:
(1)handlers:見test1
(2)environment:爲某個play設置單獨的環境變量,例子見P94-95
(3)delegate_to:任務委派
例:- name: 123
shell: "echo $PATH>/test/1"
delegate_to: 192.168.2.30
把shell命令委派給192.168.2.30節點執行,其餘hosts中指定的節點不執行。
(4)register:註冊變量
將操做結果,包括stdout和stderr,保存到變量中,再根據變量的內容決定下一步,這個保存操做結果的變量就是註冊變量。
例:- shell: ***
register: result
使用result.stdout和result.stderr讀取執行結果的標準輸出和標準錯誤。
(5)vars_prompt: 交互式提示
例:
---
- hosts: all
vars_prompt:
- name: share_user
prompt: "username?"
- name: share_pass
prompt: "password"
private: yes
經常使用選項:
private: yes #用戶輸入不可見
default #設置默認值
confirm: yes #要求輸入兩次
五、流程控制
(1)when條件判斷 P109
例1:第一步執行的返回值中有'ready'時,再執行下一步操做。
- command: my-app --status
register: myapp_result
- command: do-something
when: "'ready' in myapp_result"
例2:stat模塊查看文件信息,若是文件沒建立,則執行command
- stat: path=/etc/sysctl.conf
register: stat_result
- command: touch /etc/sysctl.conf
when: stat_result.stat.exists == false
(2)changed_when和failed_when P110-111
(3)wait_for模塊,任務暫停 P112
六、循環
原文:http://xdays.me/ansible狀態管理.html
(1)標準遍歷
用with_items能夠遍歷一個列表,注意這裏只會遍歷一層。示例以下:
- name: add several users
user: name={{ item }} state=present groups=wheel
with_items:
- testuser1
- testuser2
(2)嵌套遍歷
用with_nested能夠遍歷一個列表,注意這裏會遍歷多層,直到最內層。示例以下:
- name: give users access to multiple databases
mysql_user: name={{ item[0] }} priv={{ item[1] }}.*:ALL append_privs=yes password=foo
with_nested:
- [ 'alice', 'bob', 'eve' ]
- [ 'clientdb', 'employeedb', 'providerdb' ]
(3)遍歷字典
用with_dict能夠遍歷一個字典,用key和value來表示。示例以下:
#變量文件
---
users:
alice:
name: Alice Appleworth
telephone: 123-456-7890
bob:
name: Bob Bananarama
telephone: 987-654-3210
#playbook文件
tasks:
- name: Print phone records
debug: msg="User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})"
with_dict: users
(4)文件通配符循環
用with_fileglob能夠獲取本地文件列表。示例以下:
# copy each file over that matches the given pattern
- copy: src={{ item }} dest=/etc/fooapp/ owner=root mode=600
with_fileglob:
- /playbooks/files/fooapp/*
(5)對齊的列表
用with_together能夠達到相似Python裏的zip函數的功能。示例以下:
#變量文件:
---
alpha: [ 'a', 'b', 'c', 'd' ]
numbers: [ 1, 2, 3, 4 ]
#playbook文件
tasks:
- debug: msg="{{ item.0 }} and {{ item.1 }}"
with_together:
- alpha
- numbers
(6)循環一個task的輸出
with_lines指令後跟一個命令,ansible會遍歷命令的輸出。示例以下:
- name: Example of looping over a command result
shell: /usr/bin/frobnicate {{ item }}
with_lines: /usr/bin/frobnications_per_host --param {{ inventory_hostname }}
七、Tags標籤 #P113-114
標籤功能能夠給Roles、文件、單獨的任務甚至整個Playbook打上標籤,而後利用這些標籤來指定要運行或不運行Playbook中的個別任務。
例:
vim test.yml
---
- hosts: test
tags: deploy #給整個Playbook打一個標籤
tasks:
- name: test_tags
shell: ***
tags: #爲任務打標籤
- test1
- test2
- include: foo.yml
tags: foo
ansible-playbook test.yml --tags "test1" #只執行test_tags任務
ansible-playbook test.yml --skip-tags "test2" #跳過test_tags任務
寫法格式:
tags: ['one','two']
tags:
- one
- twohtml