小机灵鬼|自动化运维工具:ansible(二)( 七 )
[root@ansible ansible]#cat testfor2.yml ---- hosts: allremote_user: rootvars:ports:- listen_port: 80- listen_port: 82- listen_port: 83tasks:- name: copy conftemplate: src=http://kandian.youth.cn/root/ansible/templates/for2.conf.j2 dest=/root/for2.conf[root@ansible ansible]#cat templates/for2.conf.j2 {% for port in ports %}server{listen {{ port.listen_port }}}{% endfor %}[root@ansible ansible]#ansible-playbook testfor2.yml PLAY [all] *************************************************************************TASK [Gathering Facts] *************************************************************ok: [192.168.100.20]ok: [192.168.100.10]TASK [copy conf] *******************************************************************changed: [192.168.100.10]changed: [192.168.100.20]PLAY RECAP *************************************************************************192.168.100.10: ok=2changed=1unreachable=0failed=0skipped=0rescued=0ignored=0192.168.100.20: ok=2changed=1unreachable=0failed=0skipped=0rescued=0ignored=0node1节点查看:[root@node1 ~]# cat for2.conf server{listen 80}server{listen 82}server{listen 83}[root@ansible ansible]#cat testfor3.yml ---- hosts: allremote_user: rootvars:ports:- web1:port: 81name: web1.yang.comrootdir: /root/website1- web1:port: 82name: web2.yang.comrootdir: /root/website2- web1:port: 83name: web3.yang.comrootdir: /root/website3tasks:- name: copy conftemplate: src=/root/ansible/templates/for3.conf.j2 dest=/root/for3.conf[root@ansible ansible]#cat templates/for3.conf.j2 {% for p in ports %}server{listen {{ p.port }}servername {{ p.name }}documentroot {{ p.rootdir }}}{% endfor %}[root@ansible ansible]#ansible-playbook testfor3.yml PLAY [all] *************************************************************************TASK [Gathering Facts] *************************************************************ok: [192.168.100.20]ok: [192.168.100.10]TASK [copy conf] *******************************************************************changed: [192.168.100.20]changed: [192.168.100.10]PLAY RECAP *************************************************************************192.168.100.10: ok=2changed=1unreachable=0failed=0skipped=0rescued=0ignored=0192.168.100.20: ok=2changed=1unreachable=0failed=0skipped=0rescued=0ignored=0node1节点查看:[root@node1 ~]# cat for3.conf server{listen 81servername web1.yang.comdocumentroot /root/website1}server{listen 82servername web2.yang.comdocumentroot /root/website2}server{listen 83servername web3.yang.comdocumentroot /root/website3}[root@ansible ansible]#cat testfor4.yml ---- hosts: allremote_user: rootvars:ports:- web1:port: 81#name: web1.yang.comrootdir: /root/website1- web1:port: 82name: web2.yang.comrootdir: /root/website2- web1:port: 83#name: web3.yang.comrootdir: /root/website3tasks:- name: copy conftemplate: src=http://kandian.youth.cn/root/ansible/templates/for4.conf.j2 dest=/root/for4.conf[root@ansible ansible]#cat templates/for4.conf.j2 {% for p in ports %}server{listen {{ p.port }}{% if p.name is defined %}#如果被定义了则生成 , 没有定义则不生成servername {{ p.name }}{% endif %}documentroot {{ p.rootdir }}}{% endfor %}[root@ansible ansible]#ansible-playbook testfor4.yml PLAY [all] *************************************************************************TASK [Gathering Facts] *************************************************************ok: [192.168.100.10]ok: [192.168.100.20]TASK [copy conf] *******************************************************************changed: [192.168.100.20]changed: [192.168.100.10]PLAY RECAP *************************************************************************192.168.100.10: ok=2changed=1unreachable=0failed=0skipped=0rescued=0ignored=0192.168.100.20: ok=2changed=1unreachable=0failed=0skipped=0rescued=0ignored=0node1节点查看:[root@node1 ~]# cat for4.conf server{listen 81documentroot /root/website1}server{listen 82servername web2.yang.comdocumentroot /root/website2}server{listen 83documentroot /root/website3}
推荐阅读
- 小机灵鬼|干货速来!透彻剖析微服务架构设计模式,深入开发Java有奇效
- 中国新闻网|沈阳自动化所牵头制定“WIA-FA”一致性测试国家标准获批
- 科技匠|手把手教你如何使用免费自动化工具——Appium(安卓版)
- 执笔抒情|万茜发文向宁静郁可唯几位姐姐道歉,配图还在抖机灵
- 小机灵鬼|央视重磅关注天府新区这件事
- NBA|原创万茜发文向宁静郁可唯几位姐姐道歉,配图还在抖机灵
- 小机灵鬼|小米截胡?雷军宣布第三代自研屏下摄像头明年量产,中兴黯然失色
- 津爱改装车|我选择的开源运维监控系统-Prometheus
- 小机灵鬼|4K屏设计专业神器!ROG幻15成开学季最香生产力工具
- SpreadJS 应用案例:电力自动化在线数据采集报表系统
