Ansible Basics

From Nick's Personal Wiki
Revision as of 07:55, 20 March 2025 by Nick (talk | contribs) (Added Inventory File)
Jump to navigation Jump to search

Installing Ansible

Enable Required Repositories

Ansible is available via the EPEL (Extra Packages for Enterprise Linux) or the official Red Hat repositories.

Enable EPEL Repository

For RHEL systems, install the EPEL repository:

sudo dnf install epel-release 


Install Ansible

Once the repository is enabled, install Ansible using the package manager:

sudo dnf install ansible 


Verify the Installation

After installation, verify Ansible is installed correctly:

ansible --version 

You should see output similar to:

ansible [core 2.x.x]
  config file = /etc/ansible/ansible.cfg
  python version = 3.x.x 


Troubleshooting

  • If epel-release is not found, try enabling the codeready-builder repo or check for typos.


Creating an Ansible Inventory File

An inventory file for Ansible is a file that has a list of the hosts Ansible will interact with, their hostnames, IP addresses, and other relevant information.

An example inventory file might look like:

[web-server] (this is the host group)
web-server-01 ansible_host=1.2.3.4 (each entry is a single host, with arguments/options beside)
web-server-02 ansible_host=WEBSERVER_HOSTNAME 

You can also format the inventory file, to manage entire host groups collectively, like this:

[file-server]
file01 ansible_host=1.2.3.4
file02 ansible_host=5.6.7.8
file03 ansible_host=9.10.11.12

[file-server:vars]
ansible_user=USER
ansible_port=PORT#
ansible_become=[true/false] (whether or not Ansible will become 'root' user)
ansible_become_user=[true/false] (whether or not Ansible will become the current user)
ansible_ssh_private_key=/PATH/TO/IDENTITY/FILE  


Additional Resources