Как установить и настроить PowerShell 7 с помощью Ansible
PowerShell 7 — это универсальная оболочка и язык программирования. Теперь, когда он кроссплатформенный, большинству системных администраторов нужен способ автоматического развертывания языка на нескольких системах. С этой целью Ansible является идеальной системой для создания простых в использовании сборников сценариев для развертывания PowerShell 7 на любом количестве систем, которое может потребоваться.
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
Расширение нашего Playbook для других хостов
---
- 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'
Запускаем нашу Playbook
ansible-playbook /path/to/main.yml -i /path/to/hosts