Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ansible custom filters demo
#place the file in your ansible playbook director under filter_plugins
#/home/user/my_playbook.yml
#/home/user/filter_plugins/my_filters.py
#!/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
---
- hosts: localhost
connection: local
tasks:
- name: Print a message
debug:
msg: "{{'test'|a_filter}}"
user@ansibletest:~$ 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
#!/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
---
- hosts: localhost
connection: local
tasks:
- name: Print a message
debug:
msg: "{{'test'|another_filter('the','filters')}}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment