码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Ansible循环与判断


    ansible循环和判断


    文章目录

    • ansible循环和判断
      • 一、循环
        • 1、with_items迭代列表
        • 2 、with_dict迭代字典
        • 3、with_fileglob迭代文件
        • 4、with_lines迭代行
        • 5、with_nested嵌套迭代
        • 6、with_sequence 排序列(start是从什么开始,end结束 stride每隔多少)
        • 7、with_random_choice 随机获得列表中的一个值
        • 8、loop
      • 二、ansible判断
        • 1、When
      • 拓展

    一、循环


    1、with_items迭代列表

    比如Linux中依次需要安装多个软件包时,我们可以使用with_items迭代功能进行实现
    例如:
    安装httpd、samba、samba-client软件包时

    [student@ansible ansible]$  vim b.yml
    - name: install packages
      hosts: node1
      tasks:
        - name: yum_repo1
          yum_repository:
            file: server
            name: baseos
            description: rhel8
            baseurl: file:///mnt/BaseOS
            enabled: yes
            gpgcheck: no
     
        - name: yum_repo2
          yum_repository:
            file: server
            name: appstream
            description: appstream
            baseurl: file:///mnt/AppStream
            enabled: yes
            gpgcheck: no
     
        - name: mount cdrom
    mount:
            src: /dev/cdrom
            path: /mnt
            fstype: iso9660
            state: mounted
     
        - name: install pks
          yum:
            name: "{
      {item}}"
            state: present
          with_items:
            - httpd
            - samba
            - samba-client
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    2 、with_dict迭代字典

    item.key对应着是字典的键,item.value对应着字典的值

    [student@ansible ansible]$  vim c.yml
    ---
    - name: test
      hosts: node1
      tasks:
        - name: debug
          debug:
            msg: "{
      {item.key}} & {
      {item.value}}"
          with_dict:
            address: 1
            netmask: 2
            gateway: 3
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    3、with_fileglob迭代文件

    比如拷贝多个文件到受控主机上时,可以使用

    [student@ansible ansible]$ vim d.yml
    ---
    - name: test
      hosts: node1
      tasks:
        - name: cp file
          copy:
            src: "{
      {item}}"
            dest: /tmp/
          with_fileglob:
            - /tmp/*.sh
            - /tmp/*.py
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    4、with_lines迭代行

    With_lines可以将命令行的输出结果按行迭代

    [student@ansible ansible]$  vim e.yml
    ---
    - name: test
      hosts: node1
      tasks:
        - name: cp file
          copy:
            src: "{
      {item}}"
            dest: /tmp/
          with_lines:
            - find  /etc/ansible  -name  "*.yml"
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    5、with_nested嵌套迭代

    a和b分别与1、2、3连接组合

    [student@ansible ansible]$ vim f.yml
    ---
    - name: test
      hosts: node1
      tasks:
        - name: debug
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    性能优化:记一次树的搜索接口优化思路
    NumPy 数组迭代与合并详解
    如何应对软件可变性?这4种常用的方法肯定要知道
    同花顺,通达信,东方财富股票竞价,早盘板块、概念、题材竞价数据接口
    __attribute__((visibility(“default“)))含义
    geemap学习笔记011:可视化遥感影像随时间的变化
    JavaOOP-类、对象、方法、变量作用域及JavaDoc注释
    回溯:组合、子集、排列 算法总结
    “小”Bug,大能量
    Day09 系统设置模块设计
  • 原文地址:https://blog.csdn.net/qq_65441164/article/details/127620607
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号