= ficheros y openssl
== 3:27 Ficheros y OpenSSL (I)
permite trabajar con ficheros, plantillas y directorios
* acl : establece y obtiene información de la listas de control de acceso
* archive : crea un fichero comprimido a partir de una lista de ficheros o estructura de directorios
* assemble : asambla un fichero de configuración desde fragmentos
* blockinfile : Inserta/Actualiza/Elimina un bloque de texto de un fichero
* copy : copiar ficheros a ubicaciones remotas (desde servidor Ansible -> nodo remoto)
* fetch : copiar del nodo remoto al servidor
* file : establece atributos a ficheros
* find : devuelve una listsa de ficheros a partir de un patrón
* inifile : manejo de ficheros INI
* iso_extract : extrae ficheros de una imagen ISO
* lineinfile : asegura que una línea está en un fichero o reemplaza la misma con el uso de REGEX
* patch : aplica parches usando GNU/Patch
* replace : reemplaza las coincidencias de un texto por otro
* stat : obtiene información del fichero o del FS
* synchronize : rsync
* tempfile : crear ficheros/directorios temporales
* template : uso de plantillas
* unarchive : extraer ficheros (en remoto)
* xatrr : atributos extendidos
los módulos para OpenSSL:
* openssl_privatekeys : generar claves privadas
* openssl_publickey : generar claves públicas
=== copy
[[https://docs.ansible.com/ansible/latest/modules/copy_module.html#copy-module]]
* obligatorios
* dest = /path/destino
* opcionales
* backup = yes/no
* content = "contenido"
* force = yes/no
* owner = usuario
* group = grupo
* mode = modo
* src = /path/origen
- name: copiar configuración
copy: src=apache2.conf dest=/etc/apache2/apache2.conf owner=www-data group=www-data
- name: crear contenido en fichero
copy: content="Mi contenido en un fichero" dest=/etc/mi.conf
=== template
[[https://docs.ansible.com/ansible/latest/modules/template_module.html#template-module]]
* obligatorios
* dest = /path/destino
* src = /path/origen
* opcionales
* backup = yes/no
* force = yes/no
* owner = usuario
* group = grupo
* mode = modo
- name: copiar pantilla de configuración
template: src=apache2.conf.j2 dest=/etc/apache2/apache2.conf backup=yes
=== file
[[https://docs.ansible.com/ansible/latest/modules/file_module.html#file-module]]
* obligatorios
* path = /path/al/fichero
* opcionales
* backup = yes/no
* force = yes/no
* owner = usuario
* group = grupo
* mode = modo
* state
* file
* link
* directory
* hard
* touch
* absent
...
tasks:
- name: propiedades del fichero
file: path="/path/al/fichero" backup=yes mode="777"
- name: borrar el fichero
file: path="/path/al/fichero" backup=yes absent=yes
- name: verificar que directorio existe
file:
path: "/path/to/directory"
state: directory
owner: root
group: systemd-journal
mode: 2755
notify: reiniciar_journald
handlers:
- name: reiniciar_journald
service: name=systemd-journald state=restarted
== 3:28 Ficheros y OpenSSL (II)
=== stat
[[https://docs.ansible.com/ansible/latest/modules/stat_module.html#stat-module]]
* obligatorio
* path = /path/al/fichero
* opcional
* get_attributes = True / False
* get_checksum = True / False
* get_md5 = True / False
* get_mime = True / False
- name: obtener datos de fichero
stat: path="/path/to/file"
register: datos_fichero
- name: mostrar información
debug: var=datos_fichero
- name: en condicional
debug: msg="es diretorio"
when: datos.stat.isdir # o cualauier otro atributo
=== fetch
[[https://docs.ansible.com/ansible/latest/modules/fetch_module.html#fetch-module]]
* obligatorio
* src : /path/to/file en nodo remoto
* dest : server Ansible
* opcional
* fail_on_missing = yes/no
* flat = yes/NO : recrea la estructura de directorios de **src** en **dest**
- name: copiar configuración red
fetch: src=/etc/network/interfaces dest=/tmp/backup
=== unarchive
[[https://docs.ansible.com/ansible/latest/modules/unarchive_module.html#unarchive-module]]
* obligatorio
* src
* dest
* opcional
* owner
* group
* mode
* remote_src = true / FALSE
* list_files = yes / NO -> lista los ficheros que se han extraído (se puede guardar en **registry**)
- name: copiar y extraer fichero en remoto
unarchive: src= dest=/opt/fichero
-name: extraer en remoto fichero ya existente allí
unarchive: src= dest=/opt/fichero remote_src=true
=== lineinfile (!)
[[https://docs.ansible.com/ansible/latest/modules/lineinfile_module.html#lineinfile-module]]
* obligatorio
* line = "texto"
* dest = /path/to/file
* en versiones más modernas, **path**
* opcionales
* owner
* group
* mode
* backup = yes / NO
* insertafter = REGEX
* insertbefore = REGEX
* regexp = REGEX
* state = present / absent : ??
- name: deshabilita SELinux
lineinfile: dest=/etc/selinux/config regexp='^SELINUX=' line='SELINUX=disabled'
- name: eliminar la línea que permite al grupo wheel del fichero de configuración de sudoers
lineinfile: dest=/etc/sudoers state=absent regexp="^wheel"
- name: añadir antes de una línea
lineinfile: dest=/etc/apache2/ports.conf regexp='^Listen' insertafter="Listen 80" line="Listen 8080"
=== blockinfile (!)
[[https://docs.ansible.com/ansible/latest/modules/blockinfile_module.html#blockinfile-module]]
* obligatorio
* block = "texto"
* dest = /path/to/file
* opcional
* owner
* group
* mode
* backup = yes / NO
* insertafter = REGEX
* insertbefore = REGEX
* marker = REGEX
* state = present / absent : ??
- name: asegurar que el texto está en el fichero
dest: /etc/ssh/sshd_config
block: |
Match user monitor
Password Authentication no
=== openssl_privatekey (!)
[[https://docs.ansible.com/ansible/latest/modules/openssl_privatekey_module.html#openssl-privatekey-module]]
* obligatorio
* path = /path/to/file
* opcional
* force = true / false
* size = 4096
* state = present / absent
* type = RSA / DSA
- name: instalar módulo phyton requerido
apt: name=phyton-openssl state=latest
- name: generar clave privada
openssl_privatekey: path=/etc/ssl/private/private.pem
=== openssl_publickey (!)
[[https://docs.ansible.com/ansible/latest/modules/openssl_publickey_module.html#openssl-publickey-module]]
* obligatorio
* path = /path/to/file
* privatekey_path = /path/to/privatekey
* opcional
* force = false / true
* state = present / absent
- name: generar clave pública
openssl_publickey:
path: /etc/ssl/private/public.key
privatekey_path: /etc/ssl/private/private.pem