## 逐台主机执行
升级负载均衡时,需要逐台进行,以实现恢复升级。
### 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