Install Ansible on MacOS, Windows, Ubuntu(debian) and Fedora(rpm) - Part 1
Thinking about Ansible as your configuration management tool, Great choice, of course, Ansible has a lot to offer and also a lot to learn to use effectively. Today’s article is your first step to Ansible!
In this tutorial,, we will dive together into Ansible's installation process on different platforms.
Table of Content
- Brief Intro to Ansible
- Prerequisites
- SSH key generation
- RPM-based Installation
- Debian-based installation (Ubuntu/Linux Mint) 18.04/20.04/22.04 LTS
- Monterey macOS installation (using brew as well as without brew)
- Installation on windows using powershell
- Possible issues
- Verify Ansible installation by running ad-hoc command
- Conclusion
1. Brief Intro to Ansible
Ansible is an automation tool that can be used to enable users to have their servers configured automatically and scalably by writing minimal scripts. Imagine having one script that can configure 10 or 1000 servers in parallel without having to be manually helped or supervised by an IT specialist. Can you imagine the possibilities!
Ansible has 3 Main components :
- Playbook: Where your ansible configuration scripts is placed
- Inventory : A place to set your instances IPs and other details necessary for the ssh process
- Sever : A running machine with ansible
2. Prerequisites
Control Node | Ansible Hosts | |
---|---|---|
1. | The Machine that has ansible installed on it and controls ansible hosts | The Machine that is controlled and managed by Ansible |
2. | Can be either a local machine or a dedicated server (AWS EC2 instance for example) | Should be able to be ssh connected to the control node |
3. | You need either root access or a user with Sudo privileges | Can be one machine or multiple machines |
4. | Will need to have ssh key pairs to connect to other machines (explained in next section ) | Each host's Public IP address is needed |
3. SSH key generation
- Create RSA key pair using :
1ssh-keygen -C "Comment"
- It will ask for the password or else you could keep the password blank also. But remember to take note of the location where the ssh keys will be generated.
1Generating public/private RSA key pair.
2Enter file in which to save the key (/home/vagrant/.ssh/id_rsa):
3Enter passphrase (empty for no passphrase):
4Enter same passphrase again:
5Your identification has been saved in /home/vagrant/.ssh/id_rsa.
6Your public key has been saved in /home/vagrant/.ssh/id_rsa.pub.
7The key fingerprint is:
8SHA256:LWGasiSDAqf8eY3pz5swa/nUl2rWc1IFgiPuqFTYsKs vagrant@amaster
9The key's randomart image is:
10+---[RSA 2048]----+
11| . |
12| . . o . . |
13|. . = . + . . . |
14|o+ o o = o . |
15|+.o = = S . . |
16|. .*.++... .. |
17| ooo*.o ..o. |
18| E .oo* .oo+ . |
19| .oo*+. + |
20+----[SHA256]-----+
- Copy ssh key to your remote server :
1 ssh-copy-id username@host
(Note- Please replace the username and host)
- Accept connection : Now the Key is added to your remote machine
4. RPM based Installation(Fedora/CentOS/RedHat)
The following installation instructions should work on RPM based linux distros .i.e. Fedora, CentOS and RedHat
Fedora
- If running Fedora distro, first update the yum repository :
1yum update
Run the following command to install Ansible from the yum repository -
1sudo yum install ansible
CentOS
- Use the following command for installing Ansible on CentOS. The command will first update
yum
repository and then it will perform Ansible installation -
1sudo yum -y update && sudo yum -y install epel-repo && sudo yum -y update && sudo yum -y install ansible
Other RPM-based distros- Use the following command to build an RPM distribution on your own -
1yum install rpm-build make asciidoc git python-setuptools python2-devel
2git clone git://github.com/ansible/ansible.git --recursive
3cd ./ansible
4make rpm
5sudo rpm -Uvh ./rpm-build/ansible-*.noarch.rpm
Each of the above commands will do the following tasks -
- Installs dependencies like git and make
- Clone the ansible repo with git
- Make rpm artifact
- Install artifact
5. Debian based installation (Ubuntu/Linux Mint) 18.04/20.04/22.04 LTS
- Add an ansible repository to the
apt-get
package manager -
1sudo apt-get install software-properties-common && sudo apt-add-repository ppa:ansible/ansible
- Update
apt-get
package manager and install Ansible -
1sudo apt-get update && sudo apt-get install ansible
Verify the ansible installation on Ubuntu by running ansible --version
6. MacOS installation (using brew as well as without brew)
6.1 Anisble installation using brew
- Install the
brew
onto your macOS (If you do not have already installed)
1/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Install Ansible
1brew install ansible
6.2 Without Using brew
- Check if Xcode is installed on your machine
1xcode-select -p
If no error is received then Xcode is installed and proceed to the next step otherwise you need install Xcode
- Check if python is installed (It’s by default installed on the new version of macOS but in-case if python is missing then install the python)
1python --version
or
1python3 --version
If you have already installed python onto your machine then it should return you back with the version -
1Python 2.X.X
OR
1Python 3.X.X
- To Install then ansible use
1sudo easy_install pip
2sudo pip install ansible --quiet
7. Installation on windows using powershell
To install Ansible on Windows you need to run Windows Subsystem for Linux (WSL) which can be used to deploy a linux distro (ubuntu by default) to enable installation of ansible.
- Install WSL from PowerShell
1wsl --install
If you do not have wsl
installed then refer to the following instructions
List the wsl
versions -
1wsl --list --online
Then select a distro and run
1wsl --install -d (Distro Name)
Set the username and password for the Linux subsystem after installing the wsl
-
1Enter new UNIX username: your-username
2New password: your-password
3Retype new password: your-password
- Install the Ansible based on your Linux subsystem distribution. If you are using debian based Linux the follow these installation instructions or if you are using rpm based linux then follow these installation instructions
8. Possible issues which you might face during the installation
8.1 AttributeError: 'module' object has no attribute '_vendor'
During the installation of Ansible on MacOS, you might face the error _AttributeError: 'module' object has no attribute 'vendor'.
How to troubleshoot?- You need to install pip with python and then install ansible with pip
1sudo easy_install pip || sudo python -m ensure pip
2sudo pip install ansible
8.2 Error: Cannot retrieve Metalink for repository: epel. Please verify its path and try again
It might be possible that during the installation of Ansible on Fedora you might end up with the error Error: Cannot retrieve Metalink for repository: epel. Please verify its path and try again -
How to troubleshoot?- Build an RPM on your own from Ansible’s repo
1yum install rpm-build make asciidoc git python-setuptools python2-devel
2git clone git://github.com/ansible/ansible.git --recursive && cd ./ansible
3make rpm && sudo rpm -Uvh ./rpm-build/ansible-*.noarch.rpm
8.3 Error during the installation of Ansible on Debian
The repository 'http://ppa.launchpad.net/ansible/ansible/ubuntu kinetic Release' does not have a Release file.
How to troubleshoot?- Add the
1sudo apt-add-repository --remove ppa:ansible/ansible
2sudo apt-get update && sudo apt-get install ansible --allow-unauthenticated
9. Verify Ansible installation by running adhoc command
Irrespective of the base operating system you verify the installation of ansible by running adhoc
commands.
For example -
- Verify the Disk usage of your machine after the ansible installation -
1ansible localhost -a "df -h" -u root
- Find the total uptime of the host machine -
1 ansible localhost -a "uptime" -u root
10. Conclusion
I hope this tutorial can help you to validly install ansible on your own machine and start your learning with Ansible, in the following article we look into How to write an ansible playbook?
Posts in this Series
- Ansible Handlers Explained Real-World Use Cases & Examples?
- How to Use SSH Keys with Ansible for Secure Server Management?
- Why YAML is so important in Ansible?
- Clone Public and Private Git Repositories with Ansible
- How to Limit Ansible playbook on Hosts, Groups and multiple hosts?
- Easy Fix for 'zsh command not found ansible' Error After Installing Ansible with Pip
- Demystifying Hosts, Inventory Roles, and Tasks
- Fixing-Unable to start service apache2 Job for apache2.service failed because the control process exited with error code?
- Why Ansible is the Ultimate Tool for DevOps Teams - A Beginner's Guide?
- Ansible how to fix destination path already exists and is not an empty directory?
- Install Ansible on MacOS, Windows, Ubuntu(debian) and Fedora(rpm) - Part 1