Showing posts with label Ansible. Show all posts
Showing posts with label Ansible. Show all posts

Saturday, April 11, 2020

Ansible templates

Ansible templates:
  • Templates will copy the source file to the target host destination.
  • While the template module gets executed it will read the template file and change all the variables to its value and copy the file to the target host.
  • Template file ends with .j2 extension which stands for Jinja2 templates.
Example-1:
—–
Create a directory.
#mkdir templates
Sample template file.
#vi templates/file.j2

<html>

<h1> Hello! Venkat </h1>

<h1> GoodDay {{username}} </h1>

</html>
Ansible Templates in Playbooks:
——————————————-
#vi playbooks/pb1.yml

- hosts: dev
vars:
- username: Venkat
- file_path: /var/www/html
tasks:
- name: Copy site files
template:
src= templates/file.j2
dest= {{file_path}}/index.html mode=0777
–> Use template module to copy the template file and the variables used in the template.

Ex-2: To change Tomcat port
#vi templates/demo.j2

.......

<Connector port= "{{myport}}"
protocol= "HTTP/1.1"
.......

(server.xml content)
After changing the Port edit the playbook.
#vi playbooks/pb2.yml
---
- hosts: web
become: true
vars:
- myport: 9090
- file_path: /etc/tomcat7/
tasks:
- name: to update packages
apt: update_cache=yes

- name: to install java
apt:
name=openjdk-7-jdk
state: latest

- name: to install tomcat
apt: 
name: tomcat7 
state: latest
- name: to change tomcat port
template:
src: templates/demo.j2
dest: {{file_path}}/server.xml
notify:
- restart tomcat

handlers:
- name: restart tomcat
service: 
name: tomcat7
state: restarted

Ansible Roles

<Ansible Playbooks                                                                                                                      Ansible Templates>
ANSIBLE ROLES:
  • Ansible roles are the organized way to perform the tasks in different playbooks according to their functionality in a directory structure way.
  • Roles allow you to call a set of variables, tasks, and handlers by simply specifying a defined role.
  • In the role file inside every directory, there is a main.yml file.
  • By default, the execution starts from the main.yml file.
Ansible Role Directory Structure:
Mainly ansible roles contain below the directories below.
  • Tasks (a series of tasks are defined here)
  • Handlers (All handlers are defined here)
  • vars (all the variables are defined here)
  • files (all the files will be placed inside this directory)
  • meta (It is used to declare role dependences and information about a role)

Example:
#vi playbooks/pb1.yml
 ---
 - hosts: dev 
   remote_user: root 
   roles: 
     - myrole
Create the directories in the role directory.
#cd playbooks
#mkdir -p roles/myrole/{files,handlers,tasks,vars,meta}
Create task files.
#vi roles/myroles/tasks/main.yml

- include: pack_install.yml
- include: deploy_app.yml
Create the installation files.
#vi roles/myrole/tasks/pack_install.yml

- name: install {{pack1}}
  apt:
  name: {{pack1}}
  state: latest
  when: ansible_distribution=='Ubuntu'
  notify:
   - start apache2

- name: install {{pack2}}
  apt: 
  name: {{pack2}}
  state: latest
  when: ansible_distribution=='Ubuntu'
  notify:
  - start tomcat

- name: install {{pack3}}
  apt:
  name: {{pack3}}
  state: latest
  when: ansible_distribution=='Ubuntu'
  notify:
  - start mysql

Create deploy file.
#cd /playbooks/roles/myrole/tasks

#vi deploy_app.yml

- name: to deploy index.html
  copy: 
    src: index.html
    dest: /var/www/html/index.html

- name: to deploy sample.war
  copy: 
    src: sample.war
    dest: /var/lib/tomcat7/webapps/sample.war
Create handlers file.
#vi roles/myrole/handlers/main.yml

- name: start apache
  service:
  name: apache2
  state: started

- name: start tomcat
  service:
  name: tomcat7
  state: started

- name: start mysql
  service:
  name: mysql
  state: started
Create variables file.
#vi roles/myrole/vars/main.yml

pack1: apache2
pack2: tomcat7
pack3: mysql-server
Create sample files.
#vi roles/myrole/files/index.html

<h1> Hello from ansible role</h1>
See the files in the files directory
#vi playbooks/roles/myrole/files# ls

index.html  sample.war
Check the syntax
#ansible-playbook playbooks/myrole.yml --syntax-check
Execute the Roles
#ansible-playbook playbooks/myrole.yml

Role tree Structure:
After creating the roles directories and all files, the tree view will display like the example below.
Common role directory tree structure
                                                                                          
<Ansible Playbooks                                                                                                                     Ansible Templates> 





Ansible: Playbooks

What is YAML:
YAML stands for YAML Ain’t Markup Language, which is human-readable and often used in the configuration file.
Features of YAML:
  • Compared to XML or JSON, YAML is less complex and provides the same features.
  • It provides configuration settings without the need to learn complex code types such as CSS, JavaScript or PHP
Basic Rules for YAML:
  •  You must end the YAML files with .yaml or .yml extension.
  •  YAML must be case sensitive.
  •  YAML doesn’t support the use of tabs. Instead of tabs, it uses spaces which are supported universally.
Basic Data Types of YAML:
YAML supports some basic data types which can be used with programming languages. Such as
1. Scalars: Strings or numbers.
2. Sequences: arrays or lists.
3. Mappings: hashes or dictionaries.
Example of Yaml file:
---
- hosts: dev
  become: true
  tasks:
  - name: to install httpd
    yum: 
    name: httpd 
    state: latest  - name: to install tomcat
    yum: name= tomcat state= latest  - name: to start httpd service
    service: name=httpd state=started  - name: to start tomcat service
    service: name=tomcat state=started
Playbooks:
1. Hosts and Users
2. Tasks lists
3. Handlers
4. Ansible Variables(Vars, Vars_prompt)
5. Ansible Loops
Hosts and Users:
Each play in a playbook, you get to choose which machines in your infrastructure to target and what remote user to complete steps(called tasks).
Ex:
---
- hosts: dev
  remote_user: root
Tasks List:
  • Each playbook contains a list of tasks.
  • Tasks are executed in order, one at a time, against all machines matched by the host pattern.
  • The playbook will run from top to bottom.
Ex:
tasks:
- name: to install apache2
  apt:
  name: apache2
  state: latest
Handlers:
Running operations On Change.
Example:
---
- hosts: dev
  remote_user: true
  tasks:
  - name: install apache2
  apt:
  name: apache2
  state: latest
  notify:
  - start apache
  - name: to install tomcat  apt:
  name: tomcat7 
  state: latest
  notify:
  - start tomcat

handlers:
 - name: start apache
   service:
   name: apache2
   state: started - name: start tomcat
   service:
   name: tomcat7
   state: started
Ansible Variables:
  • A Variable is an element which can hold a specific value.
  • Variable names can be letters, numbers, and underscores.
  • Variables should start with a letter.
  • Here we have two types of variable methods, 1- Vars & 2- Vars_prompt

Vars:
Vars is nothing but a static functionality. Once the functions in playbooks are declared it can not change.
Example:
---
- hosts: all
  remote_user: root
  vars:
  pack1: net-tools
  pack2: wget
  tasks:
  - name: install package {{pack1}}
    yum:
    name:"{{pack1}}"
    state: latest
    when: ansible_distribution=="CentOS"

  - name: install package {{pack2}}
    apt:
    name: "{{Pack2}}"
    state: latest
    when: ansible_distribution=="Ubuntu"
Vars_prompt:
It will take the user input while executing playbook with vars_prompt and store into a variable.
Example: 
---
- hosts: centos
  remote_user: root
  vars_prompt:
  - name: pack_name
  prompt: Give Package name
  private: no
  tasks:
  - name: install package {{pack_name}}
    yum:
    name: "{{pack_name}}
    state: latest
Ansible Loops:
Ansible Loops can do many things in one task, such as create a lot of users, copy a set of files, install a lot of packages.
Example: copy multiple files using ansible loops.
---
- hosts: prod
  remote_user: root
  tasks: 
  - name: Copy{{item}}
  copy:
  src: /root/files/{{item}}
  dest: /tmp/{{item}}
  with_items:
  - abc1.txt
  - abc2.txt
  - abc3.txt
Add multiple users using ansible loops:
Example:
---
- hosts: all
  remote_user: root
  tasks: 
  - name: add group
    group:
    name:"sales"
    state: present

  - name: add user {{item}}
    usr:
    name: {{item}}
    group:"sales"
    state: "present"
    with_items:
    - user1
    - user2
    - user3


<Ansible: Adhoc Commands                                                          Ansible: Ansible Roles >








Featured Post

Ansible Tool Introduction

                                                                                                                                    Next ...