Ansible: 5 Things You Should Know!
- Admin
- Aug 5, 2022
- 3 min read
Ansible is a tool for IT automation. It is capable of configuring systems, deploying software, and orchestrating more complex IT processes such as continuous deployments and zero-downtime rolling upgrades.
The primary aims of Ansible are simplicity and usability. It also has a significant emphasis on security and stability, with a minimal number of moving elements, the use of OpenSSH for transport (with other transports and pull modes as options), and a language built for even individuals who are unfamiliar with the programme.
Ansible mostly use the push technique.
Ansible has no agents.
USE CASES
Provisioning
Configuration Management
App Deployment
Continuous Delivery
Security & Compliance
Orchestration
INTEGRATIONS
Infrastructure
Networks
Containers
Cloud
DevOps Tools
5 Things You Should Know About Ansible
1. Ansible Playbooks
Playbooks are the files that contain Ansible code. Playbooks are authored in the YAML language. YAML is an abbreviation for "YAML Ain't Markup Language." Playbooks are one of Ansible's basic features that instruct it what to do. They function similarly to a to-do list for Ansible, including a list of tasks.
The Various YAML Tags in a Playbook
Name
Hosts
Vars
Tasks
A Sample playbook
---
- name: install nginx webserver
hosts: WebServer
become: yes
tasks:
- name: nginx installation
yum:
name: nginx
state: present
2. Ansible Modules
Modules are independent bits of code that may be utilised from the command line or in a playbook job. Ansible runs each module, often on the distant target node, and gathers the results. Each module is executed on the target system via Ansible.
Modules can be run from the command line:
ansible WebServer -m service -a "name=nginx state=started"
YAML is another method for passing parameters to a module.
- name: restart webserver
service:
name: nginx
state: restarted
Examples of modules are including:
Command Module
Shell Module
Copy Module
File Module
3. Ansible Variables
The use of variables in playbooks is quite similar to the use of variables in any programming language. It enables you to utilise and assign a value to a variable, which you can then use elsewhere in the script. Conditions can be placed around variable values and used in the script accordingly.
Variables in the playbook can be defined.
Variables can be specified in a separate file.
A host inventory file can be used to specify variables.
Example:
---
- hosts: all
become: true
vars:
doc_root: /var/www/example
tasks:
- name: Update apt
apt: update_cache=yes
- name: Install Apache
apt: name=apache2 state=latest
- name: Create custom document root
file: path={{ doc_root }} state=directory owner=www-data
4. Ansible Roles
The role is the basic method in Ansible for dividing a playbook into numerous files. This simplifies the creation of complicated playbooks and makes them more reusable. The playbook breaking feature allows you to logically divide the playbook into reusable components.
Each role is essentially restricted to a certain capability or desired result.
Creating a new role:
$ ansible-galaxy init demo-role
$ tree demo-role/
manojrole/
├── defaults
│ └── main.yml
├── files ├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md ├── tasks
│ └── main.yml
├── templates ├── tests │ ├── inventory
│ └── test.yml
└── vars
└── main.yml
5. Ansible Vault
Ansible Vault is a feature that allows you to store sensitive data like passwords or keys in encrypted files rather than plaintext in playbooks or roles. These vault files can then be distributed or centralised.
Creating Encrypted Files
ansible-vault create my_vault.yml
Editing Encrypted Files
ansible-vault edit my_vault.yml
Encrypting Unencrypted Files
ansible-vault encrypt my_vault.yml
Decrypting Encrypted Files
ansible-vault decrypt my_vault.yml
Viewing Encrypted Files
ansible-vault view my_vault.yml
Conclusion:
So these are the first few items you should include in your ansible playbook. Ansible can be a lot of fun to learn and use. There's a lot more to it, but this should get you started.
Comments