In this post, I’ll walk you through how I structured my Ansible project and created a playbook to automate joining machines to a domain and renaming them based on a CSV file.

I’m keeping this simple by not using Ansible roles yet — just flat files and directories. I manually created the directories. Here are the steps for automation:


Step 1: Create the necessary directories

mkdir -p ~/ansible/{group_vars,inventory,play,script,template}

 

ansible/
├── ansible.cfg
├── group_vars/
├── inventory/
├── play/
├── script/
├── template/

Step 2: Set the inventory location

Create an ansible.cfg file in the root of the project and set the inventory path and other useful defaults:


[defaults]
inventory=inventory/inv.ini
host_key_checking = False
timeout = 30
retry_files_enabled = False
~

Step 3: Add the machines to the inv.ini

This CSV maps inventory_hostname (from inv.ini) to the new computerName you want each host renamed to. I set the variables for windows as you see in [windows:vars].

Since these are Windows machines, I used Winrm. 

[windows]
grace-test-c1.testlab.org ansible_host=192.168.1.237
grace-test-c2.testlab.org ansible_host=192.168.1.238
grace-test-c3.testlab.org ansible_host=192.168.1.239
grace-test-c4.testlab.org ansible_host=192.168.1.240
grace-test-c5.testlab.org ansible_host=192.168.1.241
grace-test-c6.testlab.org ansible_host=192.168.1.242
grace-test-c7.testlab.org ansible_host=192.168.1.243

[windows:vars]

ansible_winrm_transport=ntlm
ansible_port=5985
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore
ansible_user=testlab\grace

Step 4: Create the CSV File for Other Information to Loop

 

inventory_hostname,computerName
grace-test-c1.testlab.org,grace-test-c1
grace-test-c2.testlab.org,grace-test-c2
grace-test-c3.testlab.org,grace-test-c3
grace-test-c4.testlab.org,grace-test-c4
grace-test-c5.testlab.org,grace-test-c5
grace-test-c6.testlab.org,grace-test-c6
grace-test-c7.testlab.org,grace-test-c7

 


Step 5: Store the Secrets in the Vault, which is Encrypted

Add file under the group_vars group_vars/sshvault.yml

local_admin_username: "Administrator"
local_admin_pass: "LocalPassword123"
grace_username: "testlab\\grace"
grace_become_pass: "SuperSecurePass!"
domain_name: "testlab.org"

 

Now encrypt it with:

ansible-vault encrypt group_vars/sshvault.yml

You’ll be asked to enter a password for encrypting the file. This will be required later when running the playbook.

 Here are the steps from my cli:

grace@ansible-mgt:~/ansible$ ansible-vault encrypt group_vars/sshvault.yml
New Vault password:
Confirm New Vault password:
Encryption successful
grace@ansible-mgt:~/ansible$

Step 6: Install Required Collections

ansible-galaxy collection install community.general
ansible-galaxy collection install ansible.windows

Step 7: Write the Playbook

Here is the playbook written in yaml in play/join_domain.yml

---
- name: Join machines to domain
  hosts: windows
  vars_files:
    - ../group_vars/sshvault.yml
  vars:
    ansible_user: "{{ local_admin_username }}"
    ansible_password: "{{ local_admin_pass }}"

  tasks:
    - name: Read CSV file
      community.general.read_csv:
        path: ../template/machines.csv
        delimiter: ','
        key: inventory_hostname
      register: name_csv
      delegate_to: localhost

    - name: Create hostname to domain mapping
      set_fact:
        hostname_mapping: "{{ name_csv.dict | combine({ item.key: item.value }) }}"
      loop: "{{ name_csv.dict | dict2items }}"

    - name: Rename machine
      ansible.windows.win_hostname:
        name: "{{ hostname_mapping[inventory_hostname].computerName }}"
      vars:
        ansible_become_user: "{{ grace_username }}"
        ansible_become_password: "{{ grace_become_pass }}"
      register: rename_result

    - name: Reboot if rename was successful
      ansible.windows.win_reboot:
        msg: "Rebooting after rename"
        reboot_timeout: 300
      when: rename_result.reboot_required
      vars:
        ansible_become_user: "{{ grace_username }}"
        ansible_become_password: "{{ grace_become_pass }}"

    - name: Join domain
      ansible.windows.win_domain_membership:
        dns_domain_name: "{{ domain_name }}"
        domain_admin_user: "{{ grace_username }}"
        domain_admin_password: "{{ grace_become_pass }}"
        state: domain
      register: domain_state
      retries: 3
      delay: 60
      until: domain_state.reboot_required or not domain_state.failed

    - name: Reboot after domain join
      ansible.windows.win_reboot:
        msg: "Domain joined. Rebooting..."
        reboot_timeout: 300
      when: domain_state.reboot_required

    - name: Check domain membership
      ansible.windows.win_shell: Get-WmiObject Win32_ComputerSystem | Select-Object Name,Domain
      register: domain_membership_info

    - name: Show status
      debug:
        msg: "{{ domain_membership_info }}"

Step 8: Run the Playbook

From inside ~/ansible, run:

ansible-playbook play/join_domain.yml --ask-vault-pass

It will prompt to input your vault password, and after that, 

If everything’s set up properly, running this will:

  • Parse your CSV
  • Rename the machine and reboot if needed
  • Join it to the domain
  • Reboot again and confirm domain membership”

You can test your script first using

  • Test with one machine first using --limit host1.

Final Project Tree


ansible/
├── ansible.cfg
├── group_vars/
│   └── sshvault.yml  # Encrypted
├── inventory/
│   └── inv.ini
├── play/
│   └── join_domain.yml
├── template/
│   └── machines2.csv

 

If you want to upload it to your github repository, just put the sshvault password directory in the .gitignore. For more information on how to set up your github repository, just refer to https://www.aklatcyber.com/post/41/.