ansible之role

When we use the playbook to write tasks, if an httpd needs to be installed repeatedly, we need to rewrite a yml in the playbook. The role can be defined separately for tasks, templates, etc. Store tasks and templates in separate directories. Multiple rules can be called together to achieve the realization of complex scenarios

role storage rules

Each role has its own corresponding directory structure for organization.
Insert picture description here

Step-by-step example

Take nginx as an example to realize that the ansible host provides the nginx anti-generation function, and the two back-end nginx servers provide access to static resources and dynamic resources respectively

Insert picture description here

1) Define the host list

[root@nginx ~]# cat /etc/ansible/hosts 
[local_nginx]
11.2.3.25
[http_server]
11.2.3.63
[php_server]
11.2.2.228

2) Create the nginx folder under /etc/ansible/roles, and create directories such as tasks, template, var

[root@nginx ansible]# tree
.
├── ansible.cfg
├── hosts
├── nginx_roles.yaml
└── roles
    └── nginx
        ├── default
        ├── files
        │   ├── index.html
        │   └── index.php
        ├── handlers
        │   └── main.yaml
        ├── meta
        ├── tasks
        │   └── main.yaml
        ├── templates
        │   ├── php_server.conf.j2
        │   ├── proxy_server.conf.j2
        │   ├── server.conf.j2
        │   └── www.conf.j2
        └── vars
            └── main.yaml



3) Examples of each file,

[root@nginx ansible]# cat roles/nginx/tasks/main.yaml 
- name: install epel-release
  yum: name=epel-release.noarch state=present

- name: install nginx
  yum: name=nginx state=present

- name: start nginx
  service: name=nginx state=started

- name: install php-fpm
  yum: name=php-fpm state=present
  when:  ansible_hostname  == 'php_nginx'

- name: start php-fpm
  service: name=php-fpm state=started
  when: ansible_hostname == 'php-fpm'

- name: install php-fpm_conf
  template: src=www.conf.j2 dest=/etc/php-fpm.d/www.conf
  notify: restart php-fpm
  when:  ansible_hostname  == 'php_nginx'

- name: install http_server_conf
  template: src=server.conf.j2  dest=/etc/nginx/conf.d/server.conf
  notify: reload nginx
  when:   ansible_hostname  == 'http_nginx'

- name: install php_server_conf
  template: src=php_server.conf.j2 dest=/etc/nginx/conf.d/server.conf
  notify: reload nginx
  when:  ansible_hostname  == 'php_nginx'

- name: install proxy_nginx_conf
  template: src=proxy_server.conf.j2 dest=/etc/nginx/conf.d/server.conf
  notify: reload nginx
  when:  ansible_hostname  == 'nginx'

- name: make directory
  file: name={
    
    {
    
     index_root }} state=directory

- name: cp file 
  copy: src=index.html dest={
    
    {
    
     index_root }}
  when: ansible_hostname == 'http_nginx'

- name: cp php_file
  copy: src=index.php dest={
    
    {
    
     index_root }}
  when: ansible_hostname == 'php_nginx'

[root@nginx nginx]# cat templates/proxy_server.conf.j2   #这是nginx作为反代的配置文件
server{
	listen 80;
	server_name {
   
   { domain_name }};
	root {
   
   { index_root }};
	location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma){
                   index index.html;
                   proxy_pass http://{
   
   { http_server }};
           }
	
	location ~ .*\.php$ {
		index index.php;
		proxy_pass http://{
   
   { php_server }};
	}
}



[root@nginx nginx]# cat templates/server.conf.j2  # 这是处理静态的nginx主机配置文件 
server{
 	listen 80 default_server;   #此处加了default_server,所以在tasks中需要也添加一个nginx.conf.j2的文件,将nginx.conf中的default_server去掉,避免影响冲突。
	server_name {
   
   { domain_name }};
    	location /{
		index index.html;
		root {
   
   { index_root }};
	} 

}



[root@nginx nginx]# cat templates/php_server.conf.j2 #处理动态资源的nginx主机配置文件
server{
	listen 80 default_server;
	server_name {
   
   { domain_name }};
	index index.php;
	root {
   
   { index_root }};

	location / {
		root {
   
   { index_root }};
		index index.php;
		fastcgi_pass {
   
   { php_server }}:9000;
		fastcgi_index index.php;
		fastcgi_param SCRIPT_FILENAME {
   
   { index_root }}$fastcgi_script_name;
		include fastcgi_params;
	}
	
}


[root@nginx nginx]# cat templates/www.conf.j2   #这个是php-fpm的配置文件,可以根据自己修改

[root@nginx nginx]# cat vars/main.yaml    #这个是变量设置文件
index_root: /data/root/
http_server: 11.2.3.63
php_server: 11.2.2.228
domain_name: www.ydong.com
[root@nginx nginx]# ls files/   #这个是后端nginx的网页
index.html  index.php

Result after running directly

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44564366/article/details/104074671