User Tools

Site Tools


informatica:linux:docker:ansible

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
informatica:linux:docker:ansible [2017/02/16 22:50] joseinformatica:linux:docker:ansible [2019/09/09 13:54] jose
Line 1: Line 1:
 +<code>
 +├── 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
 +</code>
 +Cada role lo creamos con:
 +  ansible-galaxy init <role>
 +
 +
 +
 +
 https://serversforhackers.com/an-ansible-tutorial https://serversforhackers.com/an-ansible-tutorial
  
Line 10: Line 50:
   docker run --name ansible-1 --net ansible --ip 172.20.0.101 --hostname ansible1 -ti 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   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
 +<code>
 +
 +172.20.0.101 | SUCCESS => {
 +    "changed": false, 
 +    "ping": "pong"
 +}
 +172.20.0.102 | SUCCESS => {
 +    "changed": false, 
 +    "ping": "pong"
 +}
 +</code>
  
      
 +====== PLAYBOOKS ======
 +Para ejecutar un playbook creamos el fichero yml y ejecutamos con -s si queremos sudo:
 +  ansible-playbook <-s> playbook.yml
 +
 +
 +nginx.yml
 +
 +<code>
 +---
 +- hosts: prueba
 +  tasks:
 +   - name: Install Nginx
 +     apt: pkg=nginx state=installed update_cache=true
 +
 +</code>
 +
 +Ejemplo:
 +<code>
 +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   
 +
 +</code>
 +
 +====== Tarea dependiente ======
 +Solo ejecuta la segunda tarea cuando ejecuta la primera. En este caso solo arranca nginx si lo instala:
 +<code>
 +- 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
 +</code>
 +
 +====== 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