Powershell and Linux

Reading Time: 2 minutes

In our R&D work in iSolutions, happens very frequently to implement “exotic” patterns to try some new features and create interesting PoC.

You always have to think in such different ways to jump over technical issues or simply to achieve your goals with less headache.

Recently I had to install e configure Ansible AWX (the open version of RedHat Ansible Tower) to speed up our deployments and to have a “smart” DevOps automation platform.

I was thinking about how to test some features and I came out to try it with Powershell Core 6.1 for linux.

I decided to let some of our users to run a job that usually require IT staff, so I created an Ansible template to power on an Azure IaaS VM.

AWX is composed of multiple linux docker containers (backend,frontend,memcached and Rabbit MQ) and when you run a job, it starts from the backend container.

There are already many ansible YAML templates ready to be used on Azure but none of them in a “Powershell way”, so this is what I did :

I installed Powershell on the backend container (its real name is awx_task) :

Then I created a credential file with AZ powershell cmdlets :

  • Login-azaccount (it output a link to a webpage with an OTP code)
  • Select-azsubscription (if you only have one, you can skip this step)
  • Save-azcontext –path “/path/to/your/credentials.json”

Next I created a simple powershell script (startvm.ps1) to power on the VM :

  • Import-AzContext -Path “/path/to/your/credentials.json”
  • Set-AzContext -SubscriptionId “your subscription ID”
  • start-azvm -name “VM name” -resourcegroupname “RG name”

Last, I wrote a YAML template for ansible :

– hosts: localhost

  tasks:

  – name: Power On Azure VM

    command: /usr/bin/pwsh startvm.ps1

    register: powershell_output

    args:

      chdir: /path/to/your/startvm.ps1

In this way, when you run the ansible yaml template, it will execute on the backend container, so it will run the powershell script.

This is the output of the template :

With our “OK” status.

You just have to wait few minutes and the VM will be up and running.

Thanks for your time!