Anisble中的任务执行控制

一、循环

1、简单循环

使用loop赋值列表的格式:

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

2、循环散列或字典列表

  • 可以赋予不同的服务不同的状态

    • name: create file
      hosts: 172.25.0.254
      tasks:
    • name: file module
      service:
      name: "{{ item.name}}"
      state: "{{ item.state }}"
      loop:
    • name: httpd
      state: started
    • name: vsftpd
      state: stopped

二、条件判定

1、when条件语句

此条件代表两者都满足时

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

此条件代表两者满足一个即可

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

2、条件判断

= value == "字符串",value == 数字
< value < 数字
> value > 数字
<= value <= 数字
>= value >= 数字
!= value != 数字

|------------------|-------------------------------------------------------------|
| is defined | 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) value in value2(value的值在value2列表中) |

上述的应用实例:

使用loop when 创建用户

复制代码
vim user.yml

- name: create user
  hosts: all
  vars:
    userlist:
    - name: user1
      id: 6666
      comment: user1 comment
    - name: user2
      id: 7777
  tasks:
  - name: create user2
    user:
      name: "{{item.name}}"
      uid: "{{item.id}}"
      comment: "{{item.comment}}"
      state: present
    when:
      item.comment is defined
    loop:
      "{{userlist}}"
  - name: create user
    user:
      name: "{{item.name}}"
      uid: "{{item.id}}"
      state: present
    when:
      item.comment is not defined
    loop:
      "{{userlist}}"

ansible-playbook user.yml  -C

三、触发器

notify:触发器当遇到更改时触发handlers
handlers:触发器触发后执行的动作

示例:

使用触发器 j2 loop 安装httpd服务并运行虚拟主机

复制代码
vim web.yml

webs:
- doc: /var/www/html
  index: "www.westos.org's page"

- 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"
复制代码
vim vhosts.conf.j2

{% for web in webs %}
{% if web.name is defined %}
<VirtualHost *:80>
  ServerName {{web.name}}
{% endif %}
{% if web.name is not defined %}
<VirtualHost _default_:80>
{% endif %}
  DocumentRoot {{web.doc}}
</VirtualHost>
{% endfor %}
复制代码
vim httpd.yml

- name: create virtualhost
  hosts: all
  vars_files: ./web.yml
  tasks:
  - name: yum
    yum:
      name: httpd
      state: present

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

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

  - name: create vhosts.conf
    template:
      src: ./vhosts.conf.j2
      dest: /etc/httpd/conf.d/vhost.conf
    notify: restart httpd

  handlers:
  - name: restart httpd
    service:
      name: httpd
      state: restarted

触发器工作

因为没有文件修改触发器没有触发所以不进行重启服务

相关推荐
少妇的美梦1 天前
logstash教程
运维
chen9451 天前
k8s集群部署vector日志采集器
运维
chen9451 天前
aws ec2部署harbor,使用s3存储
运维
christine-rr1 天前
linux常用命令(4)——压缩命令
linux·服务器·redis
東雪蓮☆1 天前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
qq_264220891 天前
LVS负载均衡群集和LVS+Keepalived群集
运维·负载均衡·lvs
乌萨奇也要立志学C++1 天前
【Linux】进程概念(二):进程查看与 fork 初探
linux·运维·服务器
雨落Liy1 天前
Nginx 从入门到进阶:反向代理、负载均衡与高性能实战指南
运维·nginx·负载均衡
Yyyy4821 天前
Nginx负载均衡集群实验步骤
运维·nginx·负载均衡
是Dream呀1 天前
时序数据库选型指南:Apache IoTDB企业级解决方案深度解析
apache·时序数据库·iotdb