Ansible 使用roles安装服务
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ansible 使用roles安装服务相关的知识,希望对你有一定的参考价值。
创建所需要的目录
[[email protected] nginx]# mkdir /etc/ansible/roles/nginx/{files,handlers,meta,vars,tasks,templates,default} -pv
新建tasks任务
[[email protected] nginx]# cd /etc/ansible/roles/nginx/ //进入nginx的roles目录[[email protected] nginx]# cat tasks/main.yml //查看配置文件内容- name: install nginx yum: name=nginx state=present - name: install config template: src=files/nginx.conf.j2 dest=/etc/nginx/nginx.conf notify: restart nginx tags: ngxconf - name: start nginx service: name=nginx state=started enabled=true
提供nginx的template文件
[[email protected] 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的定义变量
[[email protected] nginx]# cat vars/main.yml runuser: daemon
提供handlers配置文件
[[email protected] nginx]# cat handlers/main.yml - name: restart nginx service: name=nginx state=restarted
查看/etc/ansible/hosts
[[email protected] 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
[[email protected] 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
运行前一定要检查,生产环境中
[[email protected] 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 [[email protected] nginx]#
运行,是正常OK的
[[email protected] 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 [[email protected] nginx]#
查看,发现端口不一样,是因为nginx.conf.j2配置模板引用了/etc/ansible/hosts中的nginx_prot变量
[[email protected] 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
[[email protected] 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
验证,如我们所想的那样
[[email protected] 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安装服务的主要内容,如果未能解决你的问题,请参考以下文章
ansible-playbook roles 安装zabbix-agent 监控 php