82 lines
2.3 KiB
Markdown
82 lines
2.3 KiB
Markdown
---
|
||
title: "Pense-bête Ansible"
|
||
summary: "Trucs et astuces pour Ansible"
|
||
author:
|
||
- JF
|
||
date: 2024-07-19
|
||
---
|
||
|
||
# Pense-bête Ansible
|
||
|
||
#### Pour tester du jinja Ansible : [J2Live Online Jinja2 Parser and Renderer by TTL255](https://j2live.ttl255.com/)
|
||
|
||
### Des variables dans des accolades `{}`:
|
||
On veut le résultat : `touch {valeur1,valeur2}`
|
||
Les variables :
|
||
```
|
||
var1: valeur1
|
||
var2: valeur2
|
||
```
|
||
Template jinja :
|
||
```
|
||
touch {{ '{' ~ var1 }},{{ var2 ~ '}' }}
|
||
|
||
```
|
||
### Syntaxe `if`
|
||
```jinja
|
||
'{%- if env_trigramme not in ['pro'] %}[{{ env_without_pi | upper }}]{% endif -%}'
|
||
```
|
||
Affichera `''` si `env_trigramme: pro` ou `'ENV'` si `env_trigramme: env`.
|
||
|
||
### Ternary pour choisir la valeur d'une variable selon un état vrai ou faux
|
||
```yaml
|
||
- name: "{{ boolean_var is not defined|ternary('is true', variable + '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 }}"
|
||
``` |