Linux course index · Linux Advanced 2 category · Lesson 3 of 7
AI disclosure — AI-generated content, posted as is. AI can make mistakes. This extension adds new material and links related posts from this site. The newly written explanations and examples may be freely reused. Linked source material retains its own terms.
Learning goal
Convert a small configuration task into an Ansible playbook and demonstrate idempotence.
Concepts and setup
The archive often records administration as terminal history. That shows what someone typed but does not guarantee the final state on another host. Ansible describes a desired result. Idempotence means applying that same configuration to a system already at the desired state makes no further change. This lab needs Ansible and Python, uses localhost only, and never escalates privileges. Do not use diff output on files that contain secrets.
Write a config playbook and preview it
lab=$(mktemp -d)
cd "$lab" || exit
cat > lab.yml <<'YAML'
---
- name: Maintain one lab settings file
hosts: localhost
connection: local
gather_facts: false
tasks:
- name: Install the intended settings
ansible.builtin.copy:
content: "service=demo\n"
dest: "{{ playbook_dir }}/service.conf"
mode: "0644"
YAML
ansible-playbook -i localhost, lab.yml --syntax-check
ansible-playbook -i localhost, lab.yml --check --diff
test ! -e service.conf && printf 'preview did not write\n'
Possible output
playbook: lab.yml
...
changed: [localhost]
preview did not write
The comma in localhost, makes it an inline inventory. This module can predict a file change without writing it.
Apply the playbook twice
ansible-playbook -i localhost, lab.yml
ansible-playbook -i localhost, lab.yml
cat service.conf
stat -c '%a' service.conf
Possible output
... localhost : ok=1 changed=1 ...
... localhost : ok=1 changed=0 ...
service=demo
644
The first run changes the fixture. The next makes no change. If the playbook also manages a service, follow configuration verification with a health check.
Questions only
Quick-check questions
- What does changed=0 mean on the repeated run?
- Why use a copy module instead of echo in a shell task?
Assessment questions
- How would you demonstrate correction of drift?
- Why can a clean check-mode run miss a production problem?
- Name two steps before extending the inventory beyond localhost.
Questions and answers
Quick-check answers
- What does changed=0 mean on the repeated run?
Answer — The module found that the resource already matches the declared state. - Why use a copy module instead of echo in a shell task?
Answer — The module compares state and can manage content and permissions repeatably.
Assessment answer key
- How would you demonstrate correction of drift?
Answer — Deliberately change the lab file, preview and apply the playbook, check that the correct state came back, then confirm a repeated run shows changed=0. - Why can a clean check-mode run miss a production problem?
Answer — Some effects are not simulated, and actual target runtime conditions can differ. - Name two steps before extending the inventory beyond localhost.
Answer — Review and authorize targets, protect secrets, stage changes, and test rollback. Any two.
Related source articles on this site
- Some Ubuntu Linux Commands used in AWS platform #Unix/Linux #RHCSA
- Topic 2: Example: Debug a Script #Linux/Unix: Shell Programming – 001
- Create your own yum repo in Centos/Redhat #DevOps #RHCSA
- Q & A: Linux: Switch Users, Boot Process, File System
Technical references
Continue the course
Previous lesson 02 — Rootless Containers with Podman. Next lesson 04 — Performance Triage with vmstat and PSI.
Linux course index · Linux Advanced 2 category · Lesson 3 of 7
