If you are not familiar with Ansible, is an implementation language in which you can write playbooks to send a series of commands to systems and instruct them on what to do. Unlike many other configuration systems, you don't need an agent on the target system. This makes it easy to use and configure.
In this post, let's explore how to create a simple Ansible playbook to install PowerShell 7 on multiple systems.
Installing Ansible on Windows and Linux
Installing Ansible on a Linux system is very easy. Most package systems for Linux distributions have this built in. A bit of common ways to install Ansible are as follows:
Despite this, Windows is a unique case, since Ansible is not enabled as a Windows package. The easiest way to install Ansible for use on Windows is to use the Windows Subsystem for Linux (WSL). This is a virtualized instance of Linux running in parallel with Windows. Once installed, you can use the same installation commands within WSL to install Ansible.
Configurar Ansible
There are some key components for ansible that we need to install PowerShell 7. In particular, we need a hosts file to set our destination locations. There are many alternatives to configure this, but usually, a folder structure like the one shown below works fine.
Within inventories
directory, we would create a hosts
file containing all the systems we want to target with our playbook. A simple hosts file that creates a group of hosts under the production
label described below. Comments are useful to tell you what the actual hostname of the systems is.
[production]
#test-system-01
100.100.10.10
#test-system-02
100.100.10.11
#test-system-03
100.100.10.12
#test-system-04
100.100.10.13
You can create multiple groups of hosts and the same host can exist in multiple groups. This makes grouping and "tagging" those hosts easier to manage later for more complex roles..
Creating our Playbook
Now that we have our hosts file, we can start building our playbook. To do this, let's first create a new folder under the playbooks
folder to contain our playbook. In this circumstance we are going to call it deploy-powershell
. Under that folder, we will create the following file, main.yml
. the main.yml
file is our main entry point for the playbook. Does not necessarily have to be named main.yml
but it's a common convention.
---
- name: Install PowerShell 7
hosts: all
tasks:
- name: Download and Add Powershell Key to Apt-Get Keyring
apt_key:
dirección url: "https://packages.microsoft.com/keys/microsoft.asc"
state: present
- name: Add Powershell Repository into /etc/apt/sources.list - Bionic
apt_repository:
repo: 'deb [arch=amd64] https://packages.microsoft.com/ubuntu/18.04/prod bionic main'
state: present
- name: Install Powershell
apt:
pkg: powershell
state: latest
force: yes
Expanding our playbook for other hosts
At this time, our playbook only handles Ubuntu Linux systems. To update this, we can use Ansible conditionals to make this a bit more robust. Let's extend this to be compatible with Redhat Linux and Fedora systems.
To allow support for other package installation systems, we use the when
conditional clause. When reading the ansible_distribution
value, we can tell Ansible to only target certain distributions for specific commands.
---
- name: Install PowerShell 7
hosts: all
tasks:
- name: Download and Add Powershell Key to Apt-Get Keyring
apt_key:
dirección url: "https://packages.microsoft.com/keys/microsoft.asc"
state: present
- name: Add Powershell Repository into /etc/apt/sources.list - Ubuntu
apt_repository:
repo: 'deb [arch=amd64] https://packages.microsoft.com/ubuntu/18.04/prod bionic main'
state: present
when: ansible_distribution == 'Ubuntu'
- name: Add repository - Fedora
yum_repository:
name: microsoft
description: Microsoft Repository
baseurl: "https://packages.microsoft.com/config/rhel/7/prod.repo"
when: ansible_distribution == 'Fedora'
- name: Add repository - RedHat
yum_repository:
name: microsoft
description: Microsoft Repository
baseurl: "https://packages.microsoft.com/config/rhel/7/prod.repo"
when: ansible_distribution == 'RedHat'
- name: Install Powershell Package
apt:
pkg: powershell
state: latest
force: yes
when: ansible_distribution == 'Ubuntu'
- name: Install the PowerShell Package
yum:
name: powershell
state: latest
when: ansible_distribution == 'RedHat'
- name: Install the PowerShell Package
dnf:
name: powershell
state: latest
when: ansible_distribution == 'Fedora'
Running our playbook
Our playbook is set, so let's go ahead and install this on the systems we want. To do this, we will execute the next command line.
ansible-playbook /path/to/main.yml -i /path/to/hosts
While we run this, we will get the results of each step for each host the playbook is running against and its success rate. Each command will run on each system as the playbook runs, this means that each command must be completed before moving on to the next command. At the same time, the conditional when
will show as a jump within the results.
conclusion
Ansible makes it easy to install PowerShell on multiple systems at the same time. Once PowerShell 7 is installed on those systems, you can still use Ansible and then configure the shell for registration, remote access and other capabilities to make it even easier to migrate from Windows PowerShell or PowerShell Core to the new PowerShell 7 unified.
Ansible in combination with PowerShell enables you to quickly and easily deliver PowerShell to many different systems in a flexible and convenient method.