ansible提供给我们了很多常用的fitler,官方文档地址:
http://docs.ansible.com/ansible/latest/playbooks_filters.html
例子:
1 |
<span class="cp">{{</span> <span class="nv">list1</span> <span class="o">|</span> <span class="nf">min</span> <span class="cp">}}</span> |
1 |
<span class="s">"</span><span class="cp">{{</span> <span class="o">[</span><span class="s1">'a'</span><span class="o">,</span><span class="s1">'b'</span><span class="o">,</span><span class="s1">'c'</span><span class="o">]|</span><span class="nf">random</span> <span class="cp">}}</span><span class="s">"</span> |
1 |
<span class="cp">{{</span> <span class="nv">some_variable</span> <span class="o">|</span> <span class="nf">default</span><span class="o">(</span><span class="m">5</span><span class="o">)</span> <span class="cp">}}</span> |
但是有些时候,我们可能需要比较特殊的fitler, 来帮我们处理数据以达到我们的标准,如何创建自己的fitler呢,其实,和module类似,肯定是要线准备一个python函数
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 |
非常简单的函数a_filter,你传递进去一个变量,函数会在后边追加“CRAZY NEW FILETER”(filter class的格式大家记住就好了),存放的路经呢?在你的playbook同级目录创建文件夹filter_plugins,然后将自己的python文件放入即可
1 2 3 |
filter_plugins/ |-- my_filter.py `-- my_filter.pyc |
然后我们创建一个测试用的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:~$ |
完美
再看一个复杂点的例子(多参数)
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 |
对于新的b_fileter,我们使用了多个参数变量(a_variable是输入的需要处理的数据,剩下的按照顺序进行区分)
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 19 20 21 |
ansible-playbook test_fileter.yml PLAY [localhost] ************************************************************************************************************************************************************************************************** TASK [Gathering Facts] ******************************************************************************************************************************************************************************************** Sunday 14 January 2018 22:00:31 -0500 (0:00:00.159) 0:00:00.159 ******** ok: [localhost] TASK [Print a message] ******************************************************************************************************************************************************************************************** Sunday 14 January 2018 22:00:32 -0500 (0:00:01.722) 0:00:01.882 ******** ok: [localhost] => { "msg": "test - the - filters" } PLAY RECAP ******************************************************************************************************************************************************************************************************** localhost : ok=2 changed=0 unreachable=0 failed=0 Sunday 14 January 2018 22:00:32 -0500 (0:00:00.021) 0:00:01.904 ******** =============================================================================== Gathering Facts --------------------------------------------------------- 1.72s Print a message --------------------------------------------------------- 0.02s |
原文地址:http://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