运维开发网

ansible进阶循环

运维开发网 https://www.qedev.com 2020-09-08 13:57 出处:51CTO 作者:菜机运维
通过item和with_items对重复操作进行循环执行示例:hosts:jack6_1remote_user:rootgather_facts:notasks:name:touchfilefile:path:"{{item}}"state:touchwith_items:"a""b""c"在jack6_1主机上创建三

通过item和with_items 对重复操作进行循环执行

示例:


  • hosts: jack6_1

    remote_user: root

    gather_facts: no

    tasks:

    • name: touch file

      file:

      path: "{{item}}"

      state: touch

      with_items:

      • "a"
      • "b"
      • "c"

        在jack6_1主机上创建三个文件,由于是重复执行file模块,可以循环执行

        示例:

  • hosts: jack6_1

    remote_user: root

    vars:

    dirs:

    • "a"
    • "b"
    • "c"

      files:

    • "1"
    • "2"
    • "3"

      tasks:

      • name: remove dir

        file:

        path: "{{item}}"

        state: absent

        with_items: "{{dirs}}"

      • name: touch file

        file:

        path: "{{item}}"

        state: touch

        with_items: "{{files}}"

      • name: remove files

        file:

        path: "{{item}}"

        state: absent

        with_items: "{{files}}"

如下定义多个变量,重复执行某个模块

扫码领视频副本.gif

0

上一篇:

:下一篇

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号