Manage Windows Machines with Ansible – Create a Domain User With Playbooks and YAML– Part 4

In the fourth blog post of the Windows and Ansible series, we will create a Playbook that creates an Active Directory user.

In the series:


At the heart of Ansible, Playbooks are the driving force that controls the management and configuration of remote machines.

Using YAML files that contain modules, tasks and instructions, Ansible pushes configurations to managed machines as you will see soon.

Configure YAML

Let’s start with an optional step that will make your life easier working with YAML files and detecting errors.

Let’s create a .vimrc file and configure it using the following step which will start with creating the .winrc file with the command below:

vim .vimrc

After creating the file, open it and add the following line to. once done save the file and exit.

autocmd FileType yaml setlocal ai ts=2 sw=2 et

Create a YAML file

Now it is time to create our first YAML file that will connect to Active Directory and create a new user called win-test.

Please note that the module is called win_domain_user, I’m also setting the password for the user and setting the path.

- name: "Work with Active Directory"
  hosts: windows
    - name: "Create a user"
           firstname: "Win"
           surname: "Test"
           name: "WinTest"
           upn: "wintest@corp.enterprise.local"
           state: present
           fullname: "My Win Test User"
           password: "USERPASSWORD"
           path: "ou=ops,dc=corp,dc=enterprise, dc=local"

After saving the file, I will run the Playbook which will connect to Active Directory and create my user. I’ll run the playbook using the following command.

ansible-playbook win.yaml

If all goes well you will see the green status code in the console and the user in AD.

Success! You're on the list.