Jouer avec les facts et les variables

Source

Source : https://github.com/goffinet/guide-ansible-playbooks/tree/master/ansible-linux/12-jouer-facts

Objectifs

1. Valeurs de retour

Valeurs de retour

2. Définir des variables

  • vars et vars_files
  • register, setfact

3. Créer un fichier de données

Le fichier data.yml par exemple.

---
group: "omega"
users:
  - name: alfa
    password: testtest
  - name: beta
    password: testtest

4. Utiliser les données

- name: print out data.yml
  hosts: localhost
  gather_facts: False
  vars_files:
    - data.yml
  tasks:
  - debug:
      msg: "{{ item.name }} with weak password {{ item.password }}"
    loop: "{{ users }}"
  - debug:
      msg: "{{ group }}"

5. Facts

---
- name: print out operating system
  hosts: all
  gather_facts: True
  tasks:
    - name: "facts"
      setup:
      register: output
    - name: "print output"
      debug:
        msg: "{{ output }}"

6. Obtenir la version de sa distribution

---
- name: print out distribution
  hosts: all
  gather_facts: True
  tasks:
    - name: "setup facts"
      setup:
      register: output
    - name: "print output"
      debug:
       msg: " {{ ansible_distribution }} {{ ansible_distribution_version }}"

7. Obtenir son adresse ip publique

---
- name: "print ipv4 public address"
  hosts: all
  gather_facts: True
  tasks:
    - uri:
        url: https://ipinfo.io/ip
        return_content: yes
      register: ipinfo_content
    - set_fact:
        ip_address: "{{ ipinfo_content.content | replace('\n', '') }}"
    - set_fact:
        dns_name: "{{ ip_address }}.xip.io"
    - debug:
        msg: "{{ ip_address }} {{ dns_name }} {{ ansible_hostname}} "

8. Exercice

Mettre à jour automatiquement et proprement le fichier /etc/host (avec backup) avec le l’adresse et le nom de chaque hôte sur le contrôleur.

Indications :

9. Sortie brute à traiter

---
- name: "print df -Th"
  hosts: all
  gather_facts: True
  tasks:
    - shell: df -Th
      register: df_output
    - set_fact:
        df: "{{ df_output }}"
    - debug:
        msg: "{{ df.stdout_lines | list }}"

Exercice : Sortie à traiter.

tests

10. Prompts

Prompts

Le module prompts permet valoriser des variables en interagissant avec l’utilisateur.

---
- hosts: localhost
  vars_prompt:
    - name: "name"
      prompt: "what is your name?"
      private: no
    - name: "favcolor"
      prompt: "what is your favorite color?"
      private: no
    - name: "my_password"
      prompt: "Enter password"
      private: yes
      encrypt: "sha512_crypt"
      confirm: yes
      salt_size: 7
  tasks:
    - debug:
        msg: |
             {{ name }} loves {{ favcolor }}
             His password is {{ my_password }}

11. Les conditions

Ansible Conditionals

  tasks:
    - name: "Debian Action"
      debug:
        msg: "{{ ansible_hostname }} Debian host"
      when: ansible_facts['os_family'] == "Debian"
      # note that all variables can be directly in conditionals without double curly braces
  tasks:
    - name: "Action on CentOS 7 and Debian 9 systems"
      debug:
        msg: "{{ ansible_hostname }} CentOS 7 and Debian 9 systems"
      when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "7") or
            (ansible_facts['distribution'] == "Debian" and ansible_facts['distribution_major_version'] == "9")
  tasks:
    - name: "shut down CentOS 7 systems"
      debug:
        msg: "{{ ansible_hostname }} CentOS 7"
      when:
        - ansible_facts['distribution'] == "CentOS"
        - ansible_facts['distribution_major_version'] == "7"
  tasks:
    - command: /bin/false
      register: result
      ignore_errors: True

    - command: /bin/something
      when: result is failed

  # In older versions of ansible use ``success``, now both are valid but succeeded uses the correct tense.
    - command: /bin/something_else
      when: result is succeeded

    - command: /bin/still/something_else
      when: result is skipped
  tasks:
    - shell: echo "only on Red Hat 6, derivatives, and later"
      when: ansible_facts['os_family'] == "RedHat" and ansible_facts['lsb']['major_release']|int >= 6