Tuesday, April 7, 2026

Ansible_6

 Anatomy of a Playbook

---
- name: expample playbook
hosts: all
become: true
tasks:
- name: Ensure nginx pkg is installed
package:
name: nginx
update_cache: true

ansible -i <inv_name> -b -m package -a 'name=nginx update_cache=true'

Playbooks
playbook is a list of dictionaries. A playbook is a list of plays only

we have only single play named "example playbook", every play must contain the hosts variable, the magic group all (all hosts in the inventory)

name:
Decribe about the play, Ansible prints the name when the play runs.

become:
is a boolean, used to specify the privillaged account (root)

tasks:
tasks of that play

we have used the module package and the used argunets name: nginx and update_cache: true, name for the tasks are optional

Ansible will print out the name of a task when it runs.


Modules

Modules are standalone scripts written for a particular job

module is a small program that performs actions on a local machine, application programming interface (API), or remote host. Modules are expressed as code, usually in Python, and contain metadata that defines when and where a specific automation task is executed and which users can execute it.


Documentation
$ ansible-doc <module_name> # shows documentation about the modules you have installed.


Any Ansible task that runs has the potential to change the state of the host in some way. Ansible modules will first check to see whether the state of the host needs to be changed before taking any action. If the host’s state matches the module’s arguments, Ansible takes no action on the host and responds with a state of “ok.”

On the other hand, if there is a difference between the host’s state and the module’s arguments, Ansible will change the state of the host and return “changed.”


No comments:

Post a Comment