有时候我们写自己的playbook的时候变量处理真的是特别让人头大,有些变量的操作在python中就很简单,但是放到ansible playbook中就会很麻烦,所以,我们今天来看一下如何编写自己的ansible filter来帮助我们处理一下变量
直接看例子吧:
1 2 3 4 5 6 7 8 9 10 11 |
#!/usr/bin/python class FilterModule(object): def filters(self): return { 'a_filter': self.a_filter, 'another_filter': self.b_filter } def a_filter(self, a_variable): a_new_variable = a_variable + ' CRAZY NEW FILTER' return a_new_variable |
我们来分析一下代码:
定义了一个filter叫a_filter,当让我们有变量传入的时候,我们在原来变量的基础上加上’CRAZY NEW FILTER’ 生成一个新的字符串然后返回
其实这样我们就完成了一个基本的filter的例子
我们来实际使用一下,这里要说明一下,我们自己写的filter要放入和你的playbook同级的filter_plugins/下
例如:
/home/user/my_playbook.yml
/home/user/filter_plugins/my_filters.py
这里python文件名大家随意,核心内容在内容里边
我们创建一个测试用的playbook
1 2 3 4 5 6 |
--- - hosts: localhost tasks: - name: Print a message debug: msg: "{{'test'|a_filter}}" |
我们看一下运行结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
ansible-playbook my_playbook.yml PLAY *************************************************************************** TASK [setup] ******************************************************************* ok: [localhost] TASK [Print a message] ********************************************************* ok: [localhost] => { "msg": "test CRAZY NEW FILTER" } PLAY RECAP ********************************************************************* localhost : ok=2 changed=0 unreachable=0 failed=0 user@ansibletest:~$ |
我们发现我们的变量’test’在调用了我们的filter: a_filter以后,变量变成了:test CRAZY NEW FILTER
我们来增加另外一个简单的filter b_filter在原来的python文件中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#!/usr/bin/python class FilterModule(object): def filters(self): return { 'a_filter': self.a_filter, 'another_filter': self.b_filter } def a_filter(self, a_variable): a_new_variable = a_variable + ' CRAZY NEW FILTER' return a_new_variable def b_filter(self, a_variable, another_variable, yet_another_variable): a_new_variable = a_variable + ' - ' + another_variable + ' - ' + yet_another_variable return a_new_variable |
注意,我们调用filter的时候需要使用:another_filter这个是在return函数中的定义,相当于大名和小名
注意:我们的a_variable就是我们调用filter的变量本身,所以我们只需要传递后两个参数即可
再次测试
1 2 3 4 5 6 |
--- - hosts: localhost tasks: - name: Print a message debug: msg: "{{'test'|another_filter('the','filters')}}" |
结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
ansible-playbook test.yml [WARNING]: No inventory was parsed, only implicit localhost is available [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [localhost] ************************************************************************************************************************************** TASK [Gathering Facts] ******************************************************************************************************************************** ok: [localhost] TASK [Print a message] ******************************************************************************************************************************** ok: [localhost] => { "msg": "test - the - filters" } PLAY RECAP ******************************************************************************************************************************************** localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
参考: https://www.dasblinkenlichten.com/creating-ansible-filter-plugins
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