将文件行插入Ansible中另一个依赖的正则表达式过滤器中



我会将行从文件/tmp/test1复制到文件/tmp/test2/tmp/test1包含:

+ : argument1 : ALL
+ : argument2 : ALL
+ : @test1 : ALL
+ : @test2 : ALL
+ : @test3 : ALL

/tmp/test2包含:

+ : argument1.1 : ALL
+ : argument2192 : ALL
+ : @example : ALL
+ : @test2 : ALL
+ : @example1 : ALL

所以我的主要目标是插入文件/tmp/test1/tmp/test2中不存在的每一行并且添加的行必须添加在最后一行的末尾,该行包含+ ::^[[:alpha:]]^@之后的相同开头,所以/tmp/test2应该看起来像这样:

+ : argument1.1  : ALL
+ : argument2192 : ALL
+ : argument1 : ALL
+ : argument2 : ALL
+ : @example : ALL
+ : @test2 : ALL
+ : @example1 : ALL
+ : @test1 : ALL
+ : @test3 : ALL

我从Vladimir Botka_answer的回答中得到了关于如何合并这两个文件的回复。但对我来说,造成问题的是:每个<line>开始时的+ :,如果它以@alpha开始,则必须在之后应用过滤器

前缀正则表达式放入变量中,并测试前5个字符。例如,

- name: insert line
lineinfile:
path: /tmp/test2
line: "{{ item }}"
insertafter: "{{ (item[0:5] == prefix)|ternary('EOF', omit) }}"
insertbefore: "{{ (item[0:5] != prefix)|ternary(regex, omit) }}"
firstmatch: true
loop: "{{ tmp_content.stdout_lines }}"
vars:
prefix: '+ : @'
regex: '^+ : @.*$'

给出

shell> cat /tmp/test2
+ : argument1.1 : ALL
+ : argument2192 : ALL
+ : argument1 : ALL
+ : argument2 : ALL
+ : @example : ALL
+ : @test2 : ALL
+ : @example1 : ALL
+ : @test1 : ALL
+ : @test3 : ALL

  • 完整的测试剧本示例
- hosts: localhost
tasks:
- name: check test1 content
command: cat /tmp/test1
register: tmp_content
changed_when: false
- name: insert line
lineinfile:
path: /tmp/test2
line: "{{ item }}"
insertafter: "{{ (item[0:5] == prefix)|ternary('EOF', omit) }}"
insertbefore: "{{ (item[0:5] != prefix)|ternary(regex, omit) }}"
firstmatch: true
loop: "{{ tmp_content.stdout_lines }}"
vars:
prefix: '+ : @'
regex: '^+ : @.*$'
  • 剧本是幂等的。请参阅下面diff_mode的输出
shell> ansible-playbook pb.yml --diff
...
TASK [insert line] ****************************************
--- before: /tmp/test2 (content)
+++ after: /tmp/test2 (content)
@@ -1,5 +1,6 @@
+ : argument1.1 : ALL
+ : argument2192 : ALL
++ : argument1 : ALL
+ : @example : ALL
+ : @test2 : ALL
+ : @example1 : ALL
changed: [localhost] => (item=+ : argument1 : ALL)
--- before: /tmp/test2 (content)
+++ after: /tmp/test2 (content)
@@ -1,6 +1,7 @@
+ : argument1.1 : ALL
+ : argument2192 : ALL
+ : argument1 : ALL
++ : argument2 : ALL
+ : @example : ALL
+ : @test2 : ALL
+ : @example1 : ALL
changed: [localhost] => (item=+ : argument2 : ALL)
--- before: /tmp/test2 (content)
+++ after: /tmp/test2 (content)
@@ -5,3 +5,4 @@
+ : @example : ALL
+ : @test2 : ALL
+ : @example1 : ALL
++ : @test1 : ALL
changed: [localhost] => (item=+ : @test1 : ALL)
ok: [localhost] => (item=+ : @test2 : ALL)
--- before: /tmp/test2 (content)
+++ after: /tmp/test2 (content)
@@ -6,3 +6,4 @@
+ : @test2 : ALL
+ : @example1 : ALL
+ : @test1 : ALL
++ : @test3 : ALL
changed: [localhost] => (item=+ : @test3 : ALL)

Q:";将包含@的行添加到包含@的最后一个匹配线之后">

A: 在insertafterinsertbefore中使用正则表达式,并根据前缀选择第一匹配。从技术上讲,将@行附加到@块,并将其他行前置。例如,给定文件

shell> cat /tmp/test2
+ : argument1.1 : ALL
+ : argument2192 : ALL
+ : @example : ALL
+ : @test2 : ALL
+ : @example1 : ALL
+ : <rubish>

下面的任务完成的工作

- name: insert line
lineinfile:
path: /tmp/test2
line: "{{ item }}"
insertafter: "{{ (item[0:5] == prefix)|ternary(regex, omit) }}"
insertbefore: "{{ (item[0:5] != prefix)|ternary(regex, omit) }}"
firstmatch: "{{ (item[0:5] != prefix)|ternary(true, false) }}"
loop: "{{ tmp_content.stdout_lines }}"
vars:
prefix: '+ : @'
regex: '^+ : @.*$'

请参阅下方diff_mode的输出

shell> ansible-playbook pb.yml --diff
...
TASK [insert line] ******************************************
--- before: /tmp/test2 (content)
+++ after: /tmp/test2 (content)
@@ -1,5 +1,6 @@
+ : argument1.1 : ALL
+ : argument2192 : ALL
++ : argument1 : ALL
+ : @example : ALL
+ : @test2 : ALL
+ : @example1 : ALL
changed: [localhost] => (item=+ : argument1 : ALL)
--- before: /tmp/test2 (content)
+++ after: /tmp/test2 (content)
@@ -1,6 +1,7 @@
+ : argument1.1 : ALL
+ : argument2192 : ALL
+ : argument1 : ALL
++ : argument2 : ALL
+ : @example : ALL
+ : @test2 : ALL
+ : @example1 : ALL
changed: [localhost] => (item=+ : argument2 : ALL)
--- before: /tmp/test2 (content)
+++ after: /tmp/test2 (content)
@@ -5,4 +5,5 @@
+ : @example : ALL
+ : @test2 : ALL
+ : @example1 : ALL
++ : @test1 : ALL
+ : <rubish>
changed: [localhost] => (item=+ : @test1 : ALL)
ok: [localhost] => (item=+ : @test2 : ALL)
--- before: /tmp/test2 (content)
+++ after: /tmp/test2 (content)
@@ -6,4 +6,5 @@
+ : @test2 : ALL
+ : @example1 : ALL
+ : @test1 : ALL
++ : @test3 : ALL
+ : <rubish>
changed: [localhost] => (item=+ : @test3 : ALL)

最新更新