X
Popular Searches

How to Install and Configure PowerShell 7 using Ansible

Powershell logo

PowerShell 7 is a versatile shell and programming language. Now that it is cross-platform, most system admins need a way to deploy the language to multiple systems automatically. To that end, Ansible is a perfect system to create easy to use playbooks to deploy PowerShell 7 to as many systems as may be needed.

If you aren’t familiar with Ansible, it is a deployment language that one can write playbooks to send a series of commands to systems and instruct them on what to do. Unlike many other configuration systems, it does not require an agent on the target system. This makes it easy to use and set up.

In this article, we are going to explore how to create a simple Ansible playbook to install PowerShell 7 to multiple systems.

Installing Ansible on Windows & Linux

To install Ansible on a Linux system, it is very easy. Most package systems for Linux distributions have this built-in. A few of the common ways to install Ansible are as follows:

  • sudo apt install ansible
  • sudo yum install ansible
  • sudo dnf install ansible

Windows is a unique case though, as Ansible is not available 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 that runs in parallel with Windows. After this is installed, you can use the same installation commands within WSL to install Ansible.

Set Up Ansible

There are a few key components to ansible that we need to install PowerShell 7. Notably, we need a hosts file to define our target locations. There are many ways to set this up, but usually, a folder structure such as below works well.

  • inventories
  • playbooks
  • vars

Within the inventories directory, we would create a hosts file that contains all of the systems that we want to target with our playbook. A simple hosts file that creates a group of hosts under the production tag is outlined 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 are able to create multiple groups of hosts and the same host can exist in multiple groups. This makes grouping and “tagging” those hosts easier for managing them later on for more complex roles.

Creating our Playbook

Now that we have our hosts file, we can start to build our playbook. To do this let’s first create a new folder under the playbooks folder to contain our playbook. In this case, 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 primary entry point for the playbook. It doesn’t necessarily have to be named main.yml but it is common convention.

---
- name: Install PowerShell 7
  hosts: all
  tasks:
	- name: Download and Add Powershell Key to Apt-Get Keyring
	  apt_key:
	    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

Extending our Playbook for Other Hosts

Right now our playbook only handles Ubuntu Linux systems. To update this, we can use Ansible conditionals to make this a bit more robust. Let’s expand this to support Redhat Linux and Fedora systems.

To allow support for other package installation systems, we use the when conditional clause. By 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:
	    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 up, so let’s go ahead and actually install this on the systems that we want. To do this, we will run the following command line.

ansible-playbook /path/to/main.yml -i /path/to/hosts

While running this we will get the results of each step for each host that the playbook is run against and their success rate. Each command will run on each system as the playbook runs, this means that each command needs to complete before moving on to the next command. Additionally, the conditional when will show as a skip within the results.

Conclusion

Ansible makes it easy to install PowerShell on multiple systems at once. Once PowerShell 7 is installed on those systems, you can further use Ansible to then configure the shell for logging, remote access, and other abilities to make it even easier to migrate from Windows PowerShell or PowerShell Core to the new unified PowerShell 7.

Ansible in combination with PowerShell allows you to quickly and easily distribute PowerShell to many different systems in a flexible and convenient method.

Adam Bertram Adam Bertram
Adam Bertram is a 20+ year veteran of IT and an experienced online business professional. He’s a consultant, Microsoft MVP, blogger, trainer, published author and content marketer for multiple technology companies. Catch up on Adam’s articles at adamtheautomator.com, connect on LinkedIn, or follow him on Twitter at @adbertram. Read Full Bio »

The above article may contain affiliate links, which help support CloudSavvy IT.