我被困在这个小东西上了。我必须读取一个文件myfile.txt,其中的内容是关于用户ID和密码信息的。它是一种存储键值对的.properties文件,在Java相关技术中用于存储可配置的应用程序。
我必须以这样的方式拆分它:
我的文件.txt
mysql.user=abc
mysql.user.one=abc
user=abc
它应该在找到user
和=
符号的地方分裂,例如
=
之前包含user
的字符串变为idhead
=
之后的字符串变为idval
我给你一个第一个场景的例子,它应该如何发生:
idhead: mysql.user
idval: abc
如上所述,文件中可能有3 种条目场景。
- name: Fetch the first file
slurp:
src: /home/usr/Desktop/files/myfile.txt
register: file1
- name: extract content for matching pattern user & userid value
set_fact:
idinfo: "{{ file1['content'] | b64decode | regex_findall((user+)(=.*=))}}"
- name: set fact for id head
set_fact:
idhead: "{{item.split('=')[0]}}"
loop: "{{idinfo}}"
- name: set fact for id val
set_fact:
idval: "{{item.split('=')[1]}}"
loop: "{{idinfo}}"
它显示一个正则表达式错误,但是当我签入 https://regex101.com/时,它与表达式匹配。是一些 Ansible 的东西还是我错过了什么?我不明白这个。请向我建议所有三种情况下正则表达式的可能答案。
你真的不必如此复杂,也不必做多个set_fact
。
你可以以一种非常简单和天真的方式来处理它,只是循环你的内容,用一个简单的when
跳过与你要求的行为不匹配的行,将其与with_lines
测试和一个简单的in
结合使用,你的用例最终可能成为一个单一的任务:
##
# Mind that you will be constantly overriding those two facts,
# so maybe you should construct an array with those?
# See the full working playbook below for an example
##
- set_fact:
idhead: "{{ item.split('=')[0] | trim }}"
idval: "{{ item.split('=')[1] | trim }}"
with_lines: cat /home/usr/Desktop/files/myfile.txt
when: "'user' in item and '=' in item"
我的文件.txt
mysql.user=abc
mysql.user.one=abc
user=abc
foo=bar
some.user.foo = bar
工作手册:
- hosts: localhost
gather_facts: no
tasks:
- set_fact:
idhead: "{{ idhead | default([]) + [item.split('=')[0] | trim] }}"
idval: "{{ idval | default([]) + [item.split('=')[1] | trim] }}"
with_lines: cat /home/usr/Desktop/files/myfile.txt
when: item | regex_search('.*user.*=')
- debug:
msg: "{{ idhead }} {{ idval }}"
回顾一下:
PLAY [localhost] *****************************************************************************************************
TASK [set_fact] ******************************************************************************************************
ok: [localhost] => (item=mysql.user=abc)
ok: [localhost] => (item=mysql.user.one=abc)
ok: [localhost] => (item=user=abc)
skipping: [localhost] => (item=foo=bar)
ok: [localhost] => (item=some.user.foo = bar)
TASK [debug] *********************************************************************************************************
ok: [localhost] => {
"msg": "['mysql.user', 'mysql.user.one', 'user', 'some.user.foo'] ['abc', 'abc', 'abc', 'bar']"
}
PLAY RECAP ***********************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
现在对于您的正则表达式,它确实是无效的,因为user
中的u
是无效的令牌。
我想你的意思是寻找.*user.*=.*
:
给定与上述相同的myfile.txt此剧本:
- hosts: localhost
gather_facts: no
tasks:
- slurp:
src: /home/usr/Desktop/files/myfile.txt
register: file1
- set_fact:
idinfo: "{{ file1['content'] | b64decode | regex_findall('.*user.*=.*') }}"
- debug:
msg: "{{ idinfo }}"
给出回顾
PLAY [localhost] *****************************************************************************************************
TASK [slurp] *********************************************************************************************************
ok: [localhost]
TASK [set_fact] ******************************************************************************************************
ok: [localhost]
TASK [debug] *********************************************************************************************************
ok: [localhost] => {
"msg": [
"mysql.user=abc",
"mysql.user.one=abc",
"user=abc",
"some.user.foo = bar"
]
}
PLAY RECAP ***********************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0