Console9

Ansible playbook directives and modules reference

Complete reference for Ansible playbook keywords, common modules, and inventory configuration options.

Ansible playbook directives and modules reference

Complete reference for Ansible playbook keywords, common modules, and inventory configuration options.

Ansible Playbook Keywords

KeywordDescriptionExample
hostsTarget hosts or groups from the inventory.hosts: webservers
becomeEscalate privileges (run as root via sudo).become: true
become_userUser to escalate to. Default: root.become_user: postgres
varsDefine variables for the play.vars: {http_port: 80}
vars_filesLoad variables from external YAML files.vars_files: [vars/main.yml]
tasksList of tasks to execute in order.tasks: [{name: ..., apt: ...}]
handlersTasks triggered by notify— run once at end of play.handlers: [{name: Reload Nginx, service: ...}]
rolesList of roles to apply to the play.roles: [nginx, certbot]
serialNumber or percentage of hosts to process at once (rolling updates).serial: 2
gather_factsWhether to collect host facts before running tasks. Default: true.gather_facts: false

Common Ansible Modules

ModuleDescriptionExample
aptManage packages on Debian/Ubuntu.apt: name=nginx state=present update_cache=true
yum/ dnfManage packages on RHEL/CentOS/Fedora.dnf: name=nginx state=present
serviceManage systemd/init services.service: name=nginx state=started enabled=true
copyCopy files from control node to managed nodes.copy: src=files/app.conf dest=/etc/app.conf
templateRender Jinja2 templates and deploy to managed nodes.template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
fileSet file/directory permissions, ownership, create/delete.file: path=/var/log/app state=directory owner=app
userManage user accounts.user: name=deploy shell=/bin/bash groups=sudo
ufwManage UFW firewall rules.ufw: rule=allow port=22 proto=tcp
commandRun a raw command (not through shell). Not idempotent.command: /opt/app/migrate.sh
shellRun a command through the shell. Not idempotent.shell: cat /etc/hosts \| grep myhost
lineinfileEnsure a specific line exists in a file.lineinfile: path=/etc/hosts line="10.0.0.1 myhost"
gitClone or update a Git repository.git: repo=https://github.com/user/app dest=/opt/app version=main