2. March 2022 10:36
/
Administrator
/
/
Comments (0)
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
dd0eb913-55d3-4bc2-8374-65de4ecb4cb6|0|.0|96d5b379-7e1d-4dac-a6ba-1e50db561b04