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
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 >
No comments:
Post a Comment