Automated installation using Ansible

philmck

Verified User
Joined
Jan 20, 2025
Messages
9
Location
London
I'm trying to automate DirectAdmin installation using Ansible, to help with disaster recovery and host migration. I'd be interested in any experiences and sample playbooks that people have.

One thing I'm struggling with is changing the hostname and updating the Let's Encrypt certificate. I can do it using the normal GUI with no problems but can't get it to work from the command line.

I've tried using the standard ansible.builtin.hostname play and it appears to run without errors. Also tried using /usr/local/directadmin/scripts/hostname.sh (from Ansible or a command line) - again, it appears to run without errors. In both cases I rebooted the server afterwards in case that was necessary. But when I subsequently try to update the SSL certificate by calling /usr/local/directadmin/scripts/letsencrypt.sh request {{ ansible_host }} it fails, because the hostname hasn't actually changed - at least, not where it counts.

This eventually led to LetsEncrypt locking me out, even though I had Let's Encrypt (testing) selected in ACME settings (probably ignored when invoked from the command line). I tried waiting for an hour but that didn't help. I tried a clean install and a new hostname and that didn't help either because DA assigns a temporary hostname based on the IP address and that hadn't changed. I had to fire up a new VPS to get a new IP address, which cost me 5x the DA monthly subscription. (I've since discovered it's possible to change the temporary DA hostname but that requires manual configuration editing and I'm trying to avoid that.)

It's not the end of the world because there will always be a few manual tweaks needed. For example, I can't automate the DA license purchase but then again I'm not sure I want to (or need to, if I'm just restoring a server to a new host).

Has anyone had success with changing the hostname and its certificate from the command line?
 
(Answering my own question) I think I've found a better solution - set the DA_HOSTNAME environment variable (from Ansible) before calling setup.sh. There's still a problem if someone wanted to change the hostname later using SSH but at least there's a workaround - the GUI is active by then.

All you need to do is add this to the play that calls the setup.sh script.

environment:
DA_HOSTNAME: "{{ ansible_host }}"
 
Last edited:
The task I use for running the setup.sh script is something like this:

Code:
- name: "Run DirectAdmin setup | This will take a while"
  shell:
    cmd: "./setup.sh {{ directadmin__license_key }} >/var/log/ansible-da-custombuild/da_setup.sh.{{ ansible_date_time.date + 'T' + ansible_date_time.time + ansible_date_time.tz_offset }}.log 2>&1"
    chdir: "{{ directadmin__setup_sh_path }}"
    creates: "{{ directadmin__config_path }}"
  environment:
    DA_EMAIL: "{{ directadmin__admin_email_address }}"
    DA_NS1: "{{ directadmin__primary_name_server }}"
    DA_NS2: "{{ directadmin__secondary_name_server }}"
    DA_SKIP_FASTEST: "true"
    DA_SKIP_CSF: "true"
    DA_SKIP_SECURE_PHP: "true"
    DA_FOREGROUND_CUSTOMBUILD: "true"
    DA_HOSTNAME: "{{ inventory_hostname }}"
 
Back
Top