一、with_tiems 迭代 当有需要重复性执行的任务时,可以使用迭代机制 (with_items) with_items模块一般放到模块的末尾, 与模块同一缩进级别 {{ item }}将循环迭代with_items中的值 - hosts: web2 tasks: - name: fuzhi c
with_items:- { name:"www", uid:"666", group:"www", create_home:"false", shell:"/sbin/nologin"}- { name:"lhd", uid:"777", group:"lhd", create_home:"true", shell:"/sbin/nologin"}- { name:"test", uid:"888", group:"test", create_home:"false", shell:"/bin/bash"}#...
每个列表项中可能都包含一个或多个字典,既然with_items迭代的是列表项,那么肯定也能迭代列表中的各字典。 例如: tasks: - command: echo {{ item }} with_items: [ 0, 2, 4, 6, 8, 10 ] register: num - debug: msg="{% for i in num.results %} {{i.stdout}} {% endfor %}" 1. 2....
循环with_item 一次性创建多个 - hosts: web tasks: - name: crateuser user: name={{item}} with_items: - alex20 - alex21 - alex22 1. 2. 3. 4. 5. 6. 7. 8. - hosts: web tasks: - name: crateuser user: name={{item}} with_items: - alex30 - alex31 - alex32 - name: cra...
条件判断 when 循环语句 with_items 触发器 handlers 标签tags 包含include 忽略错误 ignore_error 错误处理 change 条件判断 假设我们安装Apache,在centos上安装的是httpd,在Ubuntu上安装的是httpd2,因此我们需要判断主机信息,安装不同的软件。 代码语言:javascript 代码运行次数:0 运行 AI代码解释 - hosts: web tasks...
08 When-示例1 09 with_items:迭代 10 示例1:利用迭代一次创建多个文件,安装多个命令包 11 示例2:使用迭代创建组 12 示例3:使用迭代配合字典创建用户与组 13 示例1:template,for 14 示例2:template,for,引用字典 15 示例3:for循环中调用字典 16 示例4:for循环中调用if ...
with_items:"{{csr_ifs}}"#导入config_data.yaml中csr_ifs这个部分的配置内容 when:(item.router==inventory_hostname)#条件判断,当IP地址等于router的IP才执行此任务,用于CSR1和CSR2配置不同的IP地址 -name:noshutdowninterface#ios_interface(打开接口) ...
when: ansible_ens192.ipv4.address == '{{ item.ip }}' with_items: - { ip: "172.16.60.220", password: 'haha@123' } - { ip: "172.16.60.221", password: 'kevin@123' } - { ip: "172.16.60.222", password: 'bobo@123' }
shell: "docker build -t repo ." args: chdir: /tmp/repo when: "{{ item }}_src"|changed register: "{{ item }}_image" with_items: images - name: push image shell: "docker push repo" when: "{{ item }}_image"|changed with_items: images...
with_items: "{{ data }}" 从Ansible2.5开始,建议使用loop关键字编写循环。 1.1.4 将Register变量与Loop一起使用 register关键字也可以捕获循环任务的输出。以下代码片段显示了循环任务中register变量的结构: [root@localhost ~]# vim loop_register.yml--- ...