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

触发器工作

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

相关推荐
我先去打把游戏先4 分钟前
VMware NAT 模式 Ubuntu 虚拟机「宿主机能上网、虚拟机 ping 不通外网 + apt 更新卡死」全故障复盘
linux·运维·vscode·单片机·嵌入式硬件·ubuntu·keil5
开压路机7 分钟前
基础IO
linux·运维·服务器
呉師傅21 分钟前
EPSON爱普生 L3118打印头【喷头】清洗方法
运维·服务器·网络·学习·电脑
小鸡毛程序员33 分钟前
从零搭建 Linux 开发服务器:VMware NAT 静态网络 + Docker + MySQL + Redis + 云服务器迁移
linux·服务器·网络
我命由我123451 小时前
Excel - Excel 单元格格式同时设置日期时间
运维·学习·职场和发展·excel·求职招聘·职场发展·学习方法
RSTJ_16251 小时前
PYTHON+AI LLM DAY SIXTY-SIX
服务器·开发语言·python
Cx330❀1 小时前
【Linux网络】一文吃透 TCP Socket 编程
linux·运维·服务器·开发语言·网络·tcp/ip
zizle_lin1 小时前
WSL初始化Ubuntu的使用
linux·运维·ubuntu·wsl
志栋智能1 小时前
轻量级 vs. 重平台:巡检超自动化的两种路径选择
运维·网络·人工智能·自动化
衫水1 小时前
项目后端服务 Docker 部署SOP (2026-06-04)
运维·docker·容器