首页 » python » 正文

如何创建自己的ansible filter

ansible提供给我们了很多常用的fitler,官方文档地址:

http://docs.ansible.com/ansible/latest/playbooks_filters.html

例子:

{{ list1 | min }}
"{{ ['a','b','c']|random }}"
{{ some_variable | default(5) }}

但是有些时候,我们可能需要比较特殊的fitler, 来帮我们处理数据以达到我们的标准,如何创建自己的fitler呢,其实,和module类似,肯定是要线准备一个python函数

#!/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文件放入即可

filter_plugins/
|-- my_filter.py
`-- my_filter.pyc

然后我们创建一个测试用的playbook

---
- hosts: localhost
  tasks:
    - name: Print a message
      debug:
        msg: "{{'test'|a_filter}}"

运行结果:

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:~$

完美

再看一个复杂点的例子(多参数)

#!/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是输入的需要处理的数据,剩下的按照顺序进行区分)

---
- hosts: localhost
  tasks:
    - name: Print a message
      debug:
        msg: "{{'test'|another_filter('the','filters')}}"

运行结果:

 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/

发表评论