Ansible中的任务执行控制

循环

简单循环

复制代码
{{item}} 迭代变量名称
loop:
- value1
- value2
- ...                 //赋值列表


{{item}}        //迭代变量名称

循环散列或字典列表

复制代码
- name: create file
  hosts: host1
  tasks:
  - name: file moudle
    service:
      name: "{{ item.name }}"
      state: "{{ item.state }}"
    loop:
    - name: httpd
      state: started
    - name: vsftpd
      state: stopped
~

条件

复制代码
when:
 - 条件1
 - 条件2

条件判断

复制代码
=	        //value == "字符串",value == 数字	解释
<	        //value < 数字	
>	        //value > 数字	
<=	        //value <= 数字	
>=	        //value >= 数字	
!=	        //value != 数字	
is defined value	        //value is defined	变量存在
is not defined	            //value is not defined	变量不存在
in	            //value is in value	变量为
not in	            //value is not in value	变量不为
bool变量 为true	    value	    //value的值为true
bool变量 false	    not value	//value的值为false

多条条件组合

复制代码
when:
 条件1 and 条件2
 - 条件1
 - 条件2            //当条件1和条件2都为真

when:
 条件1 or 条件2        //满足其中一个

when: >
 条件1
 or
 条件2                //满足其中一个

创建两个用户 user1user2user1 的 UID 是 6666,注释是 user1 commentuser2 的 UID 是 7777,没有注释。

复制代码
vim userlist.yml
userlist:
  - name: user1
    id: 6666
    comment: user1 comment
  - name: user2
    id: 7777

vim usercreate.yml
- name: create user
  hosts: all
  vars_files: ./userlist.yml
  tasks:
    - name: create user with comment
      user:
        name: "{{item.name}}"
        uid: "{{item.id}}"
        comment: "{{item.comment}}"
        state: present
      when: item.comment is defined
      loop:
        "{{userlist}}"

    - name: create user without comment
      user:
        name: "{{item.name}}"
        uid: "{{item.id}}"
        state: present
      when: item.comment is not defined
      loop:
        "{{userlist}}"

触发器

复制代码
notify:         //触发器当遇到更改是触发handlers
handlers:             //触发器触发后执行的动作

在指定的服务器上部署网站,并创建一个简单的主页

复制代码
vim webs.yml
webs:
  - name: bbs.westos.org
    doc: /var/www/virtual/westos.org/bbs/html
    index: "bbs.westos.org's page"

  - name: login.westos.org
    doc: /var/www/virtual/westos.org/login/html
    index: "login.westos.org's page"

  - name: www.westos.org
    doc: /var/www/html
    index: "www.westos.org's page"
复制代码
vim html.j2
{% for web in webs %}
{% if web.name is not defined %}
<VirtualHost _default_:80>
{% elif web.name is defined %}
<VirtualHost *:80>
        ServerName {{web.name}}
{% endif %}
        DocumentRoot {{web.doc}}
</VirtualHost>
{% endfor %}
复制代码
vim web.yml
- name: Create website index page
  hosts: all
  vars_files: ./webs.yml

  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: present

    - name: service
      service:
        name: httpd
        state: started
        enabled: no

    - name: Create index.html for each website
      template:
        src: ./html.j2
        dest: "/etc/httpd/conf.d/vhost.conf"
      notify: restart httpd

    - name: create directory
      lineinfile:
        path: "{{item.doc}}/index.html"
        line: "{{ item.index }}"
        create: yes
      loop: "{{webs}}"

  handlers:
    - name: restarted httpd
      service:
        name: httpd
        state: restarted
相关推荐
leo__5203 天前
自动化运维:使用Ansible简化日常任务
运维·自动化·ansible
风清再凯8 天前
自动化工具ansible,以及playbook剧本
运维·自动化·ansible
IT乌鸦坐飞机8 天前
ansible部署数据库服务随机启动并创建用户和设置用户有完全权限
数据库·ansible·centos7
遇见火星21 天前
如何使用Ansible一键部署MinIO集群?
ansible
粥周粥21 天前
ANSIBLE
ansible
码农101号21 天前
Linux中ansible模块补充和playbook讲解
linux·运维·ansible
码农101号21 天前
Linux的Ansible软件基础使用讲解和ssh远程连接
ansible
烟雨书信22 天前
ANSIBLE运维自动化管理端部署
运维·自动化·ansible
碎碎-li22 天前
ANSIBLE(运维自动化)
运维·自动化·ansible
@donshu@25 天前
Linux运维-ansible-python开发-获取inventroy信息
linux·运维·ansible