I have deployed an Azure Linux VM and installed the AADSSHLoginForLinux VM extension. This allows me to login to the VM using my Azure credentials, and allows me to configure role based access for my team using EntraID (Active Directory) groups. Any team member can log into the VM using the following command, without needing to mess with SSH keys, etc, and they will get the appropriate access (sudo or regular user) depending on what EntraID group they belong to, using the azure-cli ssh wrapper:
az ssh vm --ip 1.2.3.4
(Assuming they have previously run as login at some point.)
Now I want to use this as the ssh command for Ansible, which will automatically allow anyone in the admin security group to deploy the playbook.
az ssh vm will pass through ssh arguments supplied following --.
For example:
az ssh vm --ip 1.2.3.4 -- -p 23
So it should be possible to wrap az ssh vm up in such a way that Ansible can use it.
I have found the ssh_executable option for Ansible, but this expects a command, not a command with arguments, so I can't simply set it to az ssh vm ....
So a wrapper script will be necessary.
I have also determined that Microsoft do not supply an equivalent wrapper for scp, so this will need to be worked around.
How can I put all the pieces together to easily deploy Ansible playbooks using my Azure credentials and az ssh?
The first step is to create an SSH wrapper which will intercept the SSH arguments from Ansible and pass them through to
az ssh.This blog post describes how to do this for GCP IAP. I have adapted it to work with
az ssh:I am using the following wrapper, adapted from the blog post above:
Save this script as
ssh-wrapper.shin the same directory as your Ansible playbook.Now, we need to configure the playbook to use the wrapper. We also need to configure Ansible to use
pipedtransfer rather thanscporsftp. This will pipe files through SSH, soscpandsftpare not required.The start of your playbook should look something like this:
Assuming you have previously run
az login, and are able to login to the VM withaz ssh login --ip 1.2.3.4, you should be able to simply run the Ansible playbook as follows:Edit: Python version of wrapper
The following Python SSH wrapper has several advantages over the bash wrapper above:
az ssh.