## 逐臺主機執行
升級負載均衡時,需要逐臺進行,以實現恢復升級。
### serial語句
一般來說,當task失敗時,ansible會停止執行失敗的那臺主機上的任務。
~~~
- name: upgrade packages on servers behind load balance
hosts: localhost
serial: 1
max_fail_percentage: 25
tasks:
......
~~~
> 這里超過25%失敗
## 只執行一次
比如數據庫備份,在多臺主機只只需要選出一臺進行備份。
### run_once語句
~~~
- name:run database migrations
command: /opt/run_migrations
run_once: true
~~~
# 過濾
## default 過濾器
指定變量的默認值
~~~
“HOST”: "{{ database_host | default('localhost') }}"
~~~
## 文件路徑過濾器
處理包含控制主機文件系統的文件路徑。
~~~
basename # 文件路徑中的文件名
dirname # 文件路徑中的目錄
expanduser # 將文件路徑中 ~ 替換為文件路徑
realpath # 處理符號鏈接后的文件實際路徑
~~~
## order 主機執行順序
```
- hosts: all
order: sorted
gather_facts: False
tasks:
- debug:
var: inventory_hostname
Possible values for order are:
inventory:
The default. The order is ‘as provided’ by the inventory
reverse_inventory:
As the name implies, this reverses the order ‘as provided’ by the inventory
sorted:
Hosts are alphabetically sorted by name
reverse_sorted:
Hosts are sorted by name in reverse alphabetical order
shuffle:
Hosts are randomly ordered each run
```
## 改變輸出結果
```
tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand || /bin/true
```
## notify listener
```
As of Ansible 2.2, handlers can also “listen” to generic topics, and tasks can notify those topics as follows:
handlers:
- name: restart memcached
service:
name: memcached
state: restarted
listen: "restart web services"
- name: restart apache
service:
name: apache
state:restarted
listen: "restart web services"
tasks:
- name: restart everything
command: echo "this task will restart the web services"
notify: "restart web services"
```
## handlers
```
handlers notified 在 pre_tasks, tasks, and post_tasks 每個部分執行完自動刷新 handlers
handlers notified 在 roles 每個執行完自動刷新, 在每個 task執行之前.
下面的 meta:flush_handlers 可以立即刷新 handlers
tasks:
- shell: some tasks go here
- meta: flush_handlers
- shell: some other tasks
```
## 在模板中忽略未定義的變量報錯
```
{{ variable | mandatory }}
```
## 判斷變量類型
```
{{ myvar | type_debug }}
```
## to_uuid
```
{{ hostname | to_uuid }}
```
## 路徑處理和編碼
```
{{ path | realpath }}
To get the relative path of a link, from a start point (new in version 1.7):
{{ path | relpath('/etc') }}
To get the root and extension of a path or filename (new in version 2.0):
# with path == 'nginx.conf' the return would be ('nginx', '.conf')
{{ path | splitext }}
To work with Base64 encoded strings:
{{ encoded | b64decode }}
{{ decoded | b64encode }}
```
## [自定義過濾插件][1]
[1]:https://coding.net/u/echohiyang/p/ansible_plugins/git
- 目錄
- ansible基礎
- ansible簡介
- ansible安裝和測試
- ansible配置文件
- 常用命令
- yaml在ansible中的用法
- inventory
- 變量與facts
- when語句
- handler模塊
- 大雜燴
- ansible模塊
- assert 模塊
- copy模塊
- cron模塊
- debug模塊
- django_manage模塊
- file模塊
- filesystem模塊
- git模塊
- hostname模塊
- lineinfile模塊
- mount模塊
- mysql_user模塊
- mysql_db模塊
- pip模塊
- selinux
- setup模塊
- shell 和 command 模塊
- stat模塊
- supervisorctl
- systemd
- timezone
- unarchive模塊
- user模塊
- wait_for
- yum和service模塊
- 其他模塊或者方法
- setup模塊
- url模塊
- slack 模塊
- pause 模塊
- 其他
- 報錯處理
- playbooks
- 復雜的playbook
- 循環
- roles
- YAML
- jinja2