我有一个文件,其中包含以下文本块。我在文本块之前和之后都有文本
other_user:
hash: JKJ;LKJA;LDKJF;LKJA;LKJIUR;JFKLJDQPIRQKJ;LKFJPOQJ
#password is: some_pw0
logstash:
hash: $fj;kdjjfajf;ajKFJ;dfj;dkfja;dfjFJ:LFJj;kj;lfkajs
#password is: some_pw
other_user1:
hash: JJKLJDRKJOPIQMVOIUROIJFAUROJJFIUQWKERJJFKQURJAKDJ
#password is: some_pw1
我正在尝试使用此代码删除对logstash用户的阻止,但它不会删除它。
- name: Delete existing logstash user
blockinfile:
dest: /path_to_file/foo.yml
state: absent
block: |
logstash:
hash: $fj;kdjjfajf;ajKFJ;dfj;dkfja;dfjFJ:LFJj;kj;lfkajs
#password is: some_pw
我期望的结果是:
other_user:
hash: JKJ;LKJA;LDKJF;LKJA;LKJIUR;JFKLJDQPIRQKJ;LKFJPOQJ
#password is: some_pw0
other_user1:
hash: JJKLJDRKJOPIQMVOIUROIJFAUROJJFIUQWKERJJFKQURJAKDJ
#password is: some_pw1
我错过了什么?
有件事关于
blockinfile
。请密切关注描述:
此模块将插入/更新/删除多行文本块周围有可定制的标记线。
标记的默认值:
# {mark} ANSIBLE MANAGED BLOCK
,其中标记为 BEGIN
/END
。这里有一个具体的例子来完成之前答案中所说的内容:
- name: "Example on how to remove a block in a file"
blockinfile:
path: "example/test.sh"
state: absent
marker_begin: 'line to remove 1'
marker_end: 'line to remove 4'
marker: "{mark}"
block: |
line to remove 2
line to remove 3