Follow Consistent Naming: Use descriptive names for playbooks to indicate their purpose, e.g., deploy_app.yml
run_once will be executed at each serial execution in the play. That means, if you choose serial = 1, it will be asked to confirm as many times as the quantity of targets on the play.
Check Ansible docs: https://docs.ansible.com/ansible/latest/user_guide/playbooks_strategies.html#running-on-a-single-machine-with-run-once
When used together with serial, tasks marked as run_once will be run on one host in each serial batch. If the task must run only once regardless of serial mode, use when: inventory_hostname == ansible_play_hosts_all[0] construct.
Attention aux slicing !
https://github.com/ansible/ansible-lint/issues/1026#issuecomment-685849603
Ansible ignores run_once with the free strategy which means your tasks are run many times, once for each valid inventory host
Si run_once, toujours préciser le delegate_to ou when: inventory_hostname ==
Il est aussi possible de faire quelque chose comme :
- command: /opt/application/upgrade_db.py when: inventory_hostname == webservers[0]
Les tâches marquées comme run_once seront exécutées sur un hôte dans chaque série de lot. Si la tâche ne doit s'exécuter qu'une seule fois quel que soit le “serial” mode , utilisez
when: inventory_hostname == ansible_play_hosts_all[0]
command: ansible-galaxy ni de shell: ansible-galaxy, mais utiliser la manière native de AAP / AWX (RA_QUA_N1)
Exemple : Contrôle avant d'effectuer la connexion : playbook localhost avec gather_facts=false Playbook dédié : ansible.builtin.import_playbook: _assert_extra_vars.yml
Timeout après 5s (async)
--- - name: Test hosts: localhost tasks: - name: Sleep ansible.builtin.command: /bin/sleep 60 async: 10 # poll: 5
Voir : https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_checkmode.html#check-mode-dry
Éviter command: cat. Préférez :
- name: get actual effective params slurp: src: /proc/cmdline become: true register: all_current_activ_params - name: show effective params debug: msg: "{{ all_current_activ_params.content | b64decode }}"
Exemple :
DEFAULT_PRIVATE_ROLE_VARS
M(ansible.builtin.include_roles) and M(ansible.builtin.import_roles) C(public)
Modules :
You cannot use loops on 'import_tasks' statements. You should use 'include_tasks' instead.
Mettre les become que sur les tâches nécessitant les privilèges, et non sur tous le playbook (RA_SEC_N1)
Utiliser no_log: true pour les taches utilisant des secrets (RA_SEC_N1)
Pour les données sensibles utiliser ansible-vault ou les Crendential AWX (RA_SEC_N1)
Troubleshooting untrusted templates https://docs.ansible.com/ansible/devel/porting_guides/porting_guide_12.html
export _ANSIBLE_TEMPLAR_UNTRUSTED_TEMPLATE_BEHAVIOR=fail
https://docs.ansible.com/ansible/devel/porting_guides/porting_guide_core_2.19.html#untrusted-templates export ANSIBLE_DISPLAY_TRACEBACK=always
Voir :
callback_whitelist = timer, profile_tasks- name: Installation d'un logiciel sur plusieurs serveurs avec throttle ansible.builtin.apt: name: nginx state: present async: 600 # Exécution en mode asynchrone avec un délai maximum de 10 minutes poll: 5 # Vérification toutes les 5 secondes throttle: 3 # Limite à 3 installations simultanées when: inventory_hostname in groups['webservers']
Convention pour les variables, il doit être possible de distinguer deux types (fonctionnel) de variables :
Préférer les variables a plat plutôt que les variables dictionnaires (RA_CONV_OPT)
endpoint_url: endpoint_port:
plutôt que
endpoint: url: port:
De préférence nommer la variable de boucle (loop_var) à la place d'utiliser le nom par défaut item (RA_CONV_OPT) C'est plus lisible, et cela permet un fonctionnement non équivoque en cas de boucles imbriqués.
Il est recommandé de définir une convention de nommage pour la variable de boucle. Par exemple d'avoir toujours un suffixe _item
Utiliser label dans loop_control pour rendre l'affichage plus lisible
You should encrypt sensitive or secret variables with Ansible Vault. However, encrypting the variable names as well as the variable values makes it hard to find the source of the values. To circumvent this, you can encrypt the variables individually using ansible-vault encrypt_string, or add the following layer of indirection to keep the names of your variables accessible (by grep, for example) without exposing any secrets:
When running a playbook, Ansible finds the variables in the unencrypted file, which pulls the sensitive variable values from the encrypted file. There is no limit to the number of variable and vault files or their names.
Note that using this strategy in your inventory still requires all vault passwords to be available (for example for ansible-playbook or AWX/Ansible Tower) when run with that inventory.
Use the serial keyword to control how many machines you update at once in the batch.