Installing NIS package with ansible

Hi, I’m trying to install NIS package using ansible apt. When package is installed manually, it asks to put name of the cluster. I want to do this automatically, so I found it is saved in file /etc/defaultdomain.
My workaround:

- name: install nis package and change domain to CLUSTER_NAME
        - name: install
            name: nis
        - name: change domain
            path: /etc/defaultdomain
            regexp: '.*'
            state: present
            line: 'CLUSTER_NAME'

Is there a way to make it better?

There doesn’t appear to be any debconf options that can be set for the Debian package, however it also doesn’t contain a /etc/defaultdomain file so perhaps you are using a different distro / package?

apt-file list nis
nis: /etc/default/nis                     
nis: /usr/share/doc/nis/NEWS.Debian.gz
nis: /usr/share/doc/nis/changelog.gz
nis: /usr/share/doc/nis/copyright
nis: /usr/share/doc/nis/nis.debian.howto.gz
nis: /usr/share/lintian/overrides/nis

Forgot to mention. I’m using Ubuntu 20.04 LTS. Mention of /etc/defaultdomain I have found here - SettingUpNISHowTo - Community Help Wiki.

The Ubuntu package appears to be the same as the Debian one, so I expect it also doesn’t have any debconf options so you will need to write Ansible tasks to generate / edit the configuration as required.

I’d suggest that you template the NIS config files, that’s the only way to make sure that they will fully conform to what you’re expecting. When writing code I only use lineinfile when either not the whole file is under my control (and then still prefer blockinfile), or it’s massive and you really only need to change a single line in it.

Then it’d look like this:

- name: 'Ensure NIS packages'
    name: 'nis'
    state: 'present'

- name: 'Ensure defaultdomain'
    src: 'files/nis/defaultdomain.j2'  # translates to <playbooks>/files/nis/defaultdomain.j2
    dest: '/etc/defaultdomain'
    owner: 'root'
    group: 'root'
    mode: '0644'  # not sure, been a while since I've seen a working NIS server

And your template would look like:

# {{ ansible_managed }}
<whatever config you need, as I mentioned, it's been a while since I've touched NIS ;-)>

This will make sure that whatever happens, this file will always be exactly what you need it to be.

The only thing that can still happen is that the config file itself can become incompatible with NIS (though, I don’t see that happening anymore). Which means you’ll need to update your Ansible code (which in turn will fix it on all the systems that you have that run NIS.

Have fun! :wink:

1 Like