小机灵鬼|自动化运维工具:ansible(二)( 六 )
格式:
- 在task后添加when子句即可使用条件测试;when语句支持Jinja2表达式语法
- 对不同centos版本针对配置
[root@ansible ansible]#ansible all -m setup -a 'filter="*distribution*"'192.168.100.20 | SUCCESS => {"ansible_facts": {"ansible_distribution": "CentOS","ansible_distribution_file_parsed": true,"ansible_distribution_file_path": "/etc/redhat-release","ansible_distribution_file_variety": "RedHat","ansible_distribution_major_version": "7","ansible_distribution_release": "Core","ansible_distribution_version": "7.4","discovered_interpreter_python": "/usr/bin/python"},"changed": false}192.168.100.10 | SUCCESS => {"ansible_facts": {"ansible_distribution": "CentOS","ansible_distribution_file_parsed": true,"ansible_distribution_file_path": "/etc/redhat-release","ansible_distribution_file_variety": "RedHat","ansible_distribution_major_version": "7","ansible_distribution_release": "Core","ansible_distribution_version": "7.4","discovered_interpreter_python": "/usr/bin/python"},"changed": false}对迭代项的引用 , 固定变量名为"item"
要在task中使用with_items给定要迭代的元素列表
列表格式:
- 字符串
- 字典
[root@ansible ansible]#cat testitem.yml ---- hosts: allremote_user: roottasks:- name: create some groupsgroup: name={{ item }}with_items:- g1- g2- g3- name: create some usersuser: name={{ item.name }} group={{ item.group }}with_items:- { name: 'user1', group: 'g1' }- { name: 'user2', group: 'g2' }- { name: 'user3', group: 'g3' }playbook中的template for if:
[root@ansible ansible]#cat templates/for1.conf.j2 {% for port in ports %}server{listen {{ port }}}{% endfor %}[root@ansible ansible]#cat testfor.yml ---- hosts: allremote_user: rootvars:ports:- 81- 82- 83tasks:- name: copy conftemplate: src=http://kandian.youth.cn/root/ansible/templates/for1.conf.j2 dest=/root/for1.conf[root@ansible ansible]#ansible-playbook -C testfor.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=0[root@ansible ansible]#ansible-playbook testfor.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=0 node1节点查看:[root@node1 ~]# cat for1.conf server{listen 81}server{listen 82}server{listen 83}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 小机灵鬼|干货速来!透彻剖析微服务架构设计模式,深入开发Java有奇效
- 中国新闻网|沈阳自动化所牵头制定“WIA-FA”一致性测试国家标准获批
- 科技匠|手把手教你如何使用免费自动化工具——Appium(安卓版)
- 执笔抒情|万茜发文向宁静郁可唯几位姐姐道歉,配图还在抖机灵
- 小机灵鬼|央视重磅关注天府新区这件事
- NBA|原创万茜发文向宁静郁可唯几位姐姐道歉,配图还在抖机灵
- 小机灵鬼|小米截胡?雷军宣布第三代自研屏下摄像头明年量产,中兴黯然失色
- 津爱改装车|我选择的开源运维监控系统-Prometheus
- 小机灵鬼|4K屏设计专业神器!ROG幻15成开学季最香生产力工具
- SpreadJS 应用案例:电力自动化在线数据采集报表系统
