How to automate application deployments using Ansible
- Ansible Expects to automate using Playbooks.
- Playbooks is YAML with predefined Structure.
Playbooks Structure
- Hosts: On which nodes defined in Inventory do you want to execute the playbook
- Syntax of Hosts: name value pair
hosts: all
- Remote_user: User which ansible executes your automation.
- Become: Do you want run the playbook using sudo (set Boolean value)
hosts: all
become: yes
- Tasks: list of individual task
- Task: writing some of task properties
hosts: all
become: yes
tasks:
- name: <value>
<module_name>:
<module_param1>: <module_value1>
..
..
..
<module_paramn>: <module_valuen>
state: <module supported state>
- hosts: all
become: yes
tasks:
- name: install git
package:
name: git
state: present
- name: uninstall git
package:
name: git
state: absent
Simple Scenario: Installing Tree on all the nodes
sudo apt-get update
sudo apt-get install tree -y
Expressing the above Manual Execution in Ansible
- Inventory: i need a list with ip addresses/dns names(fqdn)
- Playbook: lets write the basic structure
- hosts: all
become: yes
tasks:
- name: update ubuntu packages
- name: install tree
- Now we need Modules to automate. Lets define modules. Modules are smallest unit of work in Ansible. There are predefined Modules. You can also define custom modules.
- To automate the activity lets search modules. lets build a google query around this
apt-get update in Ansible
- Based of results, lets fill the modules in the above playbook
- hosts: all
become: yes
tasks:
- name: update ubuntu packages and install tree
apt:
name: tree
update_cache: yes
state: present
- Create an inventory file with ip addresses in each line
- create a yaml file called as tree-playbook.yml with the following content
- hosts: all
become: yes
tasks:
- name: update ubuntu packages and install tree
apt:
name: tree
update_cache: yes
state: present
- Explore ansible-playbook command line by using
ansible-playbook --help | less
- Command to execute in this case is
ansible-playbook -i test-inventory tree-playbook.yml
Module Categories In Ansible
Execution in Ansible
- Two possibilities:
- Playbook:
- Must if you are automating and also if it is frequent activity
- Can execute multiple modules from one playbook file
- Adhoc-Commands:
- Infrequently used activities use adhoc commands.
- Can execute only one module per command.
Adhoc Commands in Ansible
ansible --help
ansible -i <inventory path> -m <modulename> -a "param1=value1 .. .. paramn=valuen" [-b] <all>
Playbook in Ansible
- Collection of Plays.
- Each play
- hosts: all
become: yes
tasks:
..
..
- hosts: all
tasks:
..
..
- Each Play in a playbook defines the phase/kind of activities. Generally playbooks are divided into following plays as best practice
- PRE-DEPLOYMENT: Installing necessary Software’s
- DEPLOYMENT: Deploying the newly built package to the server
- POST-DEPLOYMENT: Mostly cleanup activites
- Ansible modules like command, bash, shell etc.. are not idempotent.
- Any module which directly executes os commands is not idempotent and it is not recommended to use these modules until you have no other options.
- If you are using this module, idempotance is your responsibility.
Like this:
Like Loading...