Ansible Filters

Ansible默认支持Jinja2语言的内置filter,用于转换模板表达式中的数据。

定义变量默认值

如果未定义变量值,则使用default()指定的默认值

1
2
- name: Defaulting Undefined Variables
debug: msg="{{ ansible | default('xxx') }}"

省略参数值

omit参数可用于省略模块中未指定的参数值

1
2
3
4
5
6
7
8
9
10
11
12
  - name: touch files with an optional mode
file: dest={{ item.path }} state=touch mode={{ item.mode|default(omit) }}
with_items:
- path: /root/abc
- path: /root/def
- path: /root/ghi
mode: "0777"

TASK [touch files with an optional mode] *******************************************************
changed: [localhost] => (item={u'path': u'/root/abc'})
changed: [localhost] => (item={u'path': u'/root/def'})
changed: [localhost] => (item={u'path': u'/root/ghi', u'mode': u'0777'})

List

1
2
3
4
5
6
7
8
9
10
11
12
13
- hosts: localhost
gather_facts: false
vars:
list1: [1,3,5,7,9]
list2: [0,2,4,6,8]
list3: [0,1,0,3,1,2,3]
- name: List Filter
debug: msg="Min:{{ list1 | min }} Max:{{ list2 | max }} Random:{{ list2 | random }} Join:{{ list1 | join }} Join(-):{{ list2 | join('-') }}"

TASK [List Filter] ****************************************************
ok: [localhost] => {
"msg": "Min:1 Max:8 Random:8 Join:13579 Join(-):0-2-4-6-8"
}

集合

1
2
3
4
5
6
7
  - name: Set Filter
debug: msg="去重:{{ list3 | unique }} 并集:{{ list1 | union(list2) }} 交集:{{ list1 | intersect(list3) }} 差集:{{ list1 | difference(list3) }} 对称差集:{{ list1 | symmetric_difference(list3) }}"

TASK [Set Filter] ********************************************************
ok: [localhost] => {
"msg": "去重:[0, 1, 3, 2] 并集:[1, 3, 5, 7, 9, 0, 2, 4, 6, 8] 交集:[1, 3] 差集:[5, 7, 9] 对称差集:[5, 7, 9, 0, 2]"
}

IP地址

ipaddr/ipv4/ipv6可用于判断变量值是否为有效的IP地址(需要安装python-netaddr)

1
2
3
4
5
6
7
8
9
10
11
12
13
- hosts: localhost
gather_facts: false
vars:
IP1: 192.168.1.1
IP2: 172.16.1.1/24
tasks:
- name: IP address Filter
debug: msg="{{ IP1 | ipaddr }} {{ IP2 | ipv4 }} {{ IP2 | ipaddr('address') }}"

TASK [IP address Filter] ****************************************************
ok: [localhost] => {
"msg": "192.168.1.1 172.16.1.1/24 172.16.1.1"
}

URL Split

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
- hosts: localhost
gather_facts: false
vars:
URL: http://user:password@www.example.com:8080/dir/index.html?query=term#frament
tasks:
- name: URL Split Filter
debug: msg="{{ URL | urlsplit('scheme') }} {{ URL | urlsplit('username') }} {{ URL | urlsplit('password') }} {{ URL | urlsplit('hostname') }} {{ URL | urlsplit('port') }} {{ URL | urlsplit('netloc') }} {{ URL | urlsplit('path') }} {{ URL | urlsplit('query') }} {{ URL | urlsplit('fragment') }}"
- name: URL Split Filter
debug: msg="{{ URL | urlsplit }}"


TASK [URL Split Filter] *********************************************************
ok: [localhost] => {
"msg": "http user password www.example.com 8080 user:password@www.example.com:8080 /dir/index.html query=term frament"
}

TASK [URL Split Filter] *********************************************************
ok: [localhost] => {
"msg": {
"fragment": "frament",
"hostname": "www.example.com",
"netloc": "user:password@www.example.com:8080",
"password": "password",
"path": "/dir/index.html",
"port": 8080,
"query": "query=term",
"scheme": "http",
"username": "user"
}
}

其他常用的Filter

regex_search可通过正则表达式搜索指定的字符串

1
2
3
4
5
6
7
  - name: regex_search Filter
debug: msg="{{ "Hello Ansible" | regex_search('sible') }}"

TASK [regex_search Filter] *****************************************************************
ok: [localhost] => {
"msg": "sible"
}

strftime

strftime显示当前系统时间

1
2
3
4
5
6
7
  - name: strftime Filter
debug: msg="{{ '%Y-%m-%d %H:%M:%S' | strftime }}"

TASK [strftime Filter] *****************************************************************
ok: [localhost] => {
"msg": "2017-03-21 12:06:27"
}

realpath

realpath显示链接文件的真实路径

1
2
3
4
5
6
7
  - name: realpath Filter
debug: msg="{{ '/etc/grub.conf' | realpath }}"

TASK [realpath Filter] *****************************************************************
ok: [localhost] => {
"msg": "/boot/grub/grub.conf"
}

dirname

dirname通过path显示文件所在的目录

1
2
3
4
5
6
  - name: rirname Filter
debug: msg="{{ '/etc/grub.conf' | dirname }}"

TASK [rirname Filter] *****************************************************************
ok: [localhost] => {
"msg": "/etc"}

basename

basename通过path显示文件名

1
2
3
4
5
6
  - name: basename Filter
debug: msg="{{ '/etc/grub.conf' | basename }}"

TASK [basename Filter] *****************************************************************
ok: [localhost] => {
"msg": "grub.conf"}
坚持原创技术分享,您的支持将鼓励我继续创作!
0%