Massoud Mazar

Sharing The Knowledge

NAVIGATION - SEARCH

Simple Ansible filter plugin

For a recent project, I used Ansible to configure a bunch of servers. One of the servers needed to be configured as NIS server and part of that configuration is to execute ypinit. When running ypinit script, it asks for some user input and requires users enter CTRL-D when all server names are entered. To automate this through Ansible, I had to somehow pass the CTRL-D (character x04) to stdin, but jinja2 or Ansible did not have a an easy way to do that, so I used a filter plugin.

Here is the code for my plugin:

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible.module_utils._text import to_text

def ascii_char(code):
    return to_text(chr(code))

class FilterModule(object):
	''' Custom Ansible jinja2 filters '''

	def filters(self):
		return {
			'chr': ascii_char
		}

 And here is the tasks related to executing ypinit:

- name: Check for yp directory
  stat: 
    path: "/var/yp/{{ nis_domain }}"
  register: yp_dir

- name: run ypinit
  command: "/usr/lib/yp/ypinit -m"
  args:
    stdin: "{{ 4 | chr }}"
  when: not yp_dir.stat.exists

 

Add comment