Playbooks in Ansible
- Playbooks require you to describe the following important details
- hosts: where (on which nodes) do you want to run the playbook
- become: Do you need sudo permissions to execute the automation.
- tasks: Descriptive way of automating stuff in ansible
---
hosts: all
become: yes
tasks:
- name: update packages and install git
apt:
name: git
update: yes
state: present
- Modules: Atomic unit of automation in ansible.
- Refer Here for official documentation.
- Ansible Module will have parameters
- Some Paramaters are optional and remaining are mandatory
- Optional Parameters might have default value.
- In every modules documentation
Example: Write an Ansible Playbook to install git, tree and nano on nodes
- Manual Steps:
# Ubuntu
sudo apt-get update
sudo apt-get install git -y
sudo apt-get install tree -y
sudo apt-get install nano -y
# Redhat
sudo yum install git -y
sudo yum install tree -y
sudo yum install nano -y
- Ensure the commands are working manually
- Playbook:
---
- hosts: all
become: yes
tasks:
- name: Install git
apt:
name: git
update_cache: yes
state: present
- name: Install tree
apt:
name: tree
state: present
- name: Install nano
apt:
name: nano
state: present
- Ansible has a command line for executing playbooks ansible-playbook
ansible-playbook --help
ansible-playbook -i <inventory path> <path to playbook>
- When you run ansible-playbook
- Playbook recap: with the status of execution on the node. Changed = ansible did some work
- Ansible is trying to maintain state.
- When ansible playbook is failed on a particular node, it will not continue execution by default.
- Whenever executing ansible, ensure you set verbosity while developing playbooks
ansible-playbook -vvv <playbook>
ansible-playbook -vv <playbook>
ansible-playbook -v <playbook>
- Exercise: Write an ansible playbook to install java8, java9 and java 11 on ubuntu machines.