Ansible 测试失败,因为 lookup('file', '/path/to/file') 返回 gitlab-ci 中的旧内容



我使用 assert 模块编写了一些 ansible 测试。真正的任务会更改文件,测试会读取它的内容并检查它是否包含一些字符串。

在普通虚拟机(ubuntu EC2 实例(上一切正常。但是,它在 docker 容器中的 gitlab-ci 中失败。如果我听起来很困惑,请耐心等待。我很困惑。

主任务和调试任务如下所示:

- name: Disable core dumps
become: true
pam_limits: 
comment: " disable core dumps"
domain: '*'
limit_item: core
limit_type: hard
value: 0
- name: debug file content
become: true
vars:
contents: "{{ lookup('file', '/etc/security/limits.conf') }}"
debug:
var: contents

检查调试输出,我可以看到我的行* hard core 0不在内容变量中。因此,像这样的检查失败:

name: Assert that the line "* hard core 0" is in limits.conf
become: true
vars:
contents: "{{ lookup('file', '/etc/security/limits.conf') }}"
assert:
that:
contents is search('[^#][*]s+hards+cores+0.*')

但是,此检查成功:

- name: Get line with core configuration in limits.conf
shell: grep -o -E '^*s+hards+cores+0.*$' /etc/security/limits.conf
register: core_line

问题似乎真的是文件查找看不到其他人看到的文件,其他人是登录到机器的人,或者 grep 命令。

同样,在 VM 上,文件内容正确,测试成功,如预期的那样。但是,gitlab-ci 启动了一些 docker 容器(这似乎是标准的(和一些虚拟机(这是这个环境的特殊之处 - 在这种情况下,您将无法帮助我,我很抱歉打扰您(。在路上的某个地方,事情变得奇怪,我感到困惑

这不是pam_limits模块的问题。它工作得很好。当我使用lineinfile模块时,也会发生同样的情况。

ansible 版本是 ansible 2.6.4、python 2.7.6、GitLab Community Edition 10.8.7

查找是在 ansible 主机(执行 ansible-playbook 二进制文件的主机(上执行的。相反,模块在目标主机上执行(hosts: foobar剧中的语句(。

如果要使用来自远程主机的数据,可以使用fetch,也可以只使用command: cat(command: grep(。

最新更新