Diferències
Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.
| Ambdós costats versió prèvia Revisió prèvia | |||
| info:cursos:pue:devops2022:s12 [09/02/2022 09:21] – mate | info:cursos:pue:devops2022:s12 [21/03/2022 13:47] (actual) – mate | ||
|---|---|---|---|
| Línia 1: | Línia 1: | ||
| = DevOps Sesión 12 (2022-03-21) | = DevOps Sesión 12 (2022-03-21) | ||
| == Documentación relacionada | == Documentación relacionada | ||
| - | == Clase | + | <callout type=" |
| - | == TODO | + | * ./4-Topic 704 Configuration Management |
| - | <callout type=" | + | * ./Material Curso Ansible/ |
| + | * ./Material Curso Ansible/ | ||
| + | * ./Material Curso Ansible/ | ||
| + | </ | ||
| + | |||
| + | == ansible.cfg | ||
| + | <callout type=" | ||
| + | * ./4-Topic 704 Configuration Management | ||
| + | * ./Material Curso Ansible/ | ||
| + | * ./ | ||
| + | </ | ||
| + | * 4 secciones básicas | ||
| + | * defaults | ||
| + | * inventory | ||
| + | * sudo_user | ||
| + | * forks | ||
| + | * timeout | ||
| + | * log_path | ||
| + | * nocows | ||
| + | * privilege_escalation | ||
| + | * become | ||
| + | * become_method | ||
| + | * become_user | ||
| + | * ssh_connection | ||
| + | * ssh_args | ||
| + | * control_path | ||
| + | * scp_if_ssh | ||
| + | * colors | ||
| + | === inventario | ||
| + | * mantener ficheros separados (para evitar errores) por entornos (buena práctica | ||
| + | * usar **-i** para indicar el inventario | ||
| + | * palabra reservada **all** | ||
| + | === ayuda | ||
| + | * ansible-doc -l | ||
| + | * ansible-doc copy | ||
| + | * ansible-doc -s copy | ||
| + | === cowsay enable | ||
| + | <code bash>yum install cowsay -y | ||
| + | sudo vi / | ||
| + | # 218: nocows = 0 | ||
| + | # 224: cow_selection = random - otros animales | ||
| + | </ | ||
| + | |||
| + | == ansible-playbook | ||
| + | * %%--%%step | ||
| + | |||
| + | == modules | ||
| + | <callout type=" | ||
| + | * ./4-Topic 704 Configuration Management | ||
| + | * ./Material Curso Ansible/ | ||
| + | </ | ||
| + | === copy | ||
| + | * [[https:// | ||
| + | * <code yaml; playbooks_resueltos/ | ||
| + | - name: Ejemplos de modulos de ficheros y openssl | ||
| + | hosts: clientes | ||
| + | remote_user: | ||
| + | tasks: | ||
| + | - name: Crear un firchero con contenido especificado | ||
| + | copy: content=" | ||
| + | - name: Propiedades Fichero | ||
| + | file: path="/ | ||
| + | - name: copia archivo / | ||
| + | copy: | ||
| + | src: / | ||
| + | dest: /tmp | ||
| + | owner: root | ||
| + | group: root | ||
| + | mode: ' | ||
| + | backup: yes | ||
| + | ... | ||
| + | </ | ||
| + | === file | ||
| + | * mcedit (paquete mc) | ||
| + | * crea directorio **/ | ||
| + | <code yaml; modulo_file_directorio.yml> | ||
| + | --- | ||
| + | - name: Ejemplos de modulos de ficheros y openssl | ||
| + | hosts: clientes | ||
| + | remote_user: | ||
| + | tasks: | ||
| + | - name: Creacion de un directorio | ||
| + | file: | ||
| + | path: "/ | ||
| + | state: directory | ||
| + | owner: root | ||
| + | group: systemd-journal | ||
| + | mode: 2755 | ||
| + | notify: reiniciar_journald | ||
| + | handlers: | ||
| + | - name: reiniciar_journald | ||
| + | service: name=systemd-journald state=restarted | ||
| + | ... | ||
| + | </ | ||
| + | * handlers: tareas que responden a una notificación enviada por otras tareas | ||
| + | * **notify** le indica al **handler** que se tiene que ejecutar | ||
| + | * si varias tareas llaman al mismo handler, solo se ejecuta una vez, después de la última tarea que lo llame. | ||
| + | * puede haber 2 handlers que se llamen igual, con un solo **notify** se ejecutaran los dos | ||
| + | |||
| + | |||
| + | === delete | ||
| + | <code yaml; modulo_file_delete.yml> | ||
| + | --- | ||
| + | - name: Ejemplos de modulos de ficheros y openssl | ||
| + | hosts: clientes | ||
| + | remote_user: | ||
| + | tasks: | ||
| + | - name: Crear un firchero con contenido especificado | ||
| + | copy: content=" | ||
| + | - name: Propiedades Fichero | ||
| + | file: path="/ | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | === locale | ||
| + | <code bash> | ||
| + | ansible all -a " | ||
| + | ansible all -a " | ||
| + | ansible all -a " | ||
| + | ansible all -a " | ||
| + | ansible all -a " | ||
| + | |||
| + | ansible all -a " | ||
| + | |||
| + | ansible clientes -a "yum install httpd -y" | ||
| + | </ | ||
| + | |||
| + | === fetch | ||
| + | * recoge/ | ||
| + | <code yaml; modulo_lineinfile.yml> | ||
| + | --- | ||
| + | - name: Ejemplo de modulo fetch | ||
| + | hosts: clientes | ||
| + | remote_user: | ||
| + | tasks: | ||
| + | - name: Utilizando fetch para copia de seguridad | ||
| + | fetch: | ||
| + | src=/ | ||
| + | dest=/ | ||
| + | - name: Utilizando fetch para copia de seguridad con flat solo copia el fichero, el destino tiene que ser un fichero | ||
| + | #fetch: src=/ | ||
| + | fetch: src=/ | ||
| + | ... | ||
| + | </ | ||
| + | * flat: no recrea la ruta de la ubicación del fichero y lo deja directamente en el directorio que le hemos indicado | ||
| + | |||
| + | === lineinfile | ||
| + | se utiliza para la configuración de ficheros, podremos asegurarnos de que una línea en particular esté en un archivo, o reemplace una línea existente usando una expresión regular, antes o después de la expresión, que este presente o que no este presente. | ||
| + | <code yaml; modulo_lineinfile.yml> | ||
| + | --- | ||
| + | - name: Ejemplo de modulo fetch | ||
| + | hosts: clientes | ||
| + | remote_user: | ||
| + | tasks: | ||
| + | - name: Utilizando modulo lineinfile para modificar la configuracion de selinux | ||
| + | lineinfile: path=/ | ||
| + | - name: Eliminar una linea de un fichero, en este caso del arvhivo sudoers la linea de %wheel | ||
| + | lineinfile: dest=/ | ||
| + | - name: Añadir una linea de configuracion al archivo de apache httpd.conf | ||
| + | lineinfile: | ||
| + | path: / | ||
| + | regexp: ' | ||
| + | insertafter: | ||
| + | line: ' | ||
| + | - name: Añadir una linea despues de la expresinon buscada en un archivo | ||
| + | lineinfile: | ||
| + | path: / | ||
| + | regexp: '^# port for http' | ||
| + | insertbefore: | ||
| + | line: '# port for http by default' | ||
| + | |||
| + | - name: touch a file, using symbolic modes to set the permissions (equivalent to 0644) | ||
| + | file: | ||
| + | path: / | ||
| + | state: touch | ||
| + | mode: " | ||
| + | |||
| + | - name: Add a line to a file if it does not exist, without passing regexp | ||
| + | lineinfile: | ||
| + | path: / | ||
| + | line: ' | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | === install docker | ||
| + | <code yaml ; playbook_centos_install_docker.yaml> | ||
| + | --- | ||
| + | - name: Install docker Centos | ||
| + | gather_facts: | ||
| + | hosts: clientes | ||
| + | user: root | ||
| + | |||
| + | tasks: | ||
| + | - name: Install yum utils | ||
| + | yum: | ||
| + | name: yum-utils | ||
| + | state: latest | ||
| + | |||
| + | - name: Install device-mapper-persistent-data | ||
| + | yum: | ||
| + | name: device-mapper-persistent-data | ||
| + | state: latest | ||
| + | |||
| + | - name: Install lvm2 | ||
| + | yum: | ||
| + | name: lvm2 | ||
| + | state: latest | ||
| + | |||
| + | - name: Add Docker repo | ||
| + | get_url: | ||
| + | url: https:// | ||
| + | dest: / | ||
| + | become: yes | ||
| + | |||
| + | - name: Enable Docker Edge repo | ||
| + | ini_file: | ||
| + | dest: / | ||
| + | section: ' | ||
| + | option: enabled | ||
| + | value: 0 | ||
| + | become: yes | ||
| + | |||
| + | - name: Enable Docker Test repo | ||
| + | ini_file: | ||
| + | dest: / | ||
| + | section: ' | ||
| + | option: enabled | ||
| + | value: 0 | ||
| + | become: yes | ||
| + | |||
| + | - name: Install Docker | ||
| + | package: | ||
| + | name: docker-ce | ||
| + | state: latest | ||
| + | become: yes | ||
| + | |||
| + | - name: Install Docker Compose | ||
| + | package: | ||
| + | name: docker-compose | ||
| + | state: latest | ||
| + | become: yes | ||
| + | |||
| + | |||
| + | - name: Start Docker service | ||
| + | service: | ||
| + | name: docker | ||
| + | state: started | ||
| + | enabled: yes | ||
| + | become: yes | ||
| + | |||
| + | - name: Add user vagrant to docker group | ||
| + | user: | ||
| + | name: vagrant | ||
| + | groups: docker | ||
| + | append: yes | ||
| + | become: yes | ||
| + | |||
| + | - name: Utilizando modulo lineinfile para modificar la configuracion de selinux | ||
| + | lineinfile: path=/ | ||
| + | |||
| + | - name: Stop and disabled firewalld | ||
| + | service: | ||
| + | name: firewalld | ||
| + | state: stopped | ||
| + | enabled: no | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | === when_facts | ||
| + | <callout type=" | ||
| + | * ./4-Topic 704 Configuration Management | ||
| + | * ./Material Curso Ansible/ | ||
| + | * ./Specific Distribution ansible facts.txt | ||
| + | * [[https:// | ||
| + | </ | ||
| + | <code yaml; modulo_when_facts.yaml> | ||
| + | --- | ||
| + | - hosts: clientes | ||
| + | user: root | ||
| + | |||
| + | tasks: | ||
| + | |||
| + | - name: restart apache en debian | ||
| + | service: | ||
| + | name: apache2 | ||
| + | state: started | ||
| + | enabled: yes | ||
| + | when: ansible_distribution == ' | ||
| + | |||
| + | - name: restart httpd en centos | ||
| + | service: | ||
| + | name: httpd | ||
| + | state: started | ||
| + | enabled: yes | ||
| + | when: ansible_distribution == ' | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | <code yaml> | ||
| + | --- | ||
| + | - hosts: clientes | ||
| + | user: root | ||
| + | |||
| + | tasks: | ||
| + | - name: Install apache para debian | ||
| + | apt: | ||
| + | name: {{ item }} | ||
| + | state: latest | ||
| + | with_items: | ||
| + | - apache2 | ||
| + | - php | ||
| + | when: ansible_distribution == ' | ||
| + | |||
| + | - name: Install httpd para centos | ||
| + | yum: | ||
| + | name: {{ item }} | ||
| + | state: latest | ||
| + | with_items: | ||
| + | - httpd | ||
| + | - httpd-devel | ||
| + | - php* | ||
| + | - mariadb* | ||
| + | when: ansible_distribution == ' | ||
| + | |||
| + | - name: restart apache en debian | ||
| + | service: | ||
| + | name: apache2 | ||
| + | state: started | ||
| + | enabled: yes | ||
| + | when: ansible_distribution == ' | ||
| + | |||
| + | - name: restart httpd en centos | ||
| + | service: | ||
| + | name: httpd | ||
| + | state: started | ||
| + | enabled: yes | ||
| + | when: ansible_distribution == ' | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | === modulo_comandos_script.yml | ||
| + | |||
| + | == loop | ||
| + | * [[https:// | ||
| + | * **with_items** se eliminará este formato, usar **loop** | ||
| + | <code yaml> | ||
| + | - name: paquetes | ||
| + | yum: | ||
| + | name: " | ||
| + | state: present | ||
| + | loop: | ||
| + | - httpd | ||
| + | - php* | ||
| + | </ | ||
| + | |||
| + | == expect | ||
| + | <callout type=" | ||
| + | * ./4-Topic 704 Configuration Management | ||
| + | * ./module expect ansible cambiar password a usuarios linux.txt | ||
| + | * [[https:// | ||
| + | </ | ||
| + | * librerias/ | ||
| + | <code bash> | ||
| + | ansible cliente12 -a "yum install pexpect -y" | ||
| + | ansible cliente12 -a "yum -y install python-pip -y" | ||
| + | ansible cliente12 | ||
| + | </ | ||
| + | <code yaml; modulo_expect.yml> | ||
| + | --- | ||
| + | - name: Ejecuta un comando y responde a las solicitudes | ||
| + | hosts: cliente12 | ||
| + | remote_user: | ||
| + | tasks: | ||
| + | # - name: Para ejecutar este modulo tenemos que instalar el paquete pexpect | ||
| + | # yum: name=pexpect state=latest | ||
| + | |||
| + | - name: Case insensitve password string match | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | ... | ||
| + | </ | ||
| + | * The question, or key, under responses is a python regex match. Case insensitive searches are indicated with a prefix of ?i. | ||
| + | |||
| + | |||
| + | == docker | ||
| + | <callout type=" | ||
| + | * ./4-Topic 704 Configuration Management | ||
| + | * ./Material Curso Ansible/ | ||
| + | * [[https:// | ||
| + | * Laboratorio ansible docker-network.txt linea 71 | ||
| + | </ | ||
| + | <code properties ; / | ||
| + | ... | ||
| + | [docker] | ||
| + | 192.168.33.11 | ||
| + | ... | ||
| + | </ | ||
| + | <code bash> | ||
| + | ansible docker -a "yum -y install python2-pip -y" | ||
| + | ansible docker -a "pip install docker-py" | ||
| + | |||
| + | ansible docker | ||
| + | </ | ||
| + | <code yaml; docker-network.yml> | ||
| + | --- | ||
| + | - name: Crear red y conectar el contedor ansible-web | ||
| + | hosts: docker | ||
| + | remote_user: | ||
| + | tasks: | ||
| + | - name: crear red llama da ansible | ||
| + | docker_network: | ||
| + | name: ansible | ||
| + | state: present | ||
| + | connected: | ||
| + | - ansible-web | ||
| + | ... | ||
| + | </ | ||
| + | <code bash> | ||
| + | ansible docker -a " | ||
| + | ansible docker -m shell -a " | ||
| + | </ | ||
| + | |||
| + | === volumenes | ||
| + | <code yaml; playbook_modulo_cloud_docker2.yml> | ||
| + | --- | ||
| + | - name: Playbook docker con volumenes, | ||
| + | hosts: docker | ||
| + | remote_user: | ||
| + | tasks: | ||
| + | #- pip: name=pushbullet.py | ||
| + | |||
| + | - name: Creacion de un directorio /web en los servidores de docker para persistir un volumen | ||
| + | file: | ||
| + | path: "/ | ||
| + | state: directory | ||
| + | owner: root | ||
| + | group: root | ||
| + | mode: 0755 | ||
| + | - name: crear un fichero con contenido específico | ||
| + | copy: content=" | ||
| + | |||
| + | |||
| + | # Reinicio de servicios de docker | ||
| + | # - name: Restart docker | ||
| + | # | ||
| + | |||
| + | # Example action to start service docker, if not running | ||
| + | - service: | ||
| + | name: docker | ||
| + | state: started | ||
| + | |||
| + | - name: pull image | ||
| + | docker_image: | ||
| + | name: agarciaf/ | ||
| + | # state: present | ||
| + | |||
| + | - name: create docker container | ||
| + | docker_container: | ||
| + | name: intranet2 | ||
| + | image: agarciaf/ | ||
| + | ports: | ||
| + | - " | ||
| + | volumes: | ||
| + | - "/ | ||
| + | state: started | ||
| + | ... | ||
| + | |||
| + | </ | ||
| + | |||
| + | == kubernetes | ||
| + | <callout type=" | ||
| + | * ./4-Topic 704 Configuration Management/ | ||
| + | * [[https:// | ||
| + | </ | ||
| + | |||
| + | |||
| + | == roles | ||
| + | * fraccionar un playbook en diferentes ficheros para facilitar su implementeación | ||
| + | |||
| + | == pushbullet/ | ||
| + | * notificaciones a los teléfonos a través de app | ||