Understanding Ansible Configuration and Modules
Now that we have Ansible installed and working, let us explore its configuration and the concept of modules.
Ansible Configuration
Ansible uses configuration files to control its behavior. The main configuration file is ansible.cfg
, which can exist in several locations. Let us examine the default configuration file:
ls -la /etc/ansible/
You should see the default Ansible directory structure:
total 20
drwxr-xr-x 2 root root 4096 Aug 30 12:00 .
drwxr-xr-x 85 root root 4096 Aug 30 12:00 ..
-rw-r--r-- 1 root root 8669 Aug 30 12:00 ansible.cfg
-rw-r--r-- 1 root root 1021 Aug 30 12:00 hosts
Let us take a look at the hosts
file, which is the default inventory file:
cat /etc/ansible/hosts
The inventory file contains a list of hosts that Ansible can manage. By default, it contains examples that are commented out.
Creating a Custom Configuration
Let us create our own Ansible configuration file in our project directory. In the WebIDE, create a new file called ansible.cfg
in the ~/project/ansible-test
directory with the following content:
[defaults]
inventory = ./inventory
host_key_checking = False
stdout_callback = yaml
Now, let us create a simple inventory file. In the WebIDE, create a new file called inventory
in the ~/project/ansible-test
directory with the following content:
[local]
localhost ansible_connection=local
Exploring Ansible Modules
Ansible modules are reusable units of code that perform specific tasks. Let us explore some of the available modules:
ansible-doc -l | wc -l
This command lists all available modules and counts them. You should see a large number, typically over 1000 modules.
Let us view the documentation for a specific module, such as the file
module we used in our playbook:
ansible-doc file
You can press q
to exit the documentation viewer.
Creating a More Complex Playbook
Now, let us create a more advanced playbook that demonstrates a few more Ansible modules. In the WebIDE, create a new file called modules-demo.yml
in the ~/project/ansible-test
directory with the following content:
---
- name: Ansible Modules Demo
hosts: localhost
connection: local
gather_facts: yes
tasks:
- name: Display system information
debug:
msg: "System: {{ ansible_distribution }} {{ ansible_distribution_version }}"
- name: Create a directory
file:
path: /tmp/ansible-demo
state: directory
mode: "0755"
- name: Copy a file
copy:
content: "Created by Ansible modules demo playbook\n"
dest: /tmp/ansible-demo/info.txt
mode: "0644"
- name: Gather information about a file
stat:
path: /tmp/ansible-demo/info.txt
register: file_info
- name: Show file information
debug:
msg: "File created at {{ file_info.stat.mtime }}"
Let us run this playbook:
cd ~/project/ansible-test
ansible-playbook modules-demo.yml
The playbook does the following:
- Displays information about your system
- Creates a directory at
/tmp/ansible-demo
- Creates a file with custom content
- Gathers information about the file
- Displays the file's modification time
Let us mark this step as complete for our verification script:
touch /tmp/ansible_modules_explored
You have now learned about Ansible configuration, inventory files, and explored various modules. These are essential components for working with Ansible effectively.