Loops Flashcards
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 users in the appropriate groups
hosts: all
tasks:- 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
- name: include passwords from vault
include_vars:
file: passwords.yml
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 zip
hosts: localhost
become: falsevars:
source: /file
zipfile: /file.tar.gz
zipformat: ziptasks:- 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: archive
Como se usa una condicion en un loop
- name: name of the play
hosts: all
vars:
my_service: httpdtasks:- name: “{{ mr_service }} package is installed” yum: name: “{{ my_service }}” when: my_service is defined
Condicionales 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_distros
Ejemplo 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
- name: Indtall the sudo configuration
copy:
src: support.sudo
dest: /etc/sudoers.d/support
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
- name:
lineinfile:
state: present
dest: /etc/ssh/ssh_config
line: AllowGroups wheel
notify: Restart the ssh daemon