创建所需要的目录
[iyunv@HA2 nginx]# mkdir /etc/ansible/roles/nginx/{files,handlers,meta,vars,tasks,templates,default} -pv
新建tasks任务
[iyunv@HA2 nginx]# cd /etc/ansible/roles/nginx/ //进入nginx的roles目录[iyunv@HA2 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文件
[iyunv@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的定义变量
[iyunv@HA2 nginx]# cat vars/main.yml
runuser: daemon
提供handlers配置文件
[iyunv@HA2 nginx]# cat handlers/main.yml
- name: restart nginx
service: name=nginx state=restarted
查看/etc/ansible/hosts
[iyunv@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
[iyunv@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
运行前一定要检查,生产环境中
[iyunv@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
[iyunv@HA2 nginx]#
运行,是正常OK的
[iyunv@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
[iyunv@HA2 nginx]#
查看,发现端口不一样,是因为nginx.conf.j2配置模板引用了/etc/ansible/hosts中的nginx_prot变量
[iyunv@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
[iyunv@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
验证,如我们所想的那样
[iyunv@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
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com