Ansible
Source: What is Ansible | Ansible Playbook explained | Ansible Tutorial for Beginners - YouTube
What is Ansible
Section titled “What is Ansible”Introduction to Ansible
Section titled “Introduction to Ansible”Tool to automate IT tasks
Open source GPL licensed
Why automate?
Section titled “Why automate?”Save time, reduce errors, ensure consistency
- Solve repetitive and distributed tasks:
- Updates, backups, system reboots, user management
- Manage multiple, distributed systemsm complex systems
- For example, update Docker on 10 machines
- Naive approach: Document through notes, then manually execute
When is Ansible Used? Use Cases
Section titled “When is Ansible Used? Use Cases”- 4 different ways Ansible helps:
- Execute task from one machine
- Config steps with YAML file(s)
- Re-use same YAML files for different environments
- Efficient, reliable and less errors
Ansible supports all infrastructure:
- Operating systems, cloud, on-prem
- Integrations vendor supported
- Agentless, managed remotely from one machine - advantages:
- No deployment or upgrade effort
Ansible Components
Section titled “Ansible Components”Modules and Playbooks
Section titled “Modules and Playbooks”Modules:
- Small programs that do the work. They are sent to the target server.
One specific tasks like:
- Start docker container
- Create/copy file
- Start nginx service
There are modules to work with many systems, some examples:
- Cloud
- Commands
- Database
- Files
- Network
- Security
- Source control
- System
Simple to Understand becase of use of YAML
Section titled “Simple to Understand becase of use of YAML”Ansible uses YAML, making it easier to understand and human readable.
Usage, Playbooks
Section titled “Usage, Playbooks”-
Because modules do specific things, multiple modules will be required for complex tasks.
-
Sequential modules are grouped into tasks. For example:
- Task: rename table, set owner of table, and truncase table.
- Task: create directory, install nginx, start nginx
-
Tasks Configuration
- Hosts: where to run
- Vars: variables
- remoteuser: user to run as
- Module name: what to run
- Arguments: what to do
Indentation is used in YAML to show hierarchy and is strict.
-
Plays and Playbooks
Play = combination of above: hosts, vars, remoteuser, tasks with modules and arguments. Should be named after the sequential tasks
Playbook = 1 or more plays
- How and in which order
- At what time and where (which hosts)
- What modules should be executed
Example Playbook:
hosts: webserversremote_user: root# Play for Webserverstasks:- name: create directory for nginxfile:path: /etc/nginxstate: directory- name: install nginx latest versionapt:name: nginxstate: latest- name: start nginxservice:name: nginxstate: started# Play for Database- hosts: databasesremote_user: roottasks:- name: rename tablemysql_db:name: old_tablerename: new_table- name: set owner of tablemysql_user:name: new_tablepriv: '*.*:ALL'- name: truncate tablemysql_db:name: new_tablestate: importtarget: /tmp/new_table.sql -
Host Definition
Set in Hosts File or Inventory = all machines involed in task executions
- Includes IPs or hostnames
- Can be grouped like webservers, databases
- Can be any type of server (cloud, virtual, bare metal)
For example:
10.24.0.100[webservers]10.24.0.1web1.myserver.com[databases]10.24.1.4db1.myserver.com
Ansible with Docker and Other Environments
Section titled “Ansible with Docker and Other Environments”- Dockerfile to prepare application environment, for example config file, set log directory, env varaibles, app.jar, start script
Using Ansible, use the same configuration for the Docker container, but use it also on:
- Docker / Vagrant container
- Cloud instance
- Bare metal
Ansible allows to reproduce the application in any environment
Ansible can also manage the container on the host level, for example with storage and network.
Ansible Tower
Section titled “Ansible Tower”- UI Dashboard from Redhat to centrally store and manage tasks, collaborate with other, manage inventories
Alternatives to Ansible like Puppet and Chef
Section titled “Alternatives to Ansible like Puppet and Chef”Ansible | Puppet and Chef | Comment |
---|---|---|
YAML | Ruby | YAML is easier to learn |
Agentless | Agent based, installation | Less deployment required |
See Also
Section titled “See Also”- Ansible Vault - Ansible Vault
- DevOps Bootcamp with Nana - DevOps Bootcamp Series with Nana Janashia
- DevOps - DevOps - Development and Operations - Development and Operations
- Terraform - Terraform - infrastructure as code - infrastructure as code (IaC)