playbook(循环)

  • 我们在编写playbook的时候,不可避免的要执行一些重复性操作,比如指安装软件包,批量创建用户,操作某个目录下的所有文件等。正如我们所说,ansible是一门简单的自动化语言,所以流程控制、循环语句这些编程语言的基本元素它同样都具备。
  • 首先编辑一下我们的ansible清单文件
  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_ide

  • with_items
  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_嵌套_02

  • 如果我们不想从返回的信息列表中循化,而想要循环自己定义的列表,可以使用以下方式
  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_03


  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_ide_04

  • 也可以这样写
---
- hosts: testB
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_items: [ a, b, c ]

从定义的列表中取指定的对应值输出(有点像字典中的键值对):

ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_05


ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_ide_06

  • 通过上面的例子,我们如何运用循环的知识在远程主机中创建1,2,3,4,四个文件呢?
  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_ide_07


  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_ide_08


  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_ide_09

  • 我们还可以将列表的内容写在变量中,引用变量
  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_10


  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_11


  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_ide_12

  • 循环结合shell模块,使shell模块循环执行列表中的命令:
  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_嵌套_13


  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_嵌套_14


  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_15

  • 我们会发现返回的内容太多了,但具体的结果都在results这一部分中,我们可以指定返回results中的内容
  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_16


  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_17

  • 还可以指定返回results中的某个信息对应的值
  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_18


  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_19


  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_20

  • 嵌套列表和其他关键字:
    之前我们定义的列表都是一个大的列表,那么如果我们定义一个嵌套列表,例如:[[a,b],[1,2,3]]
    它又会以怎样的形式输出呢?
  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_嵌套_21


  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_22

  • with_list
    从上面的结果可以看到它把列表中每个元素单独拿出来了,而不是像我们想要的效果:[a,b] [1,2,3]为两个整体,不拆开
    这是因为with_items关键字会把列表中的嵌套列表拉平,如果想要实现不拉平里面列表的效果,需要采用with_list关键字:
  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_嵌套_23


  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_嵌套_24

  • 这次返回的效果就是我们想要的了,其实拉平嵌套列表不只是有with_items关键字,with_flattened关键字和with_items作用很相似
  • with_together
  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_25


  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_26

  • with_together会将两个小列表中根据索引对应合并起来输出,如果两个列表中值的数量不对应,会自动匹配null
  • with_cartesian
    如果我们想要在远程主机中的/testdir下分别建立a\b\c三个目录,而三个目录中分别都有1,2,3三个子目录,此时我们可以通过ansible命令:
  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_嵌套_27


  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_ide_28

  • 如果我们想要用playbook去实现,就需要用到关键字with_cartesian
  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_29


  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_嵌套_30

  • 运用这个关键字去实现目录的建立
  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_ide_31


  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_32


  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_33

  • 与with_cartesian效果相同的关键字是with_nested
  • with_indexed_items关键字,自动添加索引
  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_34


  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_35

  • 如果我们给列表中嵌套列表
  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_36


  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_ide_37

  • 可以看到它会把嵌套的列表拉平,分别添加索引

如果再嵌套一层列表

ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_38


ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_39

  • with_sequence关键字:生成指定数字
#写法二:
---
- hosts: testB
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_sequence:
      start=1
      end=5
      stride=2
写法3:如果不指定start end stride参数,直接使用count=5来表示,默认为start=1 end=5 stride=1
---
- hosts: testB
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_sequence: count=5
  • 练习:如何利用with_sequence关键字,在远程主机中创建5个分别名为westos2 westos4 westos6 westos8 westos10的五个文件呢?

  • with_random_choice关键字:随机模块,在列表中随机选定一个数或字符
  • with_dict关键字:字典


    将user中的信息放入字典中,GiGi和kobe成为字典的key值,而冒号后面的性别信息成为value值
写法二:
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    users:
      GiGi: female
      kobe: male
  tasks:
  - debug:
      msg: "username: {{item.key}},user'gender: {{item.value}}"
    with_dict: "{{users}}"

我们也可以再加入一些具体的信息

ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_嵌套_40


ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_41


此时我们可以看到,它的key值还是Gigi和kobe,但value值中多了一些信息我们可以从value中再取出具体的值,让输出的内容更加明确

ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_嵌套_42


ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_43

  • with_subelements关键字:列表

    在定义变量时将Gigi和kobe分成了两个部分

    执行结果将name和gender作为一个整体,加上kobe的一个爱好chinese输出,再将name和gender作为一个整体,加上kobe的一个爱好basketball输出,说明hobby是这个列表中嵌套的一个列表

我们可以将这两个列表中的指定值拿出来输出

---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    users:
    - name: Gigi
      gender: female
      hobby:
        - basketball
        - chinese
    - name: kobe
      gender: male
      hobby:
        - basketball
  tasks:
  - debug:
      msg: "{{item.0.name}} 's hobby is {{item.1}}"
    with_subelements:
    - "{{users}}"
    - hobby

ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_嵌套_44

  • with_file关键字:查看本地ansible主机的文件内容
  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_嵌套_45


  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_嵌套_46


  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_嵌套_47

  • 此时我们看到它返回的信息的确是本地主机中指定文件的信息,说明这与指定的操作主机无关
  • with_fileglob关键字:查看指定目录下的文件
  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_嵌套_48


  • ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_ide_49

  • 注意:这里查看的是文件名而不是文件内容,只能看到文件而不能看目录
写法二:使用通配
---
- hosts: testB
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_fileglob:
    - /testdir/*
    - /opt/test*.??

ansible中使用wait_for模块监测端口关闭 ansible wait_for讲解_引用变量_50