---
title: "Pense-bête Ansible"
summary: "Trucs et astuces pour Ansible"
author:
  - JF
date: 2024-07-19
---

# Pense-bête Ansible

### Ternary pour choisir la valeur d'une variable selon un état vrai ou faux
```yaml
- name: "{{ boolean_var is not defined|ternary('is true', 'is false') }}"
```
*Source : [ansible.builtin.ternary filter – Ternary operation filter](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/ternary_filter.html)*
***
### Renvoi le chemin du fichier à trouver entre deux.
```yaml
- name: Test paths
  tags: always
  ansible.builtin.stat:
    path: "{{ item }}"
  loop:
    - /first/path/to/test
    - /second/path/to/test
  register: paths_stat

- name: Set found path
  ansible.builtin.set_fact:
    postmaster_path: "{{ item.stat.path }}"
  loop: "{{ paths_stat.results }}"
  when: item.stat.exists
```
*Source : [ansible.builtin.stat module – Retrieve file or file system status](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/stat_module.html#ansible-builtin-stat-module-retrieve-file-or-file-system-status)*
***
### Installation de tous les rpms présents dans un répertoire.
#### Exemple de paquets pour RabbitMQ.
```yaml
#rpm_install "../packages/$DISTRIBUTION/rpms_erlang2/*.rpm"
- name: Find rpms to install
  ansible.builtin.find:
    paths: "{{ sc_src_tmp }}/"
    patterns: "*.rpm"
    use_regex: false
    recurse: true
  register: found_rpm_files
- name: found rpm files
  debug:
    var: found_rpm_files.files.path
- name: Setting rpms loop to install
  ansible.builtin.set_fact:
    rpm_list: "{{ found_rpm_files.files | map(attribute='path') | list }}"

- name: Install RabbitMQ rpms
  ansible.builtin.dnf:
    disable_gpg_check: true
    name: '{{ rpm_list }}'
    state: installed
  register: rpms_installed
- debug:
    msg: "Installed rpms: {{ rpms_installed.results }}"
```