基本上,我有一个包含接口信息的文本文件(即接口gigabit0 0/1/2,接口gigabit0 0/2/3)。我需要在每个接口命令之后添加一个额外的块。它必须如下所示:
interface gigabit0/1/2
shutdown
!
interface gigabit0/2/3
shutdown
!我已经使用了一个已经存在的"lineinfile“模块来匹配regex,并在匹配之后添加了一个新行。
---
- hosts: localhost
tasks:
- name: Ansible insert
lineinfile:
path: ./ha.txt
line: |
shutdown
!
insertafter: interface?\s[a-z]\w*/[0-9]/[0-9]
state: present
with_lines: cat ha.txt发布于 2019-04-29 10:50:56
使用替换。下面这出戏
tasks:
- replace:
path: ha.txt
regexp: '(interface?\s[a-z]\w*/[0-9]/[0-9])'
replace: |-
\1
shutdown
!带着文件
> cat ha.txt
interface gigabit0/1/2
interface gigabit0/2/3给予:
> cat ha.txt
interface gigabit0/1/2
shutdown
!
interface gigabit0/2/3
shutdown
!发布于 2020-09-21 22:12:38
这里有点吹毛求疵:在弗拉基米尔的原版和优秀版中,regexp可能会在interface之后失去interface的角色。
regexp: '(interface\s[a-z]\w*/[0-9]/[0-9])'? as-is意味着interface中的e char是可选的,这没有多大意义。
https://stackoverflow.com/questions/55900398
复制相似问题