Skip to content

Instantly share code, notes, and snippets.

@StanAngeloff
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StanAngeloff/50c98a44f0aab5af89a6 to your computer and use it in GitHub Desktop.
Save StanAngeloff/50c98a44f0aab5af89a6 to your computer and use it in GitHub Desktop.
The MIT License (MIT)
Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
# (c) PSP UK Group Ltd. <hello@psp-group.co.uk>
#
# For the full copyright and license information,
# please view the LICENSE file that was distributed with this source code.
import math
from jinja2.exceptions import FilterArgumentError
class FilterModule(object):
""" Jinja2 filter for calculating percentages of a value. """
def percentage(self, value, percentage, precision=0, method='common', as_int=True, default=0):
""" Calculate a percentage of a value, see Jinja2 round filter. """
if not method in ('common', 'ceil', 'floor'):
raise FilterArgumentError('method must be common, ceil or floor')
if percentage < 1:
percentage = (float(percentage) * 100.00)
try:
result = ((float(value) / 100.0) * float(percentage))
except (TypeError, ValueError):
return default
if method == 'common':
result = round(result, precision)
else:
func = getattr(math, method)
result = func(result * (10 ** precision)) / (10 ** precision)
if as_int:
try:
result = int(result)
except (TypeError, ValueError):
# this quirk is necessary so that "42.23"|int gives 42.
try:
result = int(float(result))
except (TypeError, ValueError):
return default
return result
def filters(self):
""" Export filters from this class to Jinja2/Ansible. """
return {
'percentage': self.percentage,
}
$ export ANSIBLE_FILTER_PLUGINS="./path_to_filter_plugins/"
$ ansible-playbook app.yml --inventory-file=inventory.ini [..]
# {{{ MySQL

mysql_max_memory: 8192

# MySQL, recommendations by MySQLTuner.
#
mysql_query_cache_size: "{{ mysql_max_memory | percentage(20) }}M"
mysql_innodb_buffer_pool_size: "{{ mysql_max_memory | percentage(80) }}M"

# }}}

# }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment