Configure On-Premises Servers With Ansible Through Azure Devops

In this post, I will walk through how you can set up an Azure DevOps pipeline agent, with Ansible, and use it for configuring and provisioning resources on your on-premises servers. In the example for this post, I will be setting up a Linux pipeline agent, installing Ansible with Kerberos authentication, and at the end creating a DNS record on a Domain Controller.
Prerequisites
To follow along with this lab you will need:
- Windows Server (Domain Controller / DNS Role installed)
- Linux Server
- Azure DevOps Account
Now for this tutorial, I have not taken into account keeping clear text password out of my repo. If you want to create a similar setup you should keep your secrets/passwords in either a key vault or as environment variables.
Setting up the Azure DevOps pipeline agent
I have created a new Project called: TEST-OnpPremisesIAC, inside my Azure DevOps organization.
To add a new pipeline agent to my new project I need to click on “Project Settings” in the lower-left corner, and then under “Pipelines” I need to click on “Agent pools”.
Inside the “Agent pools” settings menu I will click on “Add Pool”.
For the Pool Type I will select “Self-hosted”, and then give it the name: “LinuxAgents”. I will also set the “Pipeline permissions” to “Grant access permissions to all pipelines”.
Then to add a new agent to my newly created pool, I will click on the Pool “LinuxAgents” and then click on “New agent”.
Here i will chose Linux and x64 for my type of virtual machine. I will then click on the copy button next to the Download button. This will copy the link for downloading the agent binary.
I will then head over to my Linux virtual machine, create a new directory inside my home directory, and name it Downloads
|
|
I will then download the agent binary by running the command:
|
|
You then need to extract the gzip archived file by running the command:
|
|
then once the files are extracted you can install all the needed dependencies by running the command:
|
|
Then once the dependencies are installed you are ready to configure the agent. To start the configuration run the command:
|
|
Configuring the DevOps Agent
First, you will be asked to accept the Team Explorer Everywhere License agreement
|
|
Press ‘Y’ to accept
You will then be prompted for your Server URL. This URL will be: https://dev.azure.com/"name_of_organization"
|
|
You will then be asked to choose an authentication type. Here I have chosen PAT. To choose PAT just press enter
|
|
You will now need to create a PAT inside your DevOps organization.
You do this by clicking on the small user icon with a gear, in the upper right corner, and then click on “Personal Access Token”.
Then inside the “Personal Access Token” Settings menu, click on “+ New Token”. Give the Token a name and chose how long time the token should be valid. For this lab environment, I have just chosen 30 days. Then you will need to add a Scope of permissions to which the token has access. Here I have given the Token “Full access”.
You should then receive a Token. Copy it and save it in a secure place. Then paste the token into the Linux Agent.
You will then need to provide the name of the Agent Pool created earlier. Here I will type in LinuxAgents
|
|
You will then need to provide the name of the actual agent. If you want to keep the hostname of the server as the name of the agent, just press “Enter”.
|
|
You then need to provide a work folder. This is the directory from where the agent will run the pipelines. I will just choose the default option, by pressing “Enter”.
|
|
If all went well you should see that the Settings have been saved.
Now to run the agent, I want it to run as a systemd service so I don’t have to be logged in and start the agent software manually every time. To create the agent as a service you can run the script svc.sh
|
|
Then to start the service, run the command
|
|
and you should see an output similar to below:
|
|
You can now also go back to “Azure DevOps”, then to “Project Settings”, then to “Agent pools”, click on “LinuxAgents” and then on “Agents”. You should now see the agent you configured and that it is “online”
Setting the up the DevOps Agent to use Ansible with Kerberos
Before your agent will be able to run any Ansible code you will need to install some prerequisite packages and configure Kerberos authentication. You will need to set up Kerberos Authentication before the agent can connect to any Windows Domain server.
So I will start by installing the packages by running the commands:
|
|
during the installation of the packages, you will be asked for some information regarding your “Realm” or “Domain”.
When installing Kerberos the “Configuring Kerberos Authentication” screen will pop up.
Here you should just type in UPPERCASE the domain name. In my case, my domain is called LAB.LOCAL.
You will then be asked two more times for the server settings. Here you can add the IP address or FQDN of your domain controller.
Now once you have Kerberos configured, you will need to add the domain controller to your hosts file. To do this run the command:
|
|
then add the line to the bottom of the file:
|
|
here is an example of my hosts file
|
|
then reboot your linux server by running the command:
|
|
Once you machine has rebooted you will need to configure Kerberos. To do this open the file /etc/krb5.conf
|
|
Inside the file, under your REALM setting, you can remove everything from the file, and just keep the setting you created during the configurations. You should then have a file similar to mine:
|
|
You should now have Kerberos configured to authenticate against your domain controller. You can test this by running the command:
|
|
you will then be asked to provide a password
|
|
If you do not receive any output of the command, it means that the authentication was successful.
Testing If Ansible can authenticate against your Windows Domain Controller
To test that Ansible can authenticate to your Windows domain controller, you can create an Inventory file, named inventory.ini on your Linux agent. Here is an example of my inventory file.
|
|
i can now test if my Linux agent can connect to my domain controller by running the command:
|
|
and you should recieve an output similar to below:
|
|
if the output was successful you are now ready to build the pipeline in Azure DevOps, and test it.
Building an Azure DevOps Pipeline for provisioning with Ansible
The repository
In my repository i have the following two files:
inventory.ini
|
|
and
dns-playbook.yml
|
|
This playbook will connect to my domain controller, and create a new ‘A’ record inside the DNS zone “lab.local”.
Building the pipeline
To create the pipeline go to “Pipelines”, then click on “Create pipeline”. Then chose where your code is store, in my case it is in “Azure Repos Git”. Then chose you repository and select “Starter pipeline”.
In the pipeline i will define it to be triggered by a push into the main branch
|
|
I then define that I want the pipeline to run on my Linux Agent
|
|
and the only step I have is that it should run the Ansible playbook with the provided inventory file
|
|
and the complete pipeline should look similar to below:
|
|
I can then go ahead and run the pipeline to see if everything worked as supposed. If the Ansible playbook worked, you should see the green checkmark, and inside the log, you should also see that 1 configuration was changed.
and you can run the The PowerShell command on your Domain Controller, just replace the zone name with your own.
|
|
and you should now see the record created by Ansible
Conclusion
Using agents in your Azure DevOps organization will provide you with the option of running commands or scripts on your on-premises servers. This opens up the possibility of using Azure DevOps in the cloud but provisioning your on-premises servers with IaC.
When setting up a configuration similar to what I showed here, you would need to take into considerations how you want to keep your secrets and passwords for connecting to your servers or domain machines.