首页 > 代码库 > Ansible 使用roles安装服务
Ansible 使用roles安装服务
创建所需要的目录
[root@HA2 nginx]# mkdir /etc/ansible/roles/nginx/{files,handlers,meta,vars,tasks,templates,default} -pv
新建tasks任务
[root@HA2 nginx]# cd /etc/ansible/roles/nginx/ //进入nginx的roles目录[root@HA2 nginx]# cat tasks/main.yml //查看配置文件内容- name: install nginx yum: name=nginx state=present - name: install config template: src=http://www.mamicode.com/files/nginx.conf.j2 dest=/etc/nginx/nginx.conf>
提供nginx的template文件
[root@HA2 nginx]# cat files/nginx.conf.j2 | head -30 # For more information on configuration, see: # * Official English Documentation: # * Official Russian Documentation: http://nginx.org/ru/docs/user {{ runuser }}; //这个是我们自定义变量,在vars/main.yml定义 worker_processes {{ ansible_processor_vcpus }}; //以an开头通常是setup获取fastc变量 ... server { listen {{ nginx_prot }} default_server; #listen [::]:80 default_server; ...
提供vars的定义变量
[root@HA2 nginx]# cat vars/main.yml runuser: daemon
提供handlers配置文件
[root@HA2 nginx]# cat handlers/main.yml - name: restart nginx service: name=nginx state=restarted
查看/etc/ansible/hosts
[root@HA2 ansible]# cat hosts | egrep -v "^#" [webserver] 172.16.0.2 nginx_prot=808 172.16.0.4 nginx_prot=8080 [dbserver] 172.16.0.5 hname=dbserver
ps:使用roles其实就是把yaml的整个结构拆分到每个对应的同名的文件目录中去
实例操作:
查看是否安装nginx
[root@HA2 nginx]# ansible webserver -a "rpm -q nginx" 172.16.0.4 | FAILED | rc=1 >> package nginx is not installed 172.16.0.2 | FAILED | rc=1 >> package nginx is not installed
运行前一定要检查,生产环境中
[root@HA2 nginx]# ansible-playbook --check nginx.yml PLAY [webserver] ***************************************************************TASK [setup] ******************************************************************* ok: [172.16.0.4] ok: [172.16.0.2] TASK [nginx : install nginx] *************************************************** changed: [172.16.0.4] changed: [172.16.0.2] TASK [nginx : install config] ************************************************** changed: [172.16.0.2] changed: [172.16.0.4] TASK [nginx : start nginx] ***************************************************** changed: [172.16.0.4] changed: [172.16.0.2] RUNNING HANDLER [nginx : restart nginx] //这里提示,主要看清楚,因为我remote没安装使用提示not find **************************************** fatal: [172.16.0.2]: FAILED! => {"changed": false, "failed": true, "msg": "systemd could not find the requested service \"‘nginx‘\": "} fatal: [172.16.0.4]: FAILED! => {"changed": false, "failed": true, "msg": "systemd could not find the requested service \"‘nginx‘\": "} NO MORE HOSTS LEFT ************************************************************* to retry, use: --limit @/etc/ansible/roles/nginx/nginx.retry PLAY RECAP ********************************************************************* 172.16.0.2 : ok=4 changed=3 unreachable=0 failed=1 172.16.0.4 : ok=4 changed=3 unreachable=0 failed=1 [root@HA2 nginx]#
运行,是正常OK的
[root@HA2 nginx]# ansible-playbook nginx.yml PLAY [webserver] *************************************************************** TASK [setup] ******************************************************************* ok: [172.16.0.4] ok: [172.16.0.2] TASK [nginx : install nginx] *************************************************** changed: [172.16.0.4] changed: [172.16.0.2] TASK [nginx : install config] ************************************************** changed: [172.16.0.4] changed: [172.16.0.2] TASK [nginx : start nginx] ***************************************************** changed: [172.16.0.4] changed: [172.16.0.2] RUNNING HANDLER [nginx : restart nginx] **************************************** changed: [172.16.0.2] changed: [172.16.0.4] PLAY RECAP ********************************************************************* 172.16.0.2 : ok=5 changed=4 unreachable=0 failed=0 172.16.0.4 : ok=5 changed=4 unreachable=0 failed=0 [root@HA2 nginx]#
查看,发现端口不一样,是因为nginx.conf.j2配置模板引用了/etc/ansible/hosts中的nginx_prot变量
[root@HA2 nginx]# ansible webserver -m shell -a " netstat -tunlp | grep nginx" 172.16.0.4 | SUCCESS | rc=0 >> tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 26689/nginx: master 172.16.0.2 | SUCCESS | rc=0 >> tcp 0 0 0.0.0.0:808 0.0.0.0:* LISTEN 26402/nginx: master
修改nginx的work process运行用户为user5(remote必须有该账号)及修改为80端口并且指定tags定义的ngxconf运行,并触发handles
[root@HA2 nginx]# ansible-playbook -t ngxconf -e "nginx_prot=80" -e"runuser=user5" nginx.yml PLAY [webserver] *************************************************************** TASK [setup] ******************************************************************* ok: [172.16.0.4] ok: [172.16.0.2] TASK [nginx : install config] ************************************************** changed: [172.16.0.2] changed: [172.16.0.4] RUNNING HANDLER [nginx : restart nginx] **************************************** changed: [172.16.0.4] changed: [172.16.0.2] PLAY RECAP ********************************************************************* 172.16.0.2 : ok=3 changed=2 unreachable=0 failed=0 172.16.0.4 : ok=3 changed=2 unreachable=0 failed=0
验证,如我们所想的那样
[root@HA2 nginx]# ansible webserver -m shell -a "netstat -tunlp| grep nginx;echo "" ;ps aux| grep ‘nginx: worker process‘| grep -v grep" 172.16.0.2 | SUCCESS | rc=0 >> tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 26856/nginx: master user5 26857 0.0 0.7 124736 3500 ? S 13:46 0:00 nginx: worker process user5 26858 0.0 0.7 124736 3500 ? S 13:46 0:00 nginx: worker process 172.16.0.4 | SUCCESS | rc=0 >> tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 27142/nginx: master user5 27143 0.0 0.7 124736 3500 ? S 13:46 0:00 nginx: worker process user5 27144 0.0 0.7 124736 3500 ? S 13:46 0:00 nginx: worker process
本文出自 “SunshineBoySZF” 博客,请务必保留此出处http://sunshineboyszf.blog.51cto.com/12087328/1867845
Ansible 使用roles安装服务
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。