本篇博客仅供学习,没有实际项目意义,仅供学习之用
实验环境:
VMware® Workstation 12 Pro
系统平台:
CentOS Linux release 7.4.1708 内核 3.10.0-693.el7.x86_64
Ansible中文权威指南:http://www.ansible.com.cn/
配置密钥连接:
进入用户的ssh密钥路径
cd /root/.ssh/
生成公私钥
ssh-keygen -t rsa -b 4096
将公钥发送给远程目标主机
ssh-copy-id -i id_rsa.pub root@10.10.11.2
测试连接
ssh root@10.10.11.2
定义角色的路径:默认位置
[root@localhost ~]# less /etc/ansible/ansible.cfg
# additional paths to search for roles in, colon separated
#roles_path = /etc/ansible/roles
创建相应的目录:
~]# cd /etc/ansible/roles/
[root@localhost roles]# mkdir -pv ./nginx/{files,templates,tasks,handlers,vars,meta,dafault}
目录结构
[root@localhost roles]# tree
├── memcached├── dafault├── files├── handlers├── meta├── tasks├── templates└── vars
配置nginx思路:
安装程序包,复制配置文件,启动服务。
编辑/nginx/tasks/main.yml文件(设置任务列表)
[root@localhost nginx]# vim tasks/main.yml
- name: install epel-release packageyum: name=epel-release state=installed
- name: install nginx packageyum: name=nginx state=installed#准备目录路径
- name: create doc rootfile: path={{ docroot }} state=directory
- name: install home pagecopy: src=index.html dest={{ docroot }}/#自动会去templates目录下找j2文件#复制带变量的文件需要使用templates来执行
- name: install conf filetemplate: src=server.conf.j2 dest=/etc/nginx/conf.d/server.confnotify: reload nginx
- name: start nginx serviceservice: name=nginx enabled=true state=started#使用templates复制文件时的路劲设置方式:相对于etc/ansible/roles/nginx的相对路径template: copy: src=templates/server.conf.j2 dest=/etc/nginx/conf.d/server.conf完全绝对路径template: copy: src=/etc/ansible/roles/nginx/templates/server.conf.j2 dest=/etc/nginx/conf.d/server.conf需要自动去templates目录下自动识别template: src=server.conf.j2 dest=/etc/nginx/conf.d/server.conf
nginx的模版配置文件(.j2文件)
[root@localhost nginx]# vim templates/server.conf.j2
server {listen 80;server_name {{ ansible_fqdn }} {{ ansible_hostname }};location / {root {{ docroot }};index index.jsp index.html;}
}
触发器文件配置
[root@localhost nginx]# vim handlers/main.yml
- name: reload nginxserver: name=nginx state=reloaded
这个nginx wed服务,需要测试页,编辑网站的主页:
[root@localhost nginx]# vim files/index.html
Welcome to nginx
定义变量:
[root@localhost nginx]# vim vars/main.yml
#定义网站主页的路径
docroot: /data/nginx/
yaml文件play_books文件
[root@localhost ~]# cd ansible/
[root@localhost ansible]# vim nginx.yml
- hosts: allremote_user: rootroles:- nginx
进行语法检测:
[root@localhost ansible]# ansible-playbook --syntax-check nginx.yml
进行测试执行:
[root@localhost ansible]# ansible-playbook --check nginx.yml PLAY [10.10.11.2] *************************************************************************************************TASK [Gathering Facts] ********************************************************************************************
ok: [10.10.11.2]TASK [nginx : install epel-release package] ***********************************************************************
ok: [10.10.11.2]TASK [nginx : install nginx package] ******************************************************************************
ok: [10.10.11.2]TASK [nginx : create doc root] ************************************************************************************
ok: [10.10.11.2]TASK [nginx : install home page] **********************************************************************************
ok: [10.10.11.2]TASK [nginx : install conf file] **********************************************************************************
ok: [10.10.11.2]TASK [nginx : start nginx service] ********************************************************************************
ok: [10.10.11.2]PLAY RECAP ***********************************************************************************
10.10.11.2 : ok=7 changed=0 unreachable=0 failed=0
确认执行文件:
[root@localhost ansible]# ansible-playbook nginx.yml PLAY [10.10.11.2] *******************************************************************************TASK [Gathering Facts] **************************************************************************
ok: [10.10.11.2]TASK [nginx : install epel-release package] *****************************************************
ok: [10.10.11.2]TASK [nginx : install nginx package] ************************************************************
ok: [10.10.11.2]TASK [nginx : create doc root] ******************************************************************
ok: [10.10.11.2]TASK [nginx : install home page] ****************************************************************
ok: [10.10.11.2]TASK [nginx : install conf file] ****************************************************************
ok: [10.10.11.2]TASK [nginx : start nginx service] **************************************************************
ok: [10.10.11.2]PLAY RECAP **************************************************************************************
10.10.11.2 : ok=7 changed=0 unreachable=0 failed=0
去执行命令的节点查看
查看端口
[root@localhost ~]# ss -ntlp |grep 80
LISTEN 0 128 *:80 *:* users:(("nginx",pid=11368,fd=6),("nginx",pid=11314,fd=6))
查看配置文件
[root@localhost ~]# cat /etc/nginx/conf.d/server.conf
server {listen 80;server_name localhost.localdomain localhost;location / {root /data/nginx/;index index.jsp index.html;}
}
修改前先进行备份
[root@localhost roles]# cp -a nginx/ /root/
修改自定义变量
[root@localhost nginx]# vim vars/main.yml
#添加servertype变量值为web
servertype: web
docroot: /data/nginx/
修改:/nginx/tasks/main.yml文件(设置任务列表)
[root@localhost nginx]# vim tasks/main.yml - name: install epel-release packageyum: name=epel-release state=installed
- name: install nginx packageyum: name=nginx state=installed
- name: create doc rootfile: path={{ docroot }} state=directory#新添加类型为webwhen: servertype == 'web'
- name: install home pagecopy: src=index.html dest={{ docroot }}/#新添加类型为webwhen: servertype == 'web'
- name: install conf filetemplate: src=server.conf.j2 dest=/etc/nginx/conf.d/server.conf#新添加类型为webwhen: servertype == 'web'notify: reload nginx
- name: start nginx serviceservice: name=nginx enabled=true state=started
测试执行nging=web (没有问题)
[root@localhost ansible]# ansible-playbook --check nginx.yml PLAY [10.10.11.2] *******************************************************************************TASK [Gathering Facts] **************************************************************************
ok: [10.10.11.2]TASK [nginx : install epel-release package] *****************************************************
ok: [10.10.11.2]TASK [nginx : install nginx package] ************************************************************
ok: [10.10.11.2]TASK [nginx : create doc root] ******************************************************************
ok: [10.10.11.2]TASK [nginx : install home page] ****************************************************************
ok: [10.10.11.2]TASK [nginx : install conf file] ****************************************************************
ok: [10.10.11.2]TASK [nginx : start nginx service] **************************************************************
changed: [10.10.11.2]PLAY RECAP **************************************************************************************
10.10.11.2 : ok=7 changed=1 unreachable=0 failed=0
设置变量
[root@localhost nginx]# vim vars/main.yml
servertype: web
#设置变量,代理服务器的代理目标主机变量
backendurl: 'http://127.0.0.1:8080'
docroot: /data/nginx/
创建proxy的模版文件
[root@localhost nginx]# vim templates/proxy.conf.j2
server {listen 80;server_name {{ ansible_fqdn }} {{ ansible_hostname }};location / {proxy_pass {{ backendurl }};}
}
yaml文件,play_books文件,
修改:/nginx/tasks/main.yml文件(设置任务列表)
[root@localhost nginx]# vim tasks/main.yml
- name: install epel-release packageyum: name=epel-release state=installed
- name: install nginx packageyum: name=nginx state=installed
- name: create doc rootfile: path={{ docroot }} state=directorywhen: servertype == 'web'
- name: install home pagecopy: src=index.html dest={{ docroot }}/when: servertype == 'web'
- name: install web conf filetemplate: src=server.conf.j2 dest=/etc/nginx/conf.d/server.confwhen: servertype == 'web'notify: reload nginx#新加proxy部分配置段
- name: install proxy conf filetemplate: src=proxy.conf.j2 dest=/etc/nginx/conf.d/server.confwhen: servertype == 'proxy'notify: reload nginx
- name: start nginx serviceservice: name=nginx enabled=true state=started
/nginx/tasks/main.yml文件(设置任务列表)测试
手动分别测试
默认执行web
使用:-e servertype=proxy 手动触发proxy
配置/etc/ansible/hosts
[root@localhost nginx]# vim /etc/ansible/hosts
[tomcat]
10.10.11.133
[web]
10.10.11.2
yaml文件play_books文件
[root@localhost ansible]# cat nginx.yml
- hosts: tomcat
remote_user: root
roles:
- { role: nginx, servertype: proxy }- hosts: web
remote_user: root
roles:
- nginx
测试:
[root@localhost ansible]# ansible-playbook --check nginx.yml PLAY [tomcat] ***********************************************************************************TASK [Gathering Facts] **************************************************************************
ok: [10.10.11.133]TASK [nginx : install epel-release package] *****************************************************
changed: [10.10.11.133]TASK [nginx : install nginx package] ************************************************************
changed: [10.10.11.133]TASK [nginx : create doc root] ******************************************************************
skipping: [10.10.11.133]TASK [nginx : install home page] ****************************************************************
skipping: [10.10.11.133]TASK [nginx : install web conf file] ************************************************************
skipping: [10.10.11.133]TASK [nginx : install proxy conf file] **********************************************************
changed: [10.10.11.133]TASK [nginx : start nginx service] **************************************************************
changed: [10.10.11.133]RUNNING HANDLER [nginx : reload nginx] **********************************************************
changed: [10.10.11.133]PLAY [web] **************************************************************************************TASK [Gathering Facts] **************************************************************************
ok: [10.10.11.2]TASK [nginx : install epel-release package] *****************************************************
ok: [10.10.11.2]TASK [nginx : install nginx package] ************************************************************
ok: [10.10.11.2]TASK [nginx : create doc root] ******************************************************************
ok: [10.10.11.2]TASK [nginx : install home page] ****************************************************************
ok: [10.10.11.2]TASK [nginx : install web conf file] ************************************************************
ok: [10.10.11.2]TASK [nginx : install proxy conf file] **********************************************************
skipping: [10.10.11.2]TASK [nginx : start nginx service] **************************************************************
changed: [10.10.11.2]PLAY RECAP **************************************************************************************
10.10.11.133 : ok=6 changed=5 unreachable=0 failed=0
10.10.11.2 : ok=7 changed=1 unreachable=0 failed=0
真正执行结果
最后的目录结构如下:
[root@localhost nginx]# tree
.
[root@localhost nginx]# tree
.
├── dafault
├── files
│ └── index.html
├── handlers
│ └── main.yml
├── meta
├── tasks
│ └── main.yml
├── templates
│ ├── proxy.conf.j2
│ └── server.conf.j2
└── vars└── main.yml
转:https://blog.51cto.com/12105235/2128557