> For the complete documentation index, see [llms.txt](https://pascalgolla.gitbook.io/knowledge/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pascalgolla.gitbook.io/knowledge/automation/ansible.md).

# Ansible

Automation tool for configuration management and application deployment

## Introduction

Ansible is an open-source software provisioning, configuration management, and application-deployment tool. A feature of Ansible is that it uses an **agentless architecture.** This means the is no need for a daemon, also meaning no resource is taken when Ansible is not running. Ansible orchestrates a node by installing and running modules on the node temporarily via SSH. \[1]

**Alternative tool**\
[Chef ](https://en.wikipedia.org/wiki/Progress_Chef)| [Puppet](https://en.wikipedia.org/wiki/Puppet_%28software%29) | [CFEngine](https://en.wikipedia.org/wiki/CFEngine)

## What is the use case?

Let's say you have more than one computer you want to access or manage. Do you want to run the same commands on every system or want to configure all systems the same way? Instead of using SSH to get to each server by hand, you only want to type the code once and everything should be executed on all system.

Here you can use Ansible. You want to echo "Hi" on all systems, then type this on your **control node**:

```bash
ansible all -m shell -a "echo Hi"
```

`all` means we want to execute this command on all **managed nodes**. The managed nodes must be configured first in `/etc/ansible/hosts`

`-m shell` means we are using the [shell **module**](https://docs.ansible.com/ansible/latest/modules/shell_module.html). There are several modules to choose from.

`-a` referees to the module arguments, here we want to use the shell to command: "echo Hi"

### What about more complex commands?

Instead of typing all commands one after another, you can use an **Ansible Playbook**. Here you can write down a task list, of what would be executed. Here is an example of a playbook.

{% code title="ansible-playbook.yml" %}

```yaml
---
- hosts: webservers
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root

  tasks:
  - name: ensure apache is at the latest version
    yum:
      name: httpd
      state: latest
      
  - name: write the apache config file
    template:
      src: /srv/httpd.j2
      dest: /etc/httpd.conf
    notify:
    - restart apache
    
  - name: ensure apache is running
    service:
      name: httpd
      state: started
      
  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted
```

{% endcode %}

## Resources

[Ansible Documentation](https://docs.ansible.com/ansible/latest/index.html) |

## List of sources

**\[1] wikipedia**.org - [https://en.wikipedia.org/wiki/Ansible\_(software)](https://en.wikipedia.org/wiki/Ansible_%28software%29)
