ANSIBLE регулярное выражение заменить все соответствующие строки1, исключить строки2 - PullRequest
1 голос
/ 19 мая 2019

Я хотел бы заменить все экземпляры dogs на cats, кроме тех, в которых строка имеет does not play with mice.

Содержимое моего файла выглядит так:

dogs:plays with mice
dogs:plays with mice
dogs:does not play with mice
dogs:plays with mice
dogs:plays with mice
dogs:does not play with mice
dogs:plays with mice

Ниже приведены важные биты моей книги-игры, чтобы попытаться достичь желаемого результата:

  vars:
    file_to_update: /tmp/cats_dogs_file.txt
    exclude: "does not play with mice"
    from_name: "dogs"
    to_name: "cats"

  tasks:    

  - name: updates dogs to cats - excluding the necessary
    replace:
      path: "{{ file_to_update }}"
      regexp: "^{{ from_name }}(?! {{ exclude }})"
      replace: "{{ to_name }}"
    register: file_update_status

Я ожидаю, что содержимое файла будет изменено, как указано ниже:

cats:plays with mice
cats:plays with mice
dogs:does not play with mice
cats:plays with mice
cats:plays with mice
dogs:does not play with mice
cats:plays with mice

Вместо этого я в настоящее время получаю все dogs, измененные на cats.

Исключение не соблюдается.

Ответы [ 2 ]

1 голос
/ 19 мая 2019

Простым решением было бы зациклить строки и условно исключить строки, которые не подлежат замене.

Игра ниже

tasks:
  - replace:
      path: "{{ file_to_update }}"
      regexp: "{{ item }}"
      replace: "{{ item|regex_replace(from_name, to_name) }}"
    with_lines: "cat {{ file_to_update }}"
    when: exclude not in item

дает (сокращенно):

TASK [replace] 
******************************************************************************
changed: [localhost] => (item=dogs:plays with mice)
ok: [localhost] => (item=dogs:plays with mice)
skipping: [localhost] => (item=dogs:does not play with mice) 
ok: [localhost] => (item=dogs:plays with mice)
ok: [localhost] => (item=dogs:plays with mice)
skipping: [localhost] => (item=dogs:does not play with mice) 
ok: [localhost] => (item=dogs:plays with mice)

> cat cats_dogs_file.txt
cats:plays with mice
cats:plays with mice
dogs:does not play with mice
cats:plays with mice
cats:plays with mice
dogs:does not play with mice
cats:plays with mice
1 голос
/ 19 мая 2019

Может быть, здесь мы могли бы написать выражение и найти части желаемого результата и заменить их.Может быть, что-то похожее на:

(.+):(plays? with)\s(.+)

enter image description here

JavaScript Demo

const regex = /(.+):(plays? with)\s(.+)/gm;
const str = `dogs:plays with mice
dogs:plays with mice
dogs:does not play with mice
dogs:plays with mice
dogs:plays with mice
dogs:does not play with mice
dogs:plays with mice

cats:plays with mice
cats:plays with mice
dogs:does not play with mice
cats:plays with mice
cats:plays with mice
dogs:does not play with mice
cats:plays with mice`;
const subst = `cats:$2 $3`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);

Python Test

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"(.+):(plays? with)\s(.+)"

test_str = ("dogs:plays with mice\n"
    "dogs:plays with mice\n"
    "dogs:does not play with mice\n"
    "dogs:plays with mice\n"
    "dogs:plays with mice\n"
    "dogs:does not play with mice\n"
    "dogs:plays with mice\n\n"
    "cats:plays with mice\n"
    "cats:plays with mice\n"
    "dogs:does not play with mice\n"
    "cats:plays with mice\n"
    "cats:plays with mice\n"
    "dogs:does not play with mice\n"
    "cats:plays with mice")

subst = "cats:\\2 \\3"

# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)

if result:
    print (result)

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

RegEx

Если это не было вашим желаемым выражением, вы можете изменить / изменить его выражения в regex101.com .

RegEx Circuit

Вы также можете визуализировать свои выражения в jex.im :

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...