项目需求
1. 在gitlab中创建一个项目 nginxinstall
2. 编写playbook,实现一键部署nginx、部署一个静态测试页、测试部署结果
要求:
部署nginx
端口:83
运行身份:root
设置并发连接数:2048
根目录:/vaw/www/html
其他要求:
部署两个以上节点
用role
用templates
3. 将写好的playbook保存到gitlab仓库中
4.jenkins实现持续部署
192.168.59.200 docker1 jenkins git ansible
192.168.59.201 docker2 gitlab git
192.168.59.12 test2
第一步:在docker2创建一个nginxinstall项目
第二步:docker1克隆项目
[root@docker1 ~]# mkdir /nginx [root@docker1 ~]# cd /nginx [root@docker1 nginx]# ls [root@docker1 nginx]# git clone http://root@192.168.59.201/root/nginxinstall.git 正克隆到 'nginxinstall'... warning: 您似乎克隆了一个空版本库。 [root@docker1 nginx]# cd nginxinstall/
第三步:编写roles(docker1)
[root@docker1 nginxinstall]# mkdir roles/nginx/{tasks,vars,templates,files} -p [root@docker1 nginxinstall]# tree . └── roles └── nginx ├── files ├── tasks ├── templates └── vars 6 directories, 0 files [root@docker1 nginxinstall]# vim ./roles/nginx/vars/main.yaml usr: root por: 83 prc: 2048 wok: /var/www/html [root@docker1 nginxinstall]# echo v1 > ./roles/nginx/files/index.html [root@docker1 nginxinstall]# cp /etc/nginx/nginx.conf ./roles/nginx/templates/nginx.conf.j2 [root@docker1 nginxinstall]# vim ./roles/nginx/templates/nginx.conf.j2 ... user {{ usr }}; events { worker_connections {{ prc }}; } http { server { listen {{ por }}; root {{ wok }}; ... [root@docker1 nginxinstall]# vim ./roles/nginx/tasks/main.yaml - yum: name=nginx state=present - template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf - service: name=nginx state=restarted - copy: src=index.html dest={{ wok }} [root@docker1 nginxinstall]# mkdir inventory [root@docker1 nginxinstall]# ls inventory roles [root@docker1 nginxinstall]# vim inventory/host [test] 192.168.59.12 [root@docker1 nginxinstall]# vim site.yaml - hosts: all roles: - name: nginx [root@docker1 nginxinstall]# vim /etc/sysconfig/jenkins ... JENKINS_USER="root" …
第四步:查看jenkins相关信息:
[root@docker1 nginxinstall]# ps aux |grep jenkins root 13116 37.1 4.3 3694624 267464 ? Ssl 20:59 0:14 /etc/alternatives/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --daemon --httpPort=8080 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20 root 13251 0.0 0.0 112724 988 pts/0 R+ 20:59 0:00 grep --color=auto jenkins [root@docker1 nginxinstall]# systemctl restart jenkins
第五步:查看roles文件,并且模拟运行
[root@docker1 nginxinstall]# tree . ├── inventory │ └── host ├── roles │ └── nginx │ ├── files │ │ └── index.html │ ├── tasks │ │ └── main.yaml │ ├── templates │ │ └── nginx.conf.j2 │ └── vars │ └── main.yaml └── site.yaml 7 directories, 6 files [root@docker1 nginxinstall]# ansible-playbook -C -i inventory/host site.yaml PLAY [test] ************************************************************************************************************ TASK [Gathering Facts] ************************************************************************************************* ok: [192.168.59.12] TASK [nginx : yum] ***************************************************************************************************** changed: [192.168.59.12] TASK [nginx : template] ************************************************************************************************ changed: [192.168.59.12] TASK [nginx : service] ************************************************************************************************* changed: [192.168.59.12] TASK [nginx : copy] **************************************************************************************************** changed: [192.168.59.12] PLAY RECAP ************************************************************************************************************* 192.168.59.12 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
第六步:上传v1版本:
[root@docker1 nginxinstall]# git add . [root@docker1 nginxinstall]# git config --global user.email "you@example.com" [root@docker1 nginxinstall]# git config --global user.name "Your Name" [root@docker1 nginxinstall]# git commit -m v1 [master(根提交) 030ce20] v1 6 files changed, 104 insertions(+) create mode 100644 inventory/host create mode 100644 roles/nginx/files/index.html create mode 100644 roles/nginx/tasks/main.yaml create mode 100644 roles/nginx/templates/nginx.conf.j2 create mode 100644 roles/nginx/vars/main.yaml create mode 100644 site.yaml [root@docker1 nginxinstall]# git push -u origin master Password for 'http://root@192.168.59.201': Counting objects: 15, done. Compressing objects: 100% (5/5), done. Writing objects: 100% (15/15), 1.80 KiB | 0 bytes/s, done. Total 15 (delta 0), reused 0 (delta 0) To http://root@192.168.59.201/root/nginxinstall.git * [new branch] master -> master 分支 master 设置为跟踪来自 origin 的远程分支 master
第七步:docker1,网页布置jenkins
1、创建hitlab用户凭证
2、创建项目(nginx_install)
3、复制项目链接
4、填写项目
5、填写shell
6、构建
第九步:验证在其他电脑能否上传:(docker2)
[root@docker2 ~]# cd nginxinstall/ [root@docker2 nginxinstall]# ls inventory roles site.yaml [root@docker2 nginxinstall]# vim roles/nginx/files/index.html [root@docker2 nginxinstall]# git add . [root@docker2 nginxinstall]# ls inventory roles site.yaml [root@docker2 nginxinstall]# git commit -m v2 [master 6a6dfa1] v2 1 file changed, 1 insertion(+), 1 deletion(-) [root@docker2 nginxinstall]# git push -u origin master (gnome-ssh-askpass:14400): Gtk-WARNING **: 20:22:42.388: cannot open display: error: unable to read askpass response from '/usr/libexec/openssh/gnome-ssh-askpass' Password for 'http://root@192.168.59.201': Counting objects: 11, done. Compressing objects: 100% (3/3), done. Writing objects: 100% (6/6), 520 bytes | 0 bytes/s, done. Total 6 (delta 0), reused 0 (delta 0) To http://root@192.168.59.201/root/nginxinstall.git 030ce20..6a6dfa1 master -> master 分支 master 设置为跟踪来自 origin 的远程分支 master。 [root@docker2 nginxinstall]#