[英]Ansible lineinfile Regex
我有這個文字
[john]
age=20
group=wheel
[doe]
age=30
group=wheel
[stack]
age=undefined
group=wheel
color=white
[overflow]
age=undefined
我想用第一個正則表達式捕獲:
age=20
和group=wheel of [stack] 與第二個
您正在嘗試將正則表達式用於定義明確的文本文件格式; ini
查找是您實際所追求的:
# assuming the ini file is /tmp/foo.ini
tasks:
- set_fact:
john_age: '{{ lookup("ini", "age section=john file=/tmp/foo.ini") }}'
stack_group: '{{ lookup("ini", "group section=stack file=/tmp/foo.ini") }}'
然后,如果您的內容實際上不在文件中,則查找似乎僅適用於文件,因此您需要暫時將其寫出到文件中:
tasks:
- copy:
dest: /tmp/foo.ini
content: '{{ the_ini_from_your_question_goes_here }}'
- set_fact:
john_age: '{{ lookup("ini", "age section=john file=/tmp/foo.ini") }}'
stack_group: '{{ lookup("ini", "group section=stack file=/tmp/foo.ini") }}'
- file:
path: /tmp/foo.ini
state: absent
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.