1. Threats and principles #
Where are the risks?
The sensitive points of an Ansible chain #
Ansible has administrative access to your entire infrastructure: the control node is therefore a prime target. Classic risks are plain-text secrets in Git, shared SSH keys, passwords shown in logs and overly broad sudo rights.
- Secrets: encrypted at rest (Vault) and never displayed (
no_log). - Authentication: dedicated SSH keys, protected by a passphrase or agent, no root password.
- Privileges:
becomeonly when necessary, limited sudo user. - Controller: restricted, logged access, never exposed to the Internet.
2. Ansible Vault #
Encrypting sensitive variables and files.
The essential commands #
# Créer, éditer, afficher, chiffrer, déchiffrer
ansible-vault create group_vars/prod/vault.yml
ansible-vault edit group_vars/prod/vault.yml
ansible-vault view group_vars/prod/vault.yml
ansible-vault encrypt group_vars/prod/secrets.yml
ansible-vault decrypt group_vars/prod/secrets.yml
ansible-vault rekey group_vars/prod/vault.yml # changer le mot de passe
# Chiffrer une seule valeur (intégrable dans un fichier en clair)
ansible-vault encrypt_string 'S3cr3t!' --name 'db_password'db_password: !vault |
$ANSIBLE_VAULT;1.1;AES256
61373030633036386239...Using the vault at run time #
ansible-playbook site.yml --ask-vault-pass
ansible-playbook site.yml --vault-password-file ~/.vault_pass # fichier hors dépôt, chmod 600
ansible-playbook site.yml --vault-id prod@prompt --vault-id dev@~/.vault_dev # plusieurs vaults
# Dans ansible.cfg
[defaults]
vault_password_file = ~/.vault_passGood practice: Convention: put encrypted variables invault.yml, prefixed withvault_, and reference them from a plain file (db_password: "{{ vault_db_password }}"): you see the names, not the values.
Common mistake: Never version the vault password file. Add it to .gitignore.3. Authentication and privileges #
SSH, sudo and become.
Dedicated SSH keys #
# Clé dédiée à Ansible, protégée par passphrase
ssh-keygen -t ed25519 -f ~/.ssh/ansible_ed25519 -C "ansible-controller"
# Diffuser la clé publique avec Ansible lui-même
ansible all -m authorized_key -a "user=deploy key='{{ lookup('file','~/.ssh/ansible_ed25519.pub') }}' state=present" -k
# Agent SSH pour éviter de retaper la passphrase
eval "$(ssh-agent -s)" && ssh-add ~/.ssh/ansible_ed25519# ansible.cfg
[defaults]
remote_user = deploy
private_key_file = ~/.ssh/ansible_ed25519
host_key_checking = True
[ssh_connection]
pipelining = True
ssh_args = -o ControlMaster=auto -o ControlPersist=60sLimited sudo on the target #
# Autoriser l'utilisateur deploy à devenir root sans mot de passe
- name: Règle sudo pour deploy
ansible.builtin.copy:
dest: /etc/sudoers.d/deploy
content: "deploy ALL=(ALL) NOPASSWD: ALL\n"
mode: "0440"
validate: visudo -cf %sIn sensitive environments, replace ALL with the list of commands actually needed.
4. Leaking nothing #
no_log, logs and CI/CD.
no_log and diff #
- name: Créer l'utilisateur de base de données
community.postgresql.postgresql_user:
name: app
password: "{{ vault_db_password }}"
no_log: true # masque les arguments et résultats dans les logs
- name: Déployer le fichier de secrets
ansible.builtin.template:
src: secrets.env.j2
dest: /etc/app/secrets.env
mode: "0600"
diff: false # ne pas afficher le contenu avec --diffIn a CI/CD pipeline #
# GitLab CI : le mot de passe du vault vient d'une variable masquée
deploy:
script:
- echo "$VAULT_PASSWORD" > .vault_pass && chmod 600 .vault_pass
- ansible-playbook site.yml -i inventories/prod --vault-password-file .vault_pass
- shred -u .vault_passTo go further, delegate secrets to a dedicated vault (HashiCorp Vault, AWS Secrets Manager) through the community.hashi_vault lookups: the secret is then never stored in the repository.
ansible-galaxy collection install community.hashi_vault
ansible-lint --profile production site.yml # détecte les mauvaises pratiques de sécurité