roles则是在ansible中,playbooks的目录组织结构。而模块化之后,成为roles的组织结构,易读,代码可重用,层次清晰。
#一、实战目标:通过role远程部署nginx并配置
两台机器配置本地解析
[root@ansible-server ~]# vim /etc/hosts
192.168.64.131 ansible-server
192.168.64.130 ansible-web3
[root@ansible-web3 ~]# vim /etc/hosts
192.168.64.131 ansible-server
192.168.64.130 ansible-web3
添加主机组
[root@ansible-server ~]# vim /etc/ansible/hosts
[ansible-webs]
ansible-web3
配置免密登录:
[root@ansible-server ~]# ssh-copy-id -i 192.168.64.130
目录结构:
###1. 准备目录结构and创建文件:
[root@ansible-server ~]# cd /etc/ansible/roles/ #role为自带目录,如果不存在可以创建
[root@ansible-server roles]# mkdir nginx/{files,handlers,tasks,templates,vars} -p
[root@ansible-server roles]# touch site.yml nginx/{handlers,tasks,vars}/main.yml
[root@ansible-server roles]# yum install -y tree
[root@ansible-server roles]# tree
.
├── nginx
│ ├── file
│ ├── handlers
│ │ └── main.yml
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ └── vars
│ └── main.yml
└── site.yml
6 directories, 4 files
###2. 安装nginx并且配置模板and创建一个nginx的测试文件
[root@ansible-server roles]# yum -y install epel-release;yum -y install nginx;cp /etc/nginx/nginx.conf /etc/ansible/roles/nginx/templates/nginx.conf.j2
[root@ansible-server roles]# vim nginx/file/index.html
test role
###3. 编写任务
[root@ansible-server roles]# vim nginx/tasks/main.yml
---
- name: install epel
yum: name=epel-release state=latest
- name: install nginx
yum: name=nginx state=latest
- name: copy nginx.conf
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
- name: copy index.html
copy: src=/etc/ansible/roles/nginx/file/index.html dest=/usr/share/nginx/html/index.html
notify: start nginx
###4. 准备配置文件
[root@ansible-server roles]# vim nginx/templates/nginx.conf.j2
修改成如下内容。自定义变量
###5. 编写变量
[root@ansible-server roles]# vim nginx/vars/main.yml #添加如下内容
worker_connections: 2
###6. 编写handlers
[root@ansible-server roles]# vim nginx/handlers/main.yml
---
- name: start nginx #和notify的名字必须一样
service: name=nginx state=started
###7. 编写剧本
[root@ansible-server roles]# vim site.yml
---
- hosts: ansible-web3
user: root
roles:
- nginx
###8. 检测执行
[root@ansible-server roles]# ansible-playbook --syntax-check site.yml
playbook: site.yml
[root@ansible-server roles]# ansible-playbook site.yml
PLAY [ansible-web3] ****************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************
ok: [ansible-web3]
TASK [nginx : install epel] ********************************************************************************************************
changed: [ansible-web3]
TASK [nginx : install nginx] *******************************************************************************************************
changed: [ansible-web3]
TASK [nginx : copy nginx.conf] *****************************************************************************************************
changed: [ansible-web3]
TASK [nginx : copy index.html] *****************************************************************************************************
changed: [ansible-web3]
RUNNING HANDLER [nginx : start nginx] **********************************************************************************************
changed: [ansible-web3]
PLAY RECAP *************************************************************************************************************************
ansible-web3 : ok=6 changed=5 unreachable=0 failed=0
###9. 查看访问测试
[root@ansbile-web3 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 12125/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1058/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1208/master
tcp6 0 0 :::80 :::* LISTEN 12125/nginx: master
tcp6 0 0 :::22 :::* LISTEN 1058/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1208/master
#二、项目实战:通过ansible上线
###1、批量部署Jdk+Tomcat
[root@ansible-server src]# cat tomcat.yml
- hosts: webservers
user: root
tasks:
##配置JDK,上传jdk、tomcat的安装包到/usr/src
- name: configure Jdk1.8
copy: src=/usr/src/jdk-8u211-linux-x64.tar.gz dest=/usr/src
- name: unzip
shell: tar -xvzf /usr/src/jdk-8u211-linux-x64.tar.gz -C /usr/local
- name: rename to java
shell: mv /usr/local/jdk1.8.0_211 /usr/local/java
- name: configure envirement1
shell: echo "JAVA_HOME=/usr/local/java" >> /etc/profile
- name: configure envirement2
shell: echo 'PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile
##Tomcat
- name: copy tomcat
copy: src=/usr/src/apache-tomcat-8.5.45.tar.gz dest=/usr/src
- name: unzip tomcat
shell: tar -xvzf /usr/src/apache-tomcat-8.5.45.tar.gz -C /usr/local
- name: rename to tomcat
shell: mv /usr/local/apache-tomcat-8.5.45 /usr/local/tomcat
- name: copy startup file
copy: src=/usr/src/startup.sh dest=/usr/local/tomcat/bin
notify: start tomcat
handlers:
- name: start tomcat
shell: nohup /usr/local/tomcat/bin/startup.sh &
[root@java-server src]# ls
apache-tomcat-8.5.45 debug kernels tomcat.retry
apache-tomcat-8.5.45.tar.gz jdk-8u211-linux-x64.tar.gz startup.sh tomcat.yml
[root@java-server src]# head -2 startup.sh #在控制端配置文件前面添加不然拷贝到被控制端会报错
#!/bin/sh
source /etc/profile
###2、批量部署Jenkins
项目描述:
- 准备两台机器,一台作为nginx代理。一台为tomcat服务器。
- tomcat服务器手动部署tomcat服务,并将webapps目录下面的内容提前删掉。
- 将jenkins.war包上传到nginx服务器。通过ansible将war包拷贝过去。并启动tomcat
- 配置nginx反向代理tomcat,实现访问jenkins。
一、tomcat服务器
1.安装jdk与tomcat略。
2.添加tomcat启动脚本中添加环境变量
[root@ansible-web2 ~]# vim /usr/local/tomcat/bin/startup.sh #需要添加如下内容
source /etc/profile
二、nginx服务器:
1.安装nginx与ansible,上传jenkins的war包略。
2.ansible配置如下:
3.定义变量:
[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# mkdir vars
[root@ansible ansible]# vim vars/path.yml
src_path: /root/jenkins.war
dest_path: /usr/local/tomcat/webapps/
4.配置playbook:
[root@ansible ansible]# vim jenkins.yml
- hosts: webserver2
user: root
vars_files:
- /etc/ansible/vars/path.yml
tasks:
- name: copy jenkins.war
copy: src={{ src_path }} dest={{ dest_path }}
- name: start tomcat
shell: nohup /usr/local/tomcat/bin/startup.sh &
[root@ansible ansible]# ansible-playbook --syntax-check jenkins.yml
[root@ansible ansible]# ansible-playbook jenkins.yml
5.配置nginx反向代理
[root@ansible ansible]# vim /etc/nginx/conf.d/jenkins.conf
server {
listen 80;
server_name localhost;
charset koi8-r;
access_log /var/log/nginx/host.access.log main;
location /jenkins {
proxy_pass http://192.168.62.181:8080;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
6.启动nginx
7.检查nginx与tomcat是否启动成功!
8.访问nginx服务器http://ip/jenkins。
###3、批量部署Jdk+Tomcat+Jenkins
将Jdk、Tomcat、Jenkins的安装包上传到ansbile控制节点的/usr/src下
[root@ansible ansible]# ls /usr/src/
[root@java-server ansible]# head -2 /usr/src/startup.sh #startup.sh是tomcat的启动脚本 #!/bin/sh source /etc/profile #加上此行,是为了启动加载到环境变量
变量文件:
[root@ansible ansible]# cat /etc/ansible/vars/file.yml
剧本:
[root@ansible ansible]# cat jenkins.yml
- hosts: ansible-web1
user: root
vars_files:
- /etc/ansible/vars/file.yml
tasks:
##配置JDK,上传jdk、tomcat的安装包到/usr/src
- name: configure JDK1.8
copy: src={{ src_jdk_path }} dest={{ dest_jdk_path }}
- name: unzip JDK
shell: tar -xvzf /usr/src/jdk-8u211-linux-x64.tar.gz -C /usr/local
- name: rename to java
shell: mv /usr/local/jdk1.8.0_211 /usr/local/java
- name: configure JDK envirement1
shell: echo "JAVA_HOME=/usr/local/java" >> /etc/profile
- name: configure JDK envirement2
shell: echo 'PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile
##Tomcat
- name: copy tomcat
copy: src={{ src_tomcat_path }} dest={{ dest_tomcat_path }}
- name: unzip tomcat
shell: tar -xvzf /usr/src/apache-tomcat-8.5.45.tar.gz -C /usr/local
- name: rename to tomcat
shell: mv /usr/local/apache-tomcat-8.5.45 /usr/local/tomcat
- name: copy startup file
copy: src=/usr/src/startup.sh dest=/usr/local/tomcat/bin
##Jenkins
- name: copy jenkins
copy: src=/usr/src/jenkins.war dest=/usr/local/tomcat/webapps/
notify: start jenkins
handlers:
- name: start jenkins
shell: nohup /usr/local/tomcat/bin/startup.sh &