Skip to content

Latest commit

 

History

History
273 lines (232 loc) · 15.4 KB

README.md

File metadata and controls

273 lines (232 loc) · 15.4 KB

Ansible Filters

Setup

Check the filters out to a directory of your choice:

$ hub clone lxhunter/ansible-filter-plugins /usr/local/src/ansible-filter-plugins
# or
$ git clone https://github.com/lxhunter/ansible-filter-plugins.git /usr/local/src/ansible-filter-plugins

Pro-Tip: I use fresh for this:

$ fresh lxhunter/ansible-filter-plugins . --file=/usr/local/src/ansible-filter-plugins

The easiest way to make use of the libaries is to checkout this project somewhere on your ansible host, and configure ansible.cfg to point the filters directory variable to the checkout directory:

$ filter_plugins = /usr/local/src/ansible-filter-plugins

More details can be found here: http://docs.ansible.com/ansible/developing_plugins.html#filter-plugins

Requirements

For the ip address utilites you need the python netaddr package:

$ pip install netaddr

For pprint you will need the python pretty package:

$ pip install pretty

Testing

$ python -m unittest -v string_utils
# or
$ nodemon -e 'py' --exec "python -m unittest -v string_utils"
# or
$ molecule test

Overview

Collection Utilities

  • batch - A filter that batches items.
  • [chop] - see batch
  • [decapitalize]
  • dictsort - Sort a dict and yield (key, value) pairs.
  • difference - To get the difference of 2 lists.
  • first - Return the first item of a sequence.
  • groupby - Group a sequence of objects by a common attribute.
  • intersect - Get the intersection of 2 lists
  • join - Return a string which is the concatenation of the strings in the sequence.
  • last - Return the last item of a sequence.
  • length - Return the number of items of a sequence or mapping.
  • [lines]
  • map - Applies a filter on a sequence of objects or looks up an attribute.
  • [naturalCmp] - see either sort or dictsort
  • random - Return a random item from the sequence.
  • reject - Filters a sequence of objects by applying a test to the object and rejecting the ones with the test succeeding.
  • reverse - Reverse the object or return an iterator that iterates over it the other way round.
  • select - Filters a sequence of objects by applying a test to the object and only selecting the ones with the test succeeding.
  • shuffle - Randomize an existing list, giving a different order every invocation.
  • slice - Filters a sequence of objects by applying a test to the object and only selecting the ones with the test succeeding.
  • sort - Sort an iterable.
  • symmetric_difference - To get the symmetric difference of 2 lists
  • union - Get a union of two lists
  • unique - Get a unique set from a list

Debug Utilities

Hashing / encoding / ID generation Utilities

  • b64decode - Decode a Base64 encoded string.
  • b64encode - Encode a string use Base64.
  • checksum - Get a checksum for a string
  • from_json - Reading in some JSON formatted data
  • from_yaml - Reading in YAML formatted data
  • hash - Get the hash (md5, sha1, sha224, sha256, sha384, sha512) of a string
  • password_hash - Get a password hash (md5, sha256, sha512) for a string
  • to_json - Convert value into JSON
  • to_uuid - Create a UUID from a string
  • to_yaml - Convert value into YAML

IP Utilities

  • hwaddr - Check if a given string is a MAC address or convert it between various formats.
  • ipaddr - Returns the input value if a query is True, and False if query is False.
  • ipsubnet - Can be used to manipulate network subnets in several ways.
  • ipv4 - To test if a string is a valid IPv4 address.
  • ipv6 - To test if a string is a valid ipv6 address.
  • ipwrap - Some configuration files require IPv6 addresses to be “wrapped” in square brackets ([ ]).

Number / Math Utilities

  • abs - Return the absolute value of the argument.
  • float - Convert the value into a floating point number.
  • int - Convert the value into an integer.
  • log - Get the logarithm
  • pow - Get the power of
  • root - Square root or root
  • round - Round the number to a given precision.
  • sum - Returns the sum of a sequence.

Object Utilities

  • attr - Get an attribute of an object.
  • default - Set a default value
  • rejectattr - Filters a sequence of objects by applying a test to an attribute of an object or the attribute and rejecting the ones with the test succeeding.
  • selectattr - Slice an iterator and return a list of lists containing those items.

Path Utilities

  • basename - Return the base name of pathname path.
  • dirname - Return the directory name of pathname path.
  • expanduser - Expand a path containing a tilde (~) character
  • realpath - Get the real path of a link
  • relpath - Get the relative path of a link, from a start point.
  • splitext - To get the root and extension of a path or filename.

String Utilities

  • camelize - Converts underscored or dasherized string to a camelized one.
  • capitalize - Capitalize a String.
  • center - Centers the value in a field of a given width.
  • [chars] - See list
  • classify - Converts string to camelized class name. First letter is always upper case.
  • clean - Trim and replace multiple spaces with a single space.
  • count - Counts the number of times needle is in haystack.
  • dasherize - Converts a underscored or camelized string into an dasherized one.
  • decapitalize - Converts first letter of the string to lowercase.
  • dedent - Converts first letter of the string to lowercase.
  • ends_with - Dedent unnecessary indentation.
  • escape - Convert the chars to HTML-safe sequences.
  • escape_html - Converts HTML special characters to their entity equivalents.
  • filesizeformat - Format file sizes into ‘human-readable’
  • forceescape - Enforce HTML escaping.
  • format - Apply python string formatting on an object.
  • humanize - Converts an underscored, camelized, or dasherized string into a humanized one.
  • indent - Indent a string.
  • insert - Insert word in string at the defined position.
  • list - Convert the value into a list.
  • lower - Convert a value to lowercase.
  • lpad - Return the string left justified in a string of length width.
  • [lrpad] - See center
  • ltrim - Return a copy of the string with leading characters removed.
  • [predecessor] - do not know how to implement this
  • [prune] - see truncate
  • quote - Add quotes for shell usage
  • regex_escape - Escape special characters within a regex
  • regex_replace - Replace text in a string with a regex
  • repeat - Repeats a string count times, can be seperated by separator.
  • replace - Search for needle in haystack and replace it with substitute.
  • [replaceAll] - See replace
  • reverse - Reverse the object or return an iterator that iterates over it the other way round.
  • rpad - Return the string right justified in a string of length width.
  • rtrim - Return a copy of the string with trailing characters removed.
  • safe - Make all potentionally dangerous chars safe. - safety is a illusion, so beware!
  • splice - Return the string right justified in a string of length width.
  • [sprintf] - see format
  • starts_with - Checks whether the string begins with the needle at position (default: 0).
  • string - Make a string unicode.
  • striptags - Strip SGML/XML tags and replace adjacent whitespace by one space.
  • strLeft
  • [strLeftBack]
  • [strRight]
  • [strRightBack]
  • [successor]
  • [surround]
  • [swap_case]
  • title - Return a titlecased version of the value.
  • [toSentence]
  • [toSentenceSerial]
  • [transliterate]
  • trim - Strip leading and trailing whitespace.
  • truncate - Return a truncated copy of the string.
  • [underscore]
  • [unescapeHTML]
  • [unquote]
  • upper - Convert a value to uppercase.
  • urlencode - Escape strings for use in URLs.
  • urlize - Converts URLs in plain text into clickable links.
  • wordcount - Count the words in that string.
  • [words]
  • wordwrap - Return a copy of the string passed to the filter wrapped after n characters.
  • xmlattr - Create an SGML/XML attribute string based on the items in a dict.

Tests

  • bool - Check if the value is a boolean
  • callable - Return whether the object is callable
  • defined - Return true if the variable is defined
  • divisibleby - Check if a variable is divisible by a number.
  • equalto - Check if an object has the same value as another object.
  • escaped - Check if the value is escaped.
  • even - Return true if the variable is even.
  • includes - Tests if string contains a substring.
  • [is_camel_case]
  • [is_credit_card]
  • [is_email]
  • [is_ip]
  • [is_snake_case]
  • [is_url]
  • [is_uuid]
  • [isBlank]
  • isnan - To see if something is actually a number
  • iterable - Check if it’s possible to iterate over an object.
  • lower - Return true if the variable is lowercased.
  • mapping - Return true if the object is a mapping (dict etc.).
  • none - Return true if the variable is none.
  • number - Return true if the variable is a number.
  • odd - Return true if the variable is odd.
  • sameas - Check if an object points to the same memory address than another object.
  • sequence - Return true if the variable is a sequence.
  • string - Return true if the object is a string.
  • ternary - Use one value on true and another on false
  • undefined - Like defined() but the other way round.
  • upper - Return true if the variable is uppercased.
  • version_compare - To compare a version number

Type Conversion Utilities

  • [toBoolean]
  • [toNumber]

Credit

Builtin Jinja functions:

Builtin Ansible functions:

Quote

Look deep into nature, and then you will understand everything better.

  • Albert Einstein

Contribute

Tutorial

License and Author

Author:: Alexander Jäger

Copyright 2018

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.