The promise of Ansible is that you can run a playbook against 500 servers and only the ones that need a change get changed. That only holds if your tasks are idempotent. This guide covers that, plus the dry-run habits and secret handling that make Ansible safe in production.
Idempotent = safe to run again
An idempotent task describes a desired state (“IIS is installed”, “this service is running”), not an action (“install IIS”). Run it twice: the first run reports changed, the second reports ok.
Modules are idempotent by design:
- name: Spooler service is running and automatic
ansible.windows.win_service:
name: Spooler
state: started
start_mode: auto
Raw scripts are not – they run every time and always report changed:
# Avoid when a module exists
- ansible.windows.win_shell: Set-Service Spooler -StartupType Automatic; Start-Service Spooler
When you must use a script
Make it idempotent yourself:
- name: Disable SMBv1 if enabled
ansible.windows.win_shell: |
$cfg = Get-SmbServerConfiguration
if ($cfg.EnableSMB1Protocol) {
Set-SmbServerConfiguration -EnableSMB1Protocol $false -Force
'CHANGED'
}
register: smb1
changed_when: "'CHANGED' in smb1.stdout"
- The script checks before acting.
changed_whentells Ansible the truth, so reports and handlers work correctly.- For read-only tasks, use
changed_when: false.
For desired-state configuration of Windows settings, ansible.windows.win_dsc can also use DSC resources.
Preview with –check and –diff
ansible-playbook baseline.yml -l web --check --diff
--check– modules report what would change, without changing anything.--diff– shows before/after for files and templates.
Modules that can’t predict the outcome (like win_shell) are skipped in check mode. Add check_mode: false to read-only scripts that later tasks depend on, so the dry run stays accurate.
Canary first, then the rest
- hosts: web
serial:
- 1 # one canary server
- "25%" # then a quarter at a time
max_fail_percentage: 0
If the canary fails, nothing else is touched.
Ansible Vault for secrets
Encrypt a single value:
ansible-vault encrypt_string 'S3cretValue!' --name 'app_db_password'
Paste the output into group_vars:
app_db_password: !vault |
$ANSIBLE_VAULT;1.1;AES256
6231356438...
Or encrypt whole files:
ansible-vault create inventories/prod/group_vars/web/vault.yml
ansible-vault edit inventories/prod/group_vars/web/vault.yml
Run with the password:
ansible-playbook site.yml --ask-vault-pass
# or in automation:
ansible-playbook site.yml --vault-password-file /path/from/jenkins/credential
In Jenkins, store the vault password as a Secret file credential and pass its path with withCredentials – never commit it.
Hide secrets in output
- name: Set the app connection string
ansible.windows.win_environment:
name: APP_DB_PASSWORD
value: "{{ app_db_password }}"
level: machine
no_log: true
no_log: true stops the value appearing in the console, logs and Jenkins output.
A pre-flight checklist for production runs
--list-hosts– correct servers?--check --diffon a lab group – expected changes only?- Second run reports 0 changed – idempotent?
serialandmax_fail_percentageset?- Secrets in Vault or your credential store, and
no_logon tasks that use them? - Change approved and the rollback plan written down?
What’s next
You’ve completed the Ansible track. Next: What CI/CD means for operations teams.
Practise this topic
Test yourself with free questions on this topic – instant answers and explanations.
Start practice questionsAnsibleIdempotencyVaultSecurityBest PracticesAdvanced