Como se crea un looop en playbook para crear usuarios
—-
- name: Create users
hosts: servera.lab.example.com
vars:
myusers:
- fred
- barney
- wilma
- betty
tasks:
- name: Create users
user:
name: “{{ item }}”
state: present
loop: “{{ myusers }}”
...Crear grupos con usuarios playbook
Método with_dict: loop
—-
- name: create groups
group:
name: “{{ item }}”
loop:
- flintstones
- rubbles
- name: create users in their groups
user:
name: “{{ item.name }}”
groups: “{{ item.groups }}”
with_dict:
- { name: ‘fred’, groups: ‘flintstones’ }
- { name: ‘wilma’, groups: ‘flintstones’ }
- { name: ‘barney’, groups: ‘rubbles’ }
- { name: ‘betty’, groups: ‘rubbles’ } ...Como buscar ayuda de un módulo
ansible-doc -t lookup
Como se agregan variables se ansible vault y como se usa en playbook
Como se usa un nested loop
- name: give Beatles access to their dbs
mysql_user:
name: “{{ item[0] }}”
priv: “{{ item[1] }}.*:ALL”
append_privs: yes
password: “{{ db_pass }}”
with_nested:
- “{{ beatles}}”
- “{{ category_db }}”Como se usa el módulo archive en playbook
- name: create tar
archive:
format: “{{ zipformat }}”
path: “{{ source }}”
dest: “{{ zipfile }}”
- name: check if tar exists
stat:
path: “{{ zipfile }}”
register: archive
- name: show the archive dictionary
debug:
var: archiveComo se usa una condicion en un loop
- name: “{{ mr_service }} package is installed”
yum:
name: “{{ my_service }}”
when: my_service is definedCondicionales para where sintaxis
Var == “variable” Var == 99 Var < 99 Var > 99 Var <= 99 Var >= 99 Var != 99 Var is defined Var is not defined Var ( true / false ) Var in Var2
Ejemplo de condición si ansible_distribution está dentro de supported_distros
—-
- name: demonstrate in keyword
hosts: all
gather_facts: yes
vars:
supported_distros:
- RedHat
- Fedora
tasks:
- name: install httpd using yum
yum:
name: http
state: present
when: ansible_distribution in supported_distrosEjemplo de multiple or y and conditions
when: ansible_distribution == “RedHat” or ansible_distribution == “Fedora”
when: ansible_distribution_version == “7.5” and ansible_kernel == “3.10.0-327.el7.x86_64”
Como se coloca una condición larga en una sola linea
when: >
( ansible_distribution == “RedHat” and ansible_distribution_major_version == “7” )
or
( ansible_distribution == “Fedora” and ansible_distribution_major_version == “28” )
Como se realiza una validación. Previa de los filesystem para evaluar que haya Espacio disponible antes de instalar un paquete
—-
_ name:
Lo mi se usa el
Módulo copy para copiar un archivo
Como se usa el módulo authorized key de ssh
- name: install the ssh key
authorized_key:
manage_dir: yes
user: support
key: “{{ lookup(‘file’ , ’id_rsa.pub’) }}”Como se usa el módulo lineinfile
Como se usa el parámetro notify de playbook para tareas
Se usa para notificar o lanzar tareas posteriores de otros módulos, solo
Ejecutan si la tarea resulta en change state
tasks:
- name: Disallow password authentication
lineinfile:
state: present
dest: /etc/ssh/ssh_config
line: PasswordAuthentication no
notify: Restart the ssh daemon
handlers:
- name: Restart the ssh daemon
service:
name: sshd
state: restartedComo se forza a qué se ejecute un handler
Parámetro
- name: play level
force_handlers: true
Como registras un shellscript como fallido con el shell module
tasks:
- name: run user creation script
shell: /usr/local/bien/create_users.sh
register: command_result
Failed_when: “‘Password missing’ in command_result.stdout”
Como se usa el fail module
tasks:
- name: run user creation script
shell: /usr/local/bien/create_users.sh
register: command_result
ignore_errors: yes
- name: report script failure
fail:
msg: “the password is missing in the output”
when: “‘Password missing’ in the command_result.stdout”
Como se reporta como changed en shell module ejemplo
Como
Se logra especificar un ejemplo de colocar cuando esté en estado changed
tasks:
- shell:
cmd: /usr/local/bin/upgrade-database
register: command_result
changed_when: “‘Success’ in command_result.stdout”
notify:
- restart_database
handlers:
- name: restart_database
service:
name: mariadb
state: restarted
Como se utilizan los. Bloques mostrar ejemplo en playbook
Como funciona rescue y always en un bloque playbook
Como se usa ignore errors en un playbook
task:
- name: install {{ web_package }} package
yum:
name: “{{ web_package }}”
state: present
ignore_errors: yes
…