Components of Ansible
- Executables
- Modules
- Plugins
- Config file
1. Executables
Description:
- These are the command-line tools that come with Ansible. They are used to perform various operations like running playbooks, managing inventory, and gathering facts about systems.
Key Executables:
- ansible: Used to run a single task on a specified group of hosts.
$ ansible all -m ping
This command pings all hosts in the inventory to check connectivity.
ansible-playbook: Used to run playbooks, which are YAML files containing a list of tasks to be executed on the hosts.
$ ansible-playbook site.yml
- This command runs the
site.yml
playbook. - ansible-galaxy: Used to manage Ansible roles, which are pre-packaged units of automation.
$ ansible-galaxy install geerlingguy.apache
- This command installs the Apache role from Ansible Galaxy.
- ansible-inventory: Used to display or manage the inventory, which is a list of hosts Ansible manages.
$ ansible-inventory --list
- This command lists all the hosts and groups in the inventory.
2. Modules
Description:
- Modules are the units of work that get executed by Ansible on the remote hosts. They can manage system resources like packages, files, and services.
Examples of Common Modules:
- ansible.builtin.ping: Tests connectivity to the remote host.
- name: Ping the remote host
ansible.builtin.ping:
ansible.builtin.yum: Manages packages on Red Hat-based systems.
- name: Install a package
ansible.builtin.yum:
name: httpd
state: present
ansible.builtin.service: Manages services on the remote host.
- name: Ensure a service is running
ansible.builtin.service:
name: httpd
state: started
ansible.builtin.copy: Copies files to remote hosts.
- name: Copy a file to the remote host
ansible.builtin.copy:
src: /local/path/to/file
dest: /remote/path/to/file
3. Plugins
Description:
- Plugins extend the core functionality of Ansible. They can be used for various purposes such as enhancing the execution of modules, providing additional functionality, and managing connections.
Types of Plugins:
- Callback Plugins: Modify the way Ansible outputs information.
- Connection Plugins: Manage communication between Ansible and the remote hosts (e.g., SSH, WinRM).
- Lookup Plugins: Retrieve data from external sources (e.g., environment variables, files).
- Filter Plugins: Transform data within playbooks.
- Action Plugins: Modify the way actions are executed.
Example of a Filter Plugin:
- json_query: Used to filter JSON data within a playbook
- name: Extract specific data from JSON
ansible.builtin.debug:
msg: "{{ some_json_variable | json_query('some.query.expression') }}"
4. Config file
Description:
- The Ansible configuration file (
ansible.cfg
) is used to configure settings and behaviors for Ansible. It can be located in several places, including the current directory, the home directory, or/etc/ansible
.
Example Configuration File (ansible.cfg
):
[defaults]
inventory = ./inventory
remote_user = ansible
host_key_checking = False
retry_files_enabled = False
[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False
- inventory: Specifies the path to the inventory file.
- remote_user: Sets the default remote user for connections.
- host_key_checking: Disables host key checking.
- retry_files_enabled: Disables the creation of retry files.
- become: Enables privilege escalation.
- become_method: Sets the method for privilege escalation (e.g., sudo).
- become_user: Specifies the user to become.
- become_ask_pass: Disables the prompt for the privilege escalation password.
Conclusion
Ansible’s components include executables, modules, plugins, and the configuration file. Executables are command-line tools for running tasks and managing inventory. Modules are units of work executed on remote hosts. Plugins extend Ansible’s functionality, and the configuration file sets various operational parameters. Each component plays a crucial role in making Ansible a powerful and flexible automation tool.