ansible playbook 一个非常强大的功能就是允许我们自定义filter_plugins,这个filter_plugin是什么呢?
就是我们一般看到的
{{item|max}}
其实,后边的这个 max就是一个函数,我们可以定义自己的函数,例如:
1 2 3 4 |
|-- filter_plugins | |-- zhiming_filter.py | `-- zhiming_filter.pyc `-- main.yml |
目录结构就是我们在我们的playbook下创建filter_plugins文件夹,然后写一个python文件,里边定义一些方法:
例如我的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#!/usr/bin/env python # coding=utf-8 ''' Created on Apr 13, 2016 @author: zhizhang ''' import re class FilterModule(object): @staticmethod def get_ebs_id(data): patten = re.compile(r'xiaoming-[a-zA-Z0-9]{8}') if patten.search(data): return patten.search(data).group() return None def filters(self): ''' returns a mapping of filters to methods ''' return { "get_ebs_id": self.get_ebs_id, } |
这个函数的功能事,例如我们有一串字符串
xiaoming-s7d7f7s7-we32sdfa-asdfasdf
我们只想取到xiaoming-s7d7f7s7
我们直接在playbook中使用我的方法就可以了,例如
– debug: msg=” the id infof is {{abc|get_ebs_id}}”
然后就可以了
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021
骑驴 2016/05/01 16:36
不错