Created
March 14, 2017 12:31
-
-
Save tuxfight3r/37048ba536575277f5f4d26813d69489 to your computer and use it in GitHub Desktop.
ansible custom filters demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: localhost | |
connection: local | |
tasks: | |
- name: Print a message | |
debug: | |
msg: "{{'test'|a_filter}}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- 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