User Tools

Site Tools


informatica:linux:docker:ansible
proyecto
├── inventories
│   ├── dev
│   │   ├── group_vars
│   │   ├── hosts
│   │   └── vars
│   ├── pre
│   │   ├── group_vars
│   │   ├── hosts
│   │   └── vars
│   └── pro
│       ├── group_vars
│       ├── hosts
│       └── vars
├── main.yml
└── roles
    └── create_user
        ├── README.md
        ├── defaults
        │   └── main.yml
        ├── files
        ├── handlers
        │   └── main.yml
        ├── meta
        │   └── main.yml
        ├── tasks
        │   └── main.yml
        ├── templates
        ├── tests
        │   ├── inventory
        │   └── test.yml
        └── vars
            └── main.yml

Cada role lo creamos con:

ansible-galaxy init <role>

https://serversforhackers.com/an-ansible-tutorial

Creamos red para los servidores ansible:

# docker network create --subnet=172.20.0.0/16 ansible

Ejecutamos la primera máquina con ansible:

# docker run --name ansible-master --net ansible --ip 172.20.0.101 --hostname ansiblemaster -ti iwanttobefreak/ansible

Levantamos 2 servers con debian

docker run --name ansible-1 --net ansible --ip 172.20.0.101 --hostname ansible1 -ti debian
docker run --name ansible-2 --net ansible --ip 172.20.0.102 --hostname ansible2 -ti debian

En el servidor de ansible creamos los grupos con las máquinas

/etc/ansible/hosts
[web]
172.20.0.101
172.20.0.102

Probamos si conecta:

ansible web -m ping

Fallará por no tener claves intercambiadas. Las intercambiamos y:

ansible web -m ping

172.20.0.101 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
172.20.0.102 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

PLAYBOOKS

Para ejecutar un playbook creamos el fichero yml y ejecutamos con -s si queremos sudo:

ansible-playbook <-s> playbook.yml

nginx.yml

---
- hosts: prueba
  tasks:
   - name: Install Nginx
     apt: pkg=nginx state=installed update_cache=true

Ejemplo:

root@5b83b804b705:~# ansible-playbook nginx.yml 

PLAY [prueba] ******************************************************************

TASK [setup] *******************************************************************
ok: [172.17.0.3]
ok: [172.17.0.4]

TASK [Install Nginx] ***********************************************************
changed: [172.17.0.4]
changed: [172.17.0.3]

PLAY RECAP *********************************************************************
172.17.0.3                 : ok=2    changed=1    unreachable=0    failed=0   
172.17.0.4                 : ok=2    changed=1    unreachable=0    failed=0   

Tarea dependiente

Solo ejecuta la segunda tarea cuando ejecuta la primera. En este caso solo arranca nginx si lo instala:

- hosts: prueba
  tasks:
   - name: Install Nginx
     apt: pkg=nginx state=installed update_cache=true
     notify:
      - Start Nginx

  handlers:
   - name: Start Nginx
     service: name=nginx state=started

Login con usuario

Deshabilitamos la consulta del fingerprint

# uncomment this to disable SSH key host checking
host_key_checking = False
informatica/linux/docker/ansible.txt · Last modified: 2019/09/09 13:54 by jose