黑帽联盟

标题: ansible笔记(25):循环(七) [打印本页]

作者: yun    时间: 2019-9-8 19:54
标题: ansible笔记(25):循环(七)
本帖最后由 yun 于 2019-9-11 17:14 编辑

ansible是一个系列文章,我们会尽量以通俗易懂的方式总结ansible的相关知识点。
ansible系列博文直达链接:ansible轻松入门系列
"ansible系列"中的每篇文章都建立在前文的基础之上,所以,请按照顺序阅读这些文章,否则有可能在阅读中遇到障碍。


假设你的ansible主机中有几个文件(注意:是ansible主机中的文件,不是远程目标主机中的文件),你想要获取到这些文件的内容,那么可以借助"with_file"关键字,循环的获取到这些文件的内容,示例如下:
  1. ---
  2. - hosts: test70
  3.   remote_user: root
  4.   gather_facts: no
  5.   tasks:
  6.   - debug:
  7.       msg: "{{ item }}"
  8.     with_file:
  9.     - /testdir/testdir/a.log
  10.     - /opt/testfile
复制代码
如上例所示,我们定义了一个列表,这个列表中有两个文件路径,分别是"/testdir/testdir/a.log"和"/opt/testfile",这两个文件都是ansible主机中的文件,我们通过"with_file"关键字处理了这个列表,那么我们执行一下上例playbook,看看能不能获取到对应的文件内容,执行上例后debug模块输出信息如下:
  1. TASK [debug] *******************
  2. ok: [test70] => (item=aaa) => {
  3.     "changed": false,
  4.     "item": "aaa",
  5.     "msg": "aaa"
  6. }
  7. ok: [test70] => (item=test) => {
  8.     "changed": false,
  9.     "item": "test",
  10.     "msg": "test"
  11. }
复制代码
如上所示,上例 playbook成功的获取到了文件列表中每个文件的内容,而且细心如你一定已经发现了,上例playbook的目标主机是test70,test70并非是ansible主机,而是一个远程目标主机,我的测试环境中,test71才是ansible主机,所以,可以发现,无论目标主机是谁,我们都可以通过"with_file"关键字获取到ansible主机中的文件内容,操作很简单吧。

了解完"with_file"关键字,再来聊聊另外一个关键字,它就是"with_fileglob"
"with_file"是用来获取文件内容的,而"with_fileglob"是用来匹配文件名称的,我们可以通过"with_fileglob"关键字,在指定的目录中匹配符合模式的文件名,"with_file"与"with_fileglob"也有相同的地方,它们都是针对ansible主机的文件进行操作的,而不是目标主机,那么,我们来看一个"with_fileglob"小示例,示例如下:
  1. ---
  2. - hosts: test70
  3.   remote_user: root
  4.   gather_facts: no
  5.   tasks:
  6.   - debug:
  7.       msg: "{{ item }}"
  8.     with_fileglob:
  9.     - /testdir/*
复制代码
如上例所示,我们定义了一个列表,这个列表中只有一个值,这个值是一个路径,路径中包含一个通配符,按照我们通常的理解,"/testdir/*"应该代表了/testdir目录中的所有文件,我们用"with_fileglob"处理了这个列表,那么我们来看看执行效果,执行后返回信息如下
  1. TASK [debug] *************************
  2. ok: [test70] => (item=/testdir/testfile) => {
  3.     "changed": false,
  4.     "item": "/testdir/testfile",
  5.     "msg": "/testdir/testfile"
  6. }
  7. ok: [test70] => (item=/testdir/test.sh) => {
  8.     "changed": false,
  9.     "item": "/testdir/test.sh",
  10.     "msg": "/testdir/test.sh"
  11. }
复制代码
从返回信息可以看出,上例playbook中的"/testdir/*"一共匹配到了两个文件,那么,"/testdir"目录中只有这两个文件么,我们来确认一下,查看"/testdir"目录中的文件,如下:
  1. [root@test71 testdir]# ll /testdir
  2. total 16
  3. drwxr-xr-x 2 root root 4096 Jul 27 17:26 ansible
  4. drwxr-xr-x 2 root root 4096 Jul 19 16:05 testdir
  5. -rw-r--r-- 1 root root   99 May 25 14:06 testfile
  6. -rwxr--r-- 1 root root   81 Mar 17 13:28 test.sh
复制代码
从上述信息可以看出,"/testdir"目录中一共有四项,两项是目录,两项是文件,而之前playbook匹配到的只有文件,并不包含目录,所以,我们需要注意的是,"with_fileglob"只会匹配指定目录中的文件,而不会匹配指定目录中的目录。
上例的playbook中,我们只指定了一个路径,当然,我们也可以指定多个路径,示例playbook如下:
  1. ---
  2. - hosts: test70
  3.   remote_user: root
  4.   gather_facts: no
  5.   tasks:
  6.   - debug:
  7.       msg: "{{ item }}"
  8.     with_fileglob:
  9.     - /testdir/*
  10.     - /opt/test*.???
复制代码
上例中我们定义了一个列表,这个列表中有两项,第一项表示匹配"/testdir"目录下的文件,第二项表示匹配"/opt"目录下,以"test"开头,以". 任意3个字符"结尾的文件,比如"testa.123"或者"testfile.yml",所以,执行上例playbook以后,这两个目录中符合条件的文件都会被匹配到,最终循环的被debug模块输出,你一定已经明白了,快动手试试吧~

这篇文章就总结到这里,希望能够对你有所帮助~






欢迎光临 黑帽联盟 (https://bbs.cnblackhat.com/) Powered by Discuz! X2.5