简体   繁体   English

使用insertafter复制Ansible Lineinfile

[英]Ansible lineinfile duplication using insertafter

I am trying to add an entry into my /etc/hosts file using ansibles lineinfile . 我正在尝试使用ansibles lineinfile将条目添加到我的/etc/hosts文件中。 I want the logic to be if it finds the entry 127.0.0.1 mysite.local then do nothing otherwise insert it after the line 127.0.1.1 我希望逻辑是,如果找到条目127.0.0.1 mysite.local则不执行任何操作,否则将其插入到127.0.1.1127.0.1.1

127.0.0.1   localhost
127.0.1.1   mypc
127.0.0.1      mysite.local

I have the insert after part working but it appears the actual regex search is failing to find the existing entry so I keep getting duplication of the insertion of 127.0.0.1 mysite.local 我可以在部分工作后进行插入,但是似乎实际的正则表达式搜索无法找到现有条目,因此我不断重复插入127.0.0.1 mysite.local

The docs do say; 文档确实会说;

When modifying a line the regexp should typically match both the initial state of the line as well as its state after replacement by line to ensure idempotence. 修改线时,正则表达式通常应匹配线的初始状态以及被线替换后的状态,以确保幂等。

But I'm not sure how that applies to my regex. 但是我不确定这对我的正则表达式如何适用。 Currently my play is; 目前我的戏是;

- name: Add the site to hosts
  lineinfile:
    path: /etc/hosts
    # Escape special chars
    regex: "^{{ domain|regex_escape() }}"
    line: "127.0.0.1      {{ domain }}"
    insertafter: '127\.0\.1\.1'
    firstmatch: yes
  become: yes

where domain is mysite.local . domainmysite.local

I have looked at this answer but I'm pretty sure I cannot use backrefs since the docs state; 我已经看了这个答案,但是我很确定自从文档状态以来,我不能使用backrefs

This flag changes the operation of the module slightly; 这个标志稍微改变了模块的操作。 insertbefore and insertafter will be ignored, and if the regexp doesn't match anywhere in the file, the file will be left unchanged. insertbefore和insertafter将被忽略,并且如果regexp与文件中的任何地方都不匹配,则文件将保持不变。

I have tried; 我试过了;

regex: '127\.0\.0\.1\s+?{{ domain|regex_escape() }}'

With no luck either 也没有运气

According to this link , lineinfile scans the file and applies the regex one line at a time, meaning you cannot use a regex that looks through the whole file. 根据此链接 ,lineinfile扫描文件并一次应用正则表达式一行,这意味着您不能使用浏览整个文件的正则表达式。 I am unfamiliar with the lineinfile tool, but if you can use the "replace" tool used in the link above then you can use the following Python regex to match as you need: 我不熟悉lineinfile工具,但是如果可以使用上面链接中使用的“替换”工具,则可以根据需要使用以下Python正则表达式进行匹配:

\A((?:(?!127\.0\.0\.1\s)[\s\S])*?)(?:\Z|127\.0\.0\.1\s+(?!{{ domain|regex_escape() }})\S+\n|(127\.0\.1\.1\s+\S+(?![\s\S]*\n127\.0\.0\.1\s)\n))

With the substitution: "\\1\\2127.0.0.1 {{ domain }}\\n" 替换为:“ \\ 1 \\ 2127.0.0.1 {{域}} \\ n”

The non-capturing group handles three distinct cases: 非捕获组处理三种不同的情况:

  1. Case 1 : 127.0.1.1 and 127.0.0.1 don't exist so insert at end 情况1 :127.0.1.1和127.0.0.1不存在,因此请在末尾插入
  2. Case 2 : 127.0.0.1 exists with a different host so replace the entry 情况2 :127.0.0.1与其他主机存在,因此替换条目
  3. Case 3 : 127.0.1.1 exists so insert after it 情况3 :存在127.0.1.1,因此请在其后插入

It is the second case that tackles idempotence by avoiding matching an entry for "127.0.0.1" if one already exists. 第二种情况是通过避免匹配“ 127.0.0.1”条目(如果已经存在)来解决幂等性。

It seems that firstmatch: yes was breaking things. 似乎是firstmatch: yes正在打破事情。 It work for me with following task (I replaced space with tab for fancy look but spaces work as well): 它适用于以下任务(我将空格替换为Tab以获得漂亮的外观,但空格也可以正常工作):

  - name: Add the site to hosts
    lineinfile:
      path: /etc/hosts
      # Escape special chars
      regexp: "{{ domain|regex_escape() }}"
      line: "127.0.0.1{{ '\t' }}{{ domain }}"
      insertafter: '127\.0\.1\.1'

The doc says: 医生说:

insertafter: ... If regular expressions are passed to both regexp and insertafter, insertafter is only honored if no match for regexp is found. insertafter:...如果将正则表达式同时传递给regexp和insertafter,则仅在找不到与regexp匹配的情况下才使用insertafter。

The regex in the task expands to 任务中的正则表达式扩展为

regex: ^mysite\.local

This regex is not found because there is no line that begins with "mysite.local". 找不到此正则表达式,因为没有以“ mysite.local”开头的行。 Hence insertafter is honored and "line" is inserted after 127.0.1.1 . 因此,执行insertafter并在127.0.1.1之后插入“行”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM