DEV Community

Discussion on: Connecting to Azure with Ansible

Collapse
 
nippytalkin profile image
Sandeep Sharma

Thanks Josh for prompt reply, i tried the same however it does not work. i am sure i am doing something wrong here (Refer the screen shot). this is what i do.

  1. Login to Azure Cloud Shell
  2. change directory to CloudDrive -- this is where my host file lives
  3. run this command ansible -i hosts -m win_ping & also tried ansible -i servername, -m win_ping
Thread Thread
 
joshduffney profile image
Josh Duffney • Edited

So you've run into the fun part of using Ansible with Windows. Ansible was developed for Linux first and its default connection will be SSH to Linux targets to override this you need to specify several Ansible variables to modify the connection options.

The settings and values you need to change greatly depend on your configuration, but here are some basic ones for WinRM over HTTP using NTLM authentication.

ansible_user: user
ansible_password: P@ssw0rd
ansible_port: 5985
ansible_connection: winrm
ansible_winrm_transport: ntlm
ansible_winrm_server_cert_validation: ignore

You can place these values anywhere but here are a few examples. One using the Ansible cmd and another using a playbook.

ansible all -i hosts -m win_ping -e "ansible_user=azureuser ansible_password=P@ssw0rd ansible_connection=winrm ansible_winrm_transport=ntlm ansible_winrm_server_cert_validation=ignore ansible_port=5985"

or a playbook

---
- hosts: all

vars:
    ansible_user: azureuser
    ansible_password: P@ssw0rd
    ansible_port: 5985
    ansible_connection: winrm
    ansible_winrm_transport: ntlm
    ansible_winrm_server_cert_validation: ignore

tasks:

- name: Create logging directory
    win_file:
        path: c:\logs
        state: directory

You can also put these vars in the group_vars folder or in the hosts file as vars. Which ever you prefer. I also wrote about remote management with Ansible and Windows see the below post. I hope this helps!

NOTE: If you're using WinRM over HTTP on 5985 you will have to open a firewall port on the vm or disable windows firewall.

dev.to/cloudskills/provisioning-az...

Thread Thread
 
nippytalkin profile image
Sandeep Sharma • Edited

You are right Josh, i have been using group_vars and exactly same method you mentioned on my centOs ansible controller host. However when it comes to Azure Shell it does not work. Sharing my screen output..

➜ clouddrive ansible path -i hosts -m win_ping
[WARNING]: Could not match supplied host pattern, ignoring: path
[WARNING]: No hosts matched, nothing to do
➜ clouddrive ansible patch -i hosts -m win_ping
10.0.0.222 | UNREACHABLE! => {
"changed": false,
"msg": "kerberos: the python kerberos library is not installed",
"unreachable": true
}
➜ clouddrive ls group_vars
patch

➜ clouddrive more group_vars/patch

ansible_user: myUser@Domain.com
ansible_password: Welcome@123
ansible_port: 5986
ansible_connection: winrm
ansible_winrm_server_cert_validation: ignore
ansible_winrm_transport: kerberos
ansible_winrm_kerberos_delegation: true
➜ clouddrive

Thread Thread
 
joshduffney profile image
Josh Duffney

I see you're using Kerberos auth. In that case, you'll have to install the Kerberos python libraries. I'm not sure how those will preserved probably stored in the storage account? Humm, very interesting.

yum install -y krb5-workstation
yum install -y krb5-devel 
Thread Thread
 
nippytalkin profile image
Sandeep Sharma

Azure Cloud Shell wont allow you to install anything. So there has to be another way or Ansible on Cloud Shell does not serve the purpose.

Thread Thread
 
joshduffney profile image
Josh Duffney

Good point, I'll do some digging. Something tells me there is a way to mount external modules to cloud shell without installing them directly. In the time being NTLM seems to be the best alternative.

Thread Thread
 
nippytalkin profile image
Sandeep Sharma

Again, NTLM is not an option for most of enterprise customers. either SSP or Kerberos. Will check too about External Modules on azure.