连接:https://pan.baidu.com/s/1A3Iq3gGkGS27L_Gt37_I0g
提取码:ncy2
复制这段内容后打开百度网盘手机App,操做更方便哦html
[root@Ansible /]# tree /myroles/ /myroles/ ├── nginx.yaml #入口触发配置文件 └── roles #playbook的原型配置目录 └── nginx #nginx相关模组配置目录 ├── files #copy模块和script模块的参数src默认会从这个文件夹查找 ├── handlers #用来存放notify的 ├── tasks #用来存放ansible模块任务的 ├── templates #用来存放j2的 └── vars #用来存放变量的 7 directories, 1 file
[root@Ansible /]# mkdir -p /myroles/roles/nginx [root@Ansible /]# cat /myroles/nginx.yaml --- - hosts: all #执行的主机范围 gather_facts: True #开启系统内置变量 roles: #启用roles原型配置 - nginx #执行nginx原型模组
[root@Ansible /]# mkdir -p /myroles/roles/nginx/tasks/ [root@Ansible /]# cat /myroles/roles/nginx/tasks/main.yaml --- - name: check alived #任务1的名字 ping: #执行ping模块 - name: #任务2的名字 shell: ls / #执行shell模块 register: ls_result #将执行结果保存给变量 - debug: var=ls_result #变量的值赋值给debug进行输出
[root@Ansible /]# tree /myroles/ /myroles/ ├── nginx.yaml #nginx模组入口配置文件 └── roles └── nginx #nginx原型模组目录 └── tasks └── main.yaml #nginx模组的tasks任务配置文件 3 directories, 2 files
[root@Ansible myroles]# pwd /myroles [root@Ansible myroles]# ansible-playbook nginx.yaml PLAY [all] ************************************************************************************* TASK [Gathering Facts] ************************************************************************* ok: [Web02] ok: [Web01] TASK [nginx : check alived] ******************************************************************** ok: [Web02] ok: [Web01] TASK [nginx : shell] *************************************************************************** changed: [Web01] changed: [Web02] TASK [nginx : debug] *************************************************************************** ok: [Web01] => { "ls_result": { "changed": true, "cmd": "ls /", "delta": "0:00:00.008101", "end": "2018-09-09 03:33:26.602832", "failed": false, "rc": 0, "start": "2018-09-09 03:33:26.594731", "stderr": "", "stderr_lines": [], "stdout": "bin\nboot\ndev\netc\nhome\nlib\nlib64\nmedia\nmnt\nopt\nproc\nroot\nrun\nsbin\nsrv\nsys\ntmp\nusr\nvar", "stdout_lines": [ "bin", "boot", "dev", "etc", "home", "lib", "lib64", "media", "mnt", "opt", "proc", "root", "run", "sbin", "srv", "sys", "tmp", "usr", "var" ] } } ok: [Web02] => { "ls_result": { "changed": true, "cmd": "ls /", "delta": "0:00:00.007408", "end": "2018-09-09 03:33:26.609171", "failed": false, "rc": 0, "start": "2018-09-09 03:33:26.601763", "stderr": "", "stderr_lines": [], "stdout": "bin\nboot\ndev\netc\nhome\nlib\nlib64\nmedia\nmnt\nopt\nproc\nroot\nrun\nsbin\nsrv\nsys\ntmp\nusr\nvar", "stdout_lines": [ "bin", "boot", "dev", "etc", "home", "lib", "lib64", "media", "mnt", "opt", "proc", "root", "run", "sbin", "srv", "sys", "tmp", "usr", "var" ] } } PLAY RECAP ************************************************************************************* Web01 : ok=4 changed=1 unreachable=0 failed=0 Web02 : ok=4 changed=1 unreachable=0 failed=0
ansible-playbook执行入口配置文件nginx.yaml后,它会自动在roles目录下查找nginx目录并进入后查找tasks任务目录并执行main.yaml的任务配置文件java
#本配置和以前的roles配置等效 [root@Ansible /]# cat /service/scripts/test.yaml --- - hosts: all gather_facts: True tasks: #其实roles的本质就是将tasks任务单独写了。 - name: check alived #并在入口文件里追加了roles去查找tasks配置文件路径 ping: - name: shell: ls / register: ls_result - debug: var=ls_result
[root@Ansible nginx]# pwd /myroles/roles/nginx [root@Ansible nginx]# mkdir vars [root@Ansible nginx]# cat vars/main.yaml #vars模组的配置文件 --- my_name: yangwenbo phone: 17310658206 [root@Ansible nginx]# cat tasks/main.yaml #tasks模组的配置文件 --- - name: check alived ping: - name: shell: ls / register: ls_result - debug: var=ls_result - name: #添加对变量引用的任务编排 shell: echo my phone is {{ phone }} register: echo_result - debug: var=echo_result
[root@Ansible nginx]# tree /myroles/ /myroles/ ├── nginx.retry ├── nginx.yaml #nginx模组入口配置文件 └── roles └── nginx #nginx原型模组目录 ├── tasks │ └── main.yaml #nginx模组的tasks任务配置文件 └── vars └── main.yaml #nginx模组的vars任务配置文件 5 directories, 5 files
[root@Ansible myroles]# pwd /myroles [root@Ansible myroles]# ansible-playbook nginx.yaml PLAY [all] ************************************************************************************* TASK [Gathering Facts] ************************************************************************* ok: [Web01] ok: [Web02] TASK [nginx : check alived] ******************************************************************** ok: [Web02] ok: [Web01] TASK [nginx : shell] *************************************************************************** changed: [Web02] changed: [Web01] TASK [nginx : debug] *************************************************************************** ok: [Web01] => { #中间省略。。。 ok: [Web02] => { #中间省略。。。 TASK [nginx : shell] *************************************************************************** changed: [Web01] changed: [Web02] TASK [nginx : debug] *************************************************************************** ok: [Web01] => { "echo_result": { "changed": true, "cmd": "echo my phone is 17310658206", "delta": "0:00:00.004659", "end": "2018-09-09 03:48:24.375861", "failed": false, "rc": 0, "start": "2018-09-09 03:48:24.371202", "stderr": "", "stderr_lines": [], "stdout": "my phone is 17310658206", "stdout_lines": [ "my phone is 17310658206" #输出的结果 ] } } ok: [Web02] => { "echo_result": { "changed": true, "cmd": "echo my phone is 17310658206", "delta": "0:00:00.006155", "end": "2018-09-09 03:48:24.397609", "failed": false, "rc": 0, "start": "2018-09-09 03:48:24.391454", "stderr": "", "stderr_lines": [], "stdout": "my phone is 17310658206", "stdout_lines": [ "my phone is 17310658206" #输出的结果 ] } } PLAY RECAP ************************************************************************************* Web01 : ok=6 changed=2 unreachable=0 failed=0 Web02 : ok=6 changed=2 unreachable=0 failed=0
roles模型里使用copy,script模块,默认从roles/nginx/files这里面找linux
[root@Ansible nginx]# pwd /myroles/roles/nginx [root@Ansible nginx]# mkdir files [root@Ansible nginx]# cat files/test #files模组的配置文件 welcome to yunjisuan [root@Ansible nginx]# cat files/test.sh #files模组的配置文件 echo "My name is yangwenbo" >> /tmp/test [root@Ansible nginx]# chmod +x files/test.sh [root@Ansible nginx]# ll files/test.sh -rwxr-xr-x. 1 root root 41 Sep 9 04:11 files/test.sh [root@Ansible nginx]# cat tasks/main.yaml #tasks模组的配置文件 --- - name: check alived ping: - name: shell: ls / register: ls_result - debug: var=ls_result - name: shell: echo my phone is {{ phone }} register: echo_result - debug: var=echo_result - name: #添加copy模块 copy: src=test dest=/root/ - name: #添加script模块(自动在目标IP机器上执行脚本) script: test.sh
[root@Ansible nginx]# tree /myroles/ /myroles/ ├── nginx.retry ├── nginx.yaml └── roles └── nginx #nginx原型模组目录 ├── files │ ├── test #nginx模组的files任务配置文件 │ └── test.sh #nginx模组的files的脚本 ├── tasks │ └── main.yaml #nginx模组的tasks任务配置文件 └── vars └── main.yaml #nginx模组的vars任务配置文件 5 directories, 6 files
[root@Ansible myroles]# ansible-playbook nginx.yaml PLAY [all] ************************************************************************************* TASK [Gathering Facts] ************************************************************************* ok: [Web01] ok: [Web02] TASK [nginx : check alived] ******************************************************************** ok: [Web01] ok: [Web02] TASK [nginx : shell] *************************************************************************** changed: [Web01] changed: [Web02] TASK [nginx : debug] *************************************************************************** ok: [Web01] => { #中间省略。。。 ok: [Web02] => { #中间省略。。。 TASK [nginx : shell] *************************************************************************** changed: [Web01] changed: [Web02] TASK [nginx : debug] *************************************************************************** ok: [Web01] => { #中间省略。。。 ok: [Web02] => { #中间省略。。。 TASK [nginx : copy] **************************************************************************** changed: [Web02] changed: [Web01] TASK [nginx : script] ************************************************************************** changed: [Web02] changed: [Web01] PLAY RECAP ************************************************************************************* Web01 : ok=8 changed=4 unreachable=0 failed=0 Web02 : ok=8 changed=4 unreachable=0 failed=0
[root@Web01 ~]# cat /root/test welcome to yunjisuan [root@Web01 ~]# cat /tmp/test My name is yangwenbo [root@Web02 ~]# cat /root/test welcome to yunjisuan [root@Web02 ~]# cat /tmp/test My name is yangwenbo
roles模型里使用template模块,默认从roles/nginx/template里面找nginx
[root@Ansible nginx]# pwd /myroles/roles/nginx [root@Ansible nginx]# mkdir templates [root@Ansible nginx]# cat templates/test.j2 #templates模组的配置文件 myname is {{ my_name }},my phone is {{ phone }} #引用自定义变量 my ipaddress is {{ansible_all_ipv4_addresses[0]}} #引用内置变量 [root@Ansible nginx]# cat tasks/main.yaml #tasks模组的配置文件 --- - name: check alived ping: - name: shell: ls / register: ls_result - debug: var=ls_result - name: shell: echo my phone is {{ phone }} register: echo_result - debug: var=echo_result - name: copy: src=test dest=/root/ - name: script: test.sh - name: template: src=test.j2 dest=/root/test2 #下发可变配置文件
[root@Ansible nginx]# tree /myroles/ /myroles/ ├── nginx.retry ├── nginx.yaml └── roles └── nginx #nginx原型模组目录 ├── files │ ├── test #nginx模组的files任务配置文件 │ └── test.sh #nginx模组的files的脚本 ├── tasks │ └── main.yaml #nginx模组的tasks任务配置文件 ├── templates │ └── test.j2 #nginx模组的templates任务配置文件 └── vars └── main.yaml #nginx模组的vars任务配置文件 5 directories, 6 files
[root@Ansible myroles]# pwd /myroles [root@Ansible myroles]# ansible-playbook nginx.yaml PLAY [all] ************************************************************************************* TASK [Gathering Facts] ************************************************************************* ok: [Web01] ok: [Web02] TASK [nginx : check alived] ******************************************************************** ok: [Web01] ok: [Web02] TASK [nginx : shell] *************************************************************************** changed: [Web01] changed: [Web02] TASK [nginx : debug] *************************************************************************** ok: [Web01] => { #中间省略。。。 ok: [Web02] => { #中间省略。。。 TASK [nginx : shell] *************************************************************************** changed: [Web01] changed: [Web02] TASK [nginx : debug] *************************************************************************** ok: [Web01] => { #中间省略。。。 ok: [Web02] => { #中间省略。。。 TASK [nginx : copy] **************************************************************************** ok: [Web02] ok: [Web01] TASK [nginx : script] ************************************************************************** changed: [Web01] changed: [Web02] TASK [nginx : template] ************************************************************************ changed: [Web02] changed: [Web01] PLAY RECAP ************************************************************************************* Web01 : ok=9 changed=4 unreachable=0 failed=0 Web02 : ok=9 changed=4 unreachable=0 failed=0
[root@Web01 ~]# cat /root/test2 myname is yangwenbo,my phone is 17310658206 my ipaddress is 192.168.200.74 [root@Web02 ~]# cat /root/test2 myname is yangwenbo,my phone is 17310658206 my ipaddress is 192.168.200.75
roles使用notify模块,默认从roles/nginx/handles里面找git
[root@Ansible nginx]# pwd /myroles/roles/nginx [root@Ansible nginx]# mkdir handlers [root@Ansible nginx]# cat handlers/main.yaml #handlers模组的配置文件 --- - name: start_nginx #定义handlers的动做类型 shell: /usr/local/nginx/sbin/nginx - name: stop_nginx #定义handlers的动做类型 shell: /usr/local/nginx/sbin/nginx -s stop - name: reload_nginx #定义handlers的动做类型 shell: /usr/local/nginx/sbin/nginx -s reload [root@Ansible nginx]# cat tasks/main.yaml #tasks模组的配置文件 --- - name: check alived ping: - name: shell: ls / register: ls_result - debug: var=ls_result - name: shell: echo my phone is {{ phone }} register: echo_result - debug: var=echo_result - name: copy: src=test dest=/root/ - name: script: test.sh - name: template: src=test.j2 dest=/root/test2 notify: start_nginx #执行template任务后下发通知给handlers执行start_nginx
[root@Ansible nginx]# tree /myroles/ /myroles/ ├── nginx.retry ├── nginx.yaml └── roles └── nginx #nginx原型模组目录 ├── files │ ├── test #nginx模组的files任务配置文件 │ └── test.sh #nginx模组的files的脚本 ├── handlers │ └── main.yaml #nginx模组的handlers任务配置文件 ├── tasks │ └── main.yaml #nginx模组的tasks任务配置文件 ├── templates │ └── test.j2 #nginx模组的templates任务配置文件 └── vars └── main.yaml #nginx模组的vars任务配置文件 5 directories, 6 files
[root@Ansible myroles]# pwd /myroles [root@Ansible myroles]# ansible-playbook nginx.yaml PLAY [all] ************************************************************************************* TASK [Gathering Facts] ************************************************************************* ok: [Web02] ok: [Web01] TASK [nginx : check alived] ******************************************************************** ok: [Web01] ok: [Web02] TASK [nginx : shell] *************************************************************************** changed: [Web02] changed: [Web01] TASK [nginx : debug] *************************************************************************** ok: [Web01] => { #中间省略。。。 ok: [Web02] => { #中间省略。。。 TASK [nginx : shell] *************************************************************************** changed: [Web01] changed: [Web02] TASK [nginx : debug] *************************************************************************** ok: [Web01] => { #中间省略。。。 ok: [Web02] => { #中间省略。。。 TASK [nginx : copy] **************************************************************************** ok: [Web01] ok: [Web02] TASK [nginx : script] ************************************************************************** changed: [Web01] changed: [Web02] TASK [nginx : template] ************************************************************************ ok: [Web01] ok: [Web02] PLAY RECAP ************************************************************************************* Web01 : ok=9 changed=3 unreachable=0 failed=0 Web02 : ok=9 changed=3 unreachable=0 failed=0
特别提示:notify下发通知只有当以前的任务形成了变化那么才会被执行,若是没有发生任何改变,则notify不会被执行。例如:web
[root@Ansible /]# cat /tmp/test.yaml #notify模组的配置文件 --- - hosts: Web01 gather_facts: True tasks: - name: copy: src=/tmp/test dest=/root/ #这步形成目标改变才能出发notify notify: start_nginx handlers: - name: start_nginx shell: /usr/local/nginx/sbin/nginx
#第一次执行/tmp/test.yaml [root@Ansible /]# ansible-playbook /tmp/test.yaml PLAY [Web01] *********************************************************************************** TASK [Gathering Facts] ************************************************************************* ok: [Web01] TASK [copy] ************************************************************************************ changed: [Web01] #发生了改变 RUNNING HANDLER [start_nginx] #触了发notify ****************************************************************** changed: [Web01] PLAY RECAP ************************************************************************************* Web01 : ok=3 changed=2 unreachable=0 failed=0
#再次执行/tmp/test.yaml [root@Ansible /]# ansible-playbook /tmp/test.yaml PLAY [Web01] *********************************************************************************** TASK [Gathering Facts] ************************************************************************* ok: [Web01] TASK [copy] ************************************************************************************ ok: [Web01] #没有形成任务改变,未触发notify通知 PLAY RECAP ************************************************************************************* Web01 : ok=2 changed=0 unreachable=0 failed=0
因为Jenkins是依赖于java的,因此先介绍java环境的搭建shell
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.htmlwindows
[root@Ansible yang]# pwd /yang [root@Ansible yang]# ls jdk-8u171-linux-x64.tar.gz [root@Ansible yang]# tar xf jdk-8u171-linux-x64.tar.gz -C /usr/local/ [root@Ansible yang]# cd /usr/local/ [root@Ansible local]# mv jdk1.8.0_171 jdk [root@Ansible local]# /usr/local/jdk/bin/java -version #全路径验证java是否安装成功 java version "1.8.0_171" Java(TM) SE Runtime Environment (build 1.8.0_171-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
[root@Ansible local]# sed -i.org '$a export JAVA_HOME=/usr/local/jdk/' /etc/profile [root@Ansible local]# sed -i.org '$a export PATH=$PATH:$JAVA_HOME/bin' /etc/profile [root@Ansible local]# sed -i.org '$a export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar' /etc/profile [root@Ansible local]# tail -3 /etc/profile export JAVA_HOME=/usr/local/jdk/ export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar [root@Ansible local]# source /etc/profile [root@Ansible local]# java -version java version "1.8.0_171" Java(TM) SE Runtime Environment (build 1.8.0_171-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
#下载jinkins [root@Ansible yang]# wget http://ftp-chi.osuosl.org/pub/jenkins/war-stable/2.107.2/jenkins.war [root@Ansible yang]# ls jenkins.war
#启动jenkins并后台运行 [root@Ansible ~]# nohup java -jar jenkins.war &> /tmp/jenkins.out & [root@Ansible yang]# netstat -antup | grep java | grep -v grep tcp6 0 0 :::8080 :::* LISTEN 1405/java tcp6 1 0 192.168.200.73:48132 52.202.51.185:443 CLOSE_WAIT 1405/java udp 0 0 192.168.200.73:54730 192.168.200.2:53 ESTABLISHED 1472/java udp6 0 0 :::5353 :::* 1405/java udp6 0 0 :::33848 :::* 1405/java
[root@Ansible ~]# cat .jenkins/secrets/initialAdminPassword a21db34167d442f1bb23ff335016b5d3
Jenkins的做用?浏览器
- 可视化管理服务器
- 持续构建,能够直接去svn或者git上拉取代码并下发到服务器上
- 可视化ansible
Jenkins监听端口8080安全
- 启动Jenkins方式:nohup java -jar jenkins.war &> /tmp/jenkins.out &
- 查看监听端口:netstat -antup | grep java
- 访问方式:http://192.168.200.73:8080
- Jenkins默认密码路径,须要到Jenkins所在的服务器进行查看:/root/.jenkins/secrets/initialAdminPassword
- Jenkins初始化配置:将服务器上的密码复制过来到浏览器上提交后,静心等待,出现以下界面
系统管理--->全局安全配置
系统管理--->系统设置--->找到Publish over SSH能够添加对应的操做服务器
这就添加好一台被管理的主机了。要继续添加被管理的主机只须要重复以前的过程
需求,开发改完代码上传到svn上,而后运维打包最新版本代码部署到业务服务器上。
svn的部署与应用请参考专题(一)
设置完毕后,应用保存。
在windows上对svn的yunjisuan项目进行版本提交后
选择马上构建项目
[root@Web01 /]# hostname -I 192.168.200.74 [root@Web01 /]# ls /tmp/test/ 新建文本文档.txt
[root@Web01 /]# mkdir -p /www/{html,backup} [root@Web02 /]# mkdir -p /www/{html,backup} [root@Web01 /]# tree /www /www ├── backup #html网页目录的备份放置目录 └── html #网页目录 2 directories, 0 files
#备份web服务器旧网页目录代码,并将部署到服务器上的新代码覆盖到网页目录里 cd /www /usr/bin/tar zcf html_$(date +%F-%H-%S).tar.gz html rm -rf html/* mv html_*.tar.gz backup/ mv /tmp/test/* /www/html/
[root@Web01 /]# tree /www /www ├── backup │ └── html_2018-09-11-06-53.tar.gz └── html ├── 123123.txt └── \346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt 2 directories, 3 files
[root@Web02 /]# tree /www /www ├── backup │ └── html_2018-09-11-06-53.tar.gz └── html ├── 123123.txt └── \346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt 2 directories, 3 files
右键点击想要回滚的版本选择Revert to this revision
此时你发现你的共享目录里的东西已经被回滚到了指定的版本。
最后咱们千万别忘记了右键点击共享目录,将结果进行提交(SVN commit)
[root@Web01 /]# tree /www /www ├── backup │?? ├── html_2018-09-11-06-34.tar.gz │?? └── html_2018-09-11-06-53.tar.gz └── html └── 123123.txt 2 directories, 3 files
[root@Web02 /]# tree /www /www ├── backup │?? ├── html_2018-09-11-06-34.tar.gz │?? └── html_2018-09-11-06-53.tar.gz └── html └── 123123.txt 2 directories, 3 files
至此,测试成功!
最后进入安装界面最下方勾选,安装完成时重启Jenkins
[root@Ansible /]# cat /myroles/roles/nginx/ansible-playbook/test.yaml #准备一个playbook的配置文件 --- - hosts: all tasks: - name: test jenkins ansible_playbook shell: echo "welcome to yunjisuan" >> /tmp/yunwei.txt
而后新建一个新的项目任务,进行配置
选择马上构建进行测试并查看构建的可视化输出结果