Ansible:第二个 lineinfile 任务不执行任何操作

Ansible: second lineinfile task doesn't do anything

我正在尝试使用 ansible 的 lineinfile 模块向 named.conf 中的 2 个 ACL 添加条目,但似乎只有第一个任务是 运行.

配置为:

acl operacao {
        192.168.1.33;
        192.168.2.29;
        10.10.0.0/24;
        };

acl monitoring {
        10.10.0.0/24;
        };

所以我创建了一个包含 2 个任务的剧本:

    - name: add to operacao acl
      lineinfile:
        path: "/etc/named.conf"
        line: "        10.2.2.0/24;"
        insertafter: "acl operacao"
      register: acl_op

    - name: add to monitoring acl
      lineinfile:
        path: "/etc/named.conf"
        line: "        10.2.2.0/24;"
        insertafter: "acl monitoring"
      register: acl_mon

当 运行 带有 --check 标志时,它会正确预测将插入行的位置:

TASK [add to operacao acl] **************************************************************************************************************************************************************************************************************************************************************************************************
--- before: /etc/named.conf (content)
+++ after: /etc/named.conf (content)
@@ -21,6 +21,7 @@
 };

 acl operacao {
+        10.2.2.0/24;
         192.168.1.33;
         192.168.2.29;
         10.10.0.0/24;

changed: [dnshost01]

TASK [add to monitoring acl] ************************************************************************************************************************************************************************************************************************************************************************************************
--- before: /etc/named.conf (content)
+++ after: /etc/named.conf (content)
@@ -27,6 +27,7 @@
         };

 acl monitoring {
+        10.2.2.0/24;
         10.10.0.0/24;
         };

然而,当 运行 没有 --check 标志时,它只显示第一个任务的变化,第二个不显示 运行:

TASK [add to operacao acl] **************************************************************************************************************************************************************************************************************************************************************************************************
--- before: /etc/named.conf (content)
+++ after: /etc/named.conf (content)
@@ -21,6 +21,7 @@
 };

 acl operacao {
+        10.2.2.0/24;
         192.168.1.33;
         192.168.2.29;
         10.10.0.0/24;

changed: [dnshost01]

TASK [add to monitoring acl] ************************************************************************************************************************************************************************************************************************************************************************************************
ok: [dnshost01]

这总是 运行 和 --flush-cache

这里发生了什么?我是误会了什么,还是 ansible 有问题?

我在 RHEL8.5 / python 3.6.8 上 运行ning ansible 2.9.27。此行为发生在 RHEL6 和 RHEL7 主机上。

您误解了模块的工作原理。

insertafter 选项指示插入行的位置 如果它不存在 .

当您的 运行 处于检查模式时,由于该行未插入您的第一个任务(因为您只是在检查),您在第二个任务中仍然没有匹配项,因此 ansible 报告它会使改变。

要使上述工作正常,您必须使用正则表达式,匹配每个块上的多行以查看它是否存在并插入 back-reference 以保留现有内容。这本身可能会很痛苦地完成和维护。您当前和未来 co-workers(甚至几周后的您自己...)可能很难弄清楚到底做了什么(复杂的正则表达式操作无法自我解释...)。

并且由于 lineinfile 无论如何都不支持 multi-line 正则表达式,您将不得不通过 replace module,这可能会给您的上下文带来其他挑战。

复杂使用 lineinfilereplace 来处理现有内容 in-place 通常表明您应该开始使用 template