# Tasks in a playbook are executed top down. Tasks use modules.tasks:- name:Name the task for readabilitymodule:parameters=go_here# Example:- name:Deploy Apache Configuration Filecopy:src=../../../ansible/files/configuration/httpd.confdest=/etc/httpd/conf/
---# -------- Global play declaration- hosts:webservers ## ----- Variables per playvars:git_repo:https://github.com/repo.githttp_port:8081db_name:wordpress## ------------------------### ---- Declare user to run taskssudo:yessudo_user:wordpress_user### ------------------------------gather_facts:no# dont't gather facts with SETUP module (default gathers facts - expensive in time)remote_user:roottasks:# --------------------------------- name:Install Apacheyum:name=httpd state=present- name:Start Apacheservice:name=httpd state=started
Including files
Use “- include” and “- include_vars” directives to include playbook files:
Check output of previous task as condition to run next task:
tasks:- name:Stop iptables nowservice:name=iptables state=stoppedregister:resultignore_errors:yes# supress default stop on error- debug:msg="Failure!"when:result|failed # Debug message will only be shown if task has failed# other conditions are "result|success", "result|skipped"
Checking variables with WHEN condition
Bring variable check to BOOL check:
- name:"test"hosts:localvars_prompt:- name:"os_type"prompt:"What OS? (centos or ubuntu)"default:"centos"private:novars:- is_ubuntu:"{{os_type == 'ubuntu'}}"- is_debian:"{{os_type == 'debian'}}"tasks:- debug:msg="this shows the conditional if variable equals ubuntu"when:is_ubuntu|bool- debug:msg="this shows the conditional if variable equals centos"when:is_centos|bool