Ansible using advanced steps
Experimental Environment
Group: Srvs
Group member host name, system version, and IP
Node1 centos-7-core 172.16.253.184
node2 centos-7-core 172.16.254.89
node3 centos-7-core 172.16.253.47
Node4 Centos-6-core 172.16.253.30
iterative function of PlaybookSimple item to use
---
-hosts:srvs
remote_user:root
tasks:
-name:copy files
tags:copy
when:ansible_ Distribution_major_version = = "7"
copy:src=/root/ansible/{{Item}} dest=/tmp/{{Item}}
With_items:
- httpd.conf
-httpd.conf.j2
-httpd-manager.yml
-httpd-manager-j2.yml
Item multiple use
Note: Playbook file content format requirements are very strict, {User: "Test1", Group: "Wheel"}, there are spaces between user: and "test1"
---
-hosts:srvs
remote_user:root
tasks:
-name:add users
tags:addusers
when:ansible_ Distribution_major_version = = "7"
user:name={{Item.user}} group={{Item.group}} state=present with_items:<
c8/>-{User: "Test1", Group: "Wheel"}
-{User: "Test2", Group: "Root"}
the roles of Ansible
The roles of ansible is used to organize palybook in a hierarchical and structured way. Roles can automatically load variable files, tasks and handlers based on hierarchical structure. To use roles, you only need to use the include directive in playbook. Roles Application scenario change specifies host or host grouping due to non-standard maintenance difficulties or legacy costs large number of includes are required to achieve a high code reuse roles composition of a few playbook parties
Root@node1 playbook]# tree roles/
roles/\\ansible All the information is placed in the corresponding directory below this directory
└──nginx \ \ role name
├── default \ \ To use this directory when setting defaults for the current role, you should include a main.yml file,
├──files \ \ Store a file
├──handlers \ \ This directory should always contain a main.yml file that defines each handler used by each role
├──meta \ \ Should contain a main.yml that defines the special settings of the roles and their dependencies; 1.3 and later version support
├──tasks \ \ contains at least one file named Main.yml that defines the task list for this role, which you can use the Include directive
├──templates \\template module automatically looks for Jinja2 template files in this directory
└──vars \ \ Should contain a main.yml file that defines the variables used by this role
use Ansible's roles to configure Nginx service
defining the hosts group
Vim/etc/ansible/hosts
[Srvs:vars]
webport=80
[Srvs]
172.16.253.184 webport=10080
172.16.254.89 webport=10180
172.16.253.47 webport=10280
172.16.253.30 webport=10380
Create a roles organizational structure
#创建调用roles. yml file
touch/root/ansible/nginx.yml
#创建roles目录结构
MKDIR-PV nginx/{files,tasks,templates, Vars,handlers,meta,default}
Establish Roles:nginxDefines which roles to invoke and which user to use
VIM/ROOT/ANSIBLE/NGINX.YML
---
-hosts:srvs
remote_user:root
roles:
-Nginx
An atomic-level functional module that defines roles calls in the Main.yml file: Install, config + restart
Define roles call function in #在 main.yml
vim/root/ansible/roles/nginx/tasts/main.yml
---
-include:install.yml
-include:config.yml
-include:stop.yml
Set up atomic level functions
#定义install功能
vim/root/ansible/roles/nginx/tasts/install.yml
---
-name:install
Yum:name=nginx State=present
#定义config功能
vim/root/ansible/roles/nginx/tasts/config.yml
---
-name:template
template:src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify:restart nginx
#定义stop的功能
vim/ ROOT/ANSIBLE/ROLES/NGINX/TASTS/STOP.YML
---
-name:stop nginx
Service:name=nginx state=stopped
Establish handlers function
VIM/ROOT/ANSIBLE/ROLES/NGINX/HANDLERS/MAIN.YML
---
# handlers file for Nginx
-Name:restart Nginx
Service:name=nginx state=restarted
Create a. J2 template for Nginx configuration files
This step can first install Nginx on the master host, get Nginx's default profile and modify it to become a. J2 template file
#先制作原始配置文件的备份
cp/etc/nginx/nginx.conf/root/ansible/roles/nginx/files/nginx.conf.bak
#使用原始配置文件制作. J2 template file
cp/etc/nginx/nginx.conf/root/ansible/roles/nginx/templates/nginx.conf.j2
Vim/root/ansible/roles/nginx /templates/nginx.conf.j2
#修改原始配置文件的 server{} The configuration section is as follows:
{% for vport in vports%}
server {
Listen {{ Vport}};
server_name {{ansible_hostname}}.achudk.com;
Root /usr/share/nginx/html;
include/etc/nginx/default.d/*.conf;
Location/{
}
error_page 404/404.html;
Location =/40x.html {
}
error_page 502 503 504/50x.html;
Location =/50x.html {
}
}
{% endfor%}
Create a variable file
VIM/ROOT/ANSIBLE/ROLES/NGINX/VARS/MAIN.YML
---
# vars file for Nginx
vports:
-
8080
nginx Organizational Structure Tree Chart==nginx organization Tree Chart = =
[Root@node1 roles]# tree/root/ansible/roles/nginx/
nginx/
├──default
├──files
│ └── Nginx.conf.bak
├──handlers
│ └──main.yml
├──meta
├──tasks
│ ├──config.yml
│ ├──install.yml
│ ├──main.yml
│ └──stop.yml
├──templates
│ └── Nginx.conf.j2
└──vars
└──main.yml
Validation results
Ansible-playbook/root/ansible/nginx.yml PLAY [Srvs] ************************************************************* TASK [Gathering Facts] ********************************************************* OK: [172.16.254.89] OK: [ 172.16.253.30] OK: [172.16.253.184] OK: [172.16.253.47] TASK [Nginx:install] ****************************************
Changed: [172.16.253.30] changed: [172.16.253.47] changed: [172.16.253.184] changed: [172.16.254.89] TASK [Nginx:template] ******************************************************** changed: [172.16.253.30] Changed: [ 172.16.253.47] Changed: [172.16.253.184] changed: [172.16.254.89] TASK [nginx:stop Nginx] **************************** OK: [172.16.253.30] OK: [172.16.253.47] OK: [172.16.254.89] OK: [172.16.253.184] RUNNING HAND
LER [Nginx:restart Nginx] **************************************** changed: [172.16.253.30] changed: [172.16.253.47] Changed: [172.16.253.184] changed: [172.16.254.89] PLAY RECAP ********************************************************************* 172.16.253.184:ok=5 Chang ed=3 unreachable=0 failed=0 172.16.253.30:ok=5 changed=3 unreachable=0 failed=0 172.16 .253.47:ok=5 changed=3 unreachable=0 failed=0 172.16.254.89:ok=5 changed=3 Unreachable=0 failed=0
For more advanced usage, please refer to "ansible Chinese authoritative guide"http://ansible.com.cn/