Difference between revisions of "Ansible Basics"
| Line 67: | Line 67: | ||
An example playbook may look like this: | An example playbook may look like this: | ||
<nowiki> | <nowiki> | ||
| − | - name: Basic Setup | + | - name: Basic Setup (Description of what this playbook does) |
| − | hosts: all | + | hosts: all (You can also specify a specific host group, like "file-server" or "web-server" or "prod") |
become: true | become: true | ||
tasks: | tasks: | ||
| − | - name: Set Hostname | + | - name: Set Hostname (Description of what this specific task does) |
hostname: | hostname: | ||
name: DESIRED_CUSTOM_HOSTNAME_OF_TARGET_NODE or "{{ inventory_hostname }}" (can use this option if the target's IP is included in the inventory file, Ansible will have the target set its hostname to the target's corresponding IP address) | name: DESIRED_CUSTOM_HOSTNAME_OF_TARGET_NODE or "{{ inventory_hostname }}" (can use this option if the target's IP is included in the inventory file, Ansible will have the target set its hostname to the target's corresponding IP address) | ||
| Line 84: | Line 84: | ||
reboot_timeout: 300 | reboot_timeout: 300 | ||
when: ansible_hostname != inventory_hostname (this task will only be run when this condition is met, when the device's hostname does NOT match the hostname specified in the inventory file passed to Ansible) </nowiki> | when: ansible_hostname != inventory_hostname (this task will only be run when this condition is met, when the device's hostname does NOT match the hostname specified in the inventory file passed to Ansible) </nowiki> | ||
| − | |||
| − | |||
== Running an Ansible Playbook == | == Running an Ansible Playbook == | ||
Revision as of 08:37, 20 March 2025
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. Typically these are .ini files. i.e.:
INVENTORY.ini / ANSIBLE_HOSTS.ini / SERVER_LIST.ini
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
Creating an Ansible Playbook (Basics)
An Ansible playbook is the instruction set that Ansible will follow to enforce the changes/configurations/etc across the specified hosts in the playbook file. Typically these files are .yml files. i.e.:
PLAYBOOK.yml / initial_setup.yml / web_server.yml
An example playbook may look like this:
- name: Basic Setup (Description of what this playbook does)
hosts: all (You can also specify a specific host group, like "file-server" or "web-server" or "prod")
become: true
tasks:
- name: Set Hostname (Description of what this specific task does)
hostname:
name: DESIRED_CUSTOM_HOSTNAME_OF_TARGET_NODE or "{{ inventory_hostname }}" (can use this option if the target's IP is included in the inventory file, Ansible will have the target set its hostname to the target's corresponding IP address)
- name: Ensure epel-release repository is installed
dnf: (calls the Ansible module for dnf)
name: epel-release (specifies the package to pass to dnf)
state: present ('present' checks that the package is installed, and installs it if not)
-name: Reboot if hostname changes
reboot:
msg: "Rebooting after hostname change"
connect_timeout: 5
reboot_timeout: 300
when: ansible_hostname != inventory_hostname (this task will only be run when this condition is met, when the device's hostname does NOT match the hostname specified in the inventory file passed to Ansible)
Running an Ansible Playbook
The basic syntax for running an Ansible playbook command looks like:
ansible-playbook -i INVENTORY_FILE PLAYBOOK_FILE
If your playbook will require Ansible to perform operations as a superuser (i.e. will require 'sudo') you can pass the 'sudo' password like this:
ansible-playbook -i INVENTORY_FILE PLAYBOOK_FILE --ask-become-pass