Ansible playbooks
Encyclopedia home · Search machine-readable catalog
CF-056
Local disposable inventory
Copy to inventory/lab.yml.
all:
children:
lab:
hosts:
localhost: {ansible_connection: local}Verification. Review operations before running against the controller machine locally.
Validation syntax-validated. Official reference
CF-057
Verify remote reachability
Copy to playbooks/ping.yml.
- hosts: lab
gather_facts: false
tasks:
- ansible.builtin.ping:Verification. The module tests remote Python and connectivity. It is separate from ICMP ping.
Validation syntax-validated. Official reference
CF-058
Create a managed directory
Copy to playbooks/directory.yml.
- hosts: lab
tasks:
- ansible.builtin.file:
path: /tmp/ci-lab
state: directory
mode: "0750"Verification. Run a disposable path and confirm a second run reports no change.
Validation syntax-validated. Official reference
CF-059
Render app configuration
Copy to playbooks/config.yml.
- hosts: lab
tasks:
- ansible.builtin.template:
src: app.toml.j2
dest: /tmp/ci-lab.toml
mode: "0640"Verification. Put the template next to the playbook's templates directory and use non-secret inputs.
Validation syntax-validated. Official reference
CF-060
Protect secret-bearing tasks
Copy to playbooks/credentials.yml.
- hosts: lab
tasks:
- ansible.builtin.copy:
content: "{{ lab_token }}"
dest: /tmp/ci-lab-token
mode: "0600"
no_log: true
diff: falseVerification. Use only a disposable fixture secret and verified directory ownership. Use secure runtime retrieval in production.
Validation syntax-validated. Official reference
CF-061
Install a curated Ubuntu package
Copy to playbooks/packages.yml.
- hosts: ubuntu_canary
become: true
tasks:
- ansible.builtin.apt:
name: ca-certificates
state: present
update_cache: true
cache_valid_time: 3600Verification. Scope to approved hosts and run --check first.
Validation syntax-validated. Official reference
CF-062
Execute a rollback handler on failed task
Copy to playbooks/rollback.yml.
- hosts: lab
tasks:
- block:
- ansible.builtin.command: /usr/local/bin/lab-deploy
rescue:
- ansible.builtin.command: /usr/local/bin/lab-recover
- ansible.builtin.fail: {msg: "Deployment failed and recovery ran"}Verification. Install trusted idempotent deploy/recover hooks. Rescue never silently reports deploy success.
Validation syntax-validated. Official reference
CF-063
Roll through hosts one at a time
Copy to playbooks/deploy.yml.
- hosts: web
serial: 1
any_errors_fatal: true
tasks:
- ansible.builtin.import_role: {name: web}Verification. Include health and load balancer drain within the reviewed role.
Validation syntax-validated. Official reference
CF-064
Only restart a changed service
Copy to playbooks/service.yml.
- hosts: web
become: true
tasks:
- ansible.builtin.copy: {src: web.conf, dest: /etc/web.conf, mode: "0644"}
notify: Restart web
handlers:
- name: Restart web
ansible.builtin.service: {name: web, state: restarted}Verification. Verify a second copy is unchanged and therefore doesn't notify the handler.
Validation syntax-validated. Official reference
CF-065
Validate app health remotely
Copy to playbooks/health.yml.
- hosts: web
tasks:
- ansible.builtin.uri: {url: "http://127.0.0.1:8000/healthz", status_code: 200}
register: probe
until: probe.status == 200
retries: 8
delay: 2Verification. Probe from each host. Add a separate load balancer health probe.
Validation syntax-validated. Official reference