小机灵鬼|自动化运维工具: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}
小机灵鬼|自动化运维工具:ansible(二)迭代:当有需要重复性执行的任务时 , 可以使用迭代机制
对迭代项的引用 , 固定变量名为"item"
要在task中使用with_items给定要迭代的元素列表
列表格式:
  • 字符串
  • 字典

小机灵鬼|自动化运维工具:ansible(二)迭代嵌套子变量
[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' }
小机灵鬼|自动化运维工具:ansible(二)
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}


推荐阅读