Ansible structure playbook

In this tutorial, I wrote about the Ansible structure playbook. What does the Ansible structure playbook mean? Let’s begin!

A sample Ansible Directory Layout for staging development as below

[huupv2@server-deployment huupv2]$ tree myproject			
myproject			
├── group_vars			
│   └── nginx-server-var.yml			
├── host_vars			
│   └── server-web01.yml			
├── inventory			
│   └── my_hosts			
├── nginx-server.yml			
├── requirements			
│   └── web-server.yml			
├── roles			
└── vars			
    └── nginx_instance.yml			
			
6 directories, 6 files			

Create a structured folder for the Ansible playbook

[huupv2@server-deployment huupv2]$ mkdir myproject									
[huupv2@server-deployment huupv2]$ mkdir -p myproject/{inventory,host_vars,group_vars,vars,requirements,roles}

Create structure files for the Ansible playbook

“>
[huupv2@server-deployment huupv2]$ touch myproject/requirements/web-server.yml							
[huupv2@server-deployment huupv2]$ touch myproject/vars/nginx_instance.yml							
[huupv2@server-deployment huupv2]$ touch myproject/group_vars/nginx-server-var.yml							
[huupv2@server-deployment huupv2]$ touch myproject/inventory/my_hosts							
[huupv2@server-deployment huupv2]$ touch myproject/host_vars/server-web01.yml							
[huupv2@server-deployment huupv2]$ touch myproject/nginx-server.yml							

For example Ansible structure playbook

Server “server-deployment” installed Ansible and you want to deploy Nginx for server “server-web01” etc as in the picture below:

Ansible structure playbook

Working with inventory

[huupv2@server-deployment myproject]$ cat inventory/my_hosts
					
[dev-develop:children]					
tomcat-server					
nginx-server					
db-server					
					
[tomcat-server]					
					
[nginx-server]					
server-web01					
					
[db-server]					
server-db01	

Working with host_vars

set variables for individual hosts in the generated inventory file. For example, The content host_vars/nginx-server.yml file as below

nginx_instances:					
 - env_id: "dev"					
   instance_name:					
     - "DEVOPSROLES"					

Working with group_vars

set variables to particular groups. For example, The content group_vars/nginx-server-var.yml as below

nginx_version:  1.14.2					
nginx_package:					
  fullname: nginx-1.14.2-1.el6.ngx					

Working with requirements

Automatically install Ansible Galaxy roles with requirements/web-server.yml. For example, The content requirements/web-server.yml as below

## tag					
## version: v1.0.0					
## develop specify develop branch					
# version: remotes/origin/develop					
					
- src: git+https://huupv2:123456789@gitlab.com/tomcat/role-tomcat.git					
  version: v2.2					
					
- src: git+https://huupv2:123456789@gitlab.com/nginx/role-nginx.git					
  version: feature/dev-nginx					

Use ansible-galaxy to install all roles required by your playbook.

ansible-galaxy install -r requirements/web-server.yml -p roles

Working with vars

For example, The content file myproject/vars/nginx_instance.yml as below

# For devopsroles / role-nginx		
nginx_instance:		
# templates/conf/common.conf.j2		
  common_settings:		
    client_body_buffer_size: 16k		
    client_body_timeout: 60s		
    client_header_buffer_size: 1k		
    client_header_timeout: 60s		
    client_max_body_size: 1m		
    default_type: text/html		
    keepalive_timeout: 60s		
    large_client_header_buffers:		
      count: 4		
      size: 8k		
# templates/conf/nginx_site_instance.conf Used by		
  instance_indexes:		
  - index.html		
  - index.htm		
  instance_name: DEVOPSROLES							  		
# templates/conf/http[s].virtual_host.conf Used by		
  virtual_host_settings:				
    indexes:		
    - index.html		
    - index.htm		
    listen_port:		
      http: '8080'		
      https: '8443'					
    locations:				
    name: devopsroles.com		
    root: /app/DEVOPSROLES/nginx/htdocs		
    server_names:		
    - devopsroles.com		
		

Working with nginx-server.yml

- hosts: nginx-server		
  user: huupv		
  become: yes		
  roles:		
    - role-nginx		

Ansible run playbook

sudo ansible-playbook -i inventory/my_hosts nginx-server.yml -vvv --limit "server-web01"

Conclusion

Through the article, you can create a structured Ansible playbook simple. I hope will this your helpful. For more details, Ansible refers to Ansible tutorial.

, ,

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created DevopsRoles.com site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.