yaml语法之 条件测试,循环,tags 介绍
条件测试
when语句:在tasks中使用。
[root@ansible ~]# vim test5.yaml - hosts: centos6-7 remote_user: root tasks: - name: install httpd yum: name=httpd state=latest - name: start centos6 httpd shell: service httpd start when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "6" --> facts变量 - name: start centos7 httpd shell: systemctl start httpd.service when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"
循环:迭代,需要重复执行的任务
对迭代项的引用,固定变量名为 "item",使用with_item属性给定要迭代的元素;
[root@ansible ~]# vim test6.yaml - hosts: centos7 remote_user: root tasks: - name: create groups group: name={{ item }} state=present with_items: - groupx1 - groupx2 - name: create users user: name={{ item.name }} group={{ item.group }} state=present with_items: - {name: 'userx1', group: 'groupx1'} - {name: 'userx2', group: 'groupx2'}
tags:给指定的任务定义一个调用标识
[root@ansible ~]# vim test7.yaml - hosts: centos7 remote_user: root tasks: - name: install httpd yum: name=httpd state=latest - name: copy config file copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf tags: httpdconf - name: start httpd service service: name=httpd state=started - name: print date shell: /usr/bin/date tags: showdate
转载于:https://blog.51cto.com/sixijie123/1878082