Ansible从文件中获取一个字符串,并在该输出中注册一个变量
我的要求如下:Ansible从文件中获取一个字符串,并在该输出中注册一个变量,ansible,Ansible,我的要求如下: <Resource auth="Container" logAbandoned="true" name="jdbc/InfraForms" type="javax.sql.DataSource" url="jdbc:jtds:sqlserver://<MY_SERVER_IP>:143;lastUpdateCount=true;useLOBs=false" username="XXXXX" password=
<Resource auth="Container" logAbandoned="true" name="jdbc/InfraForms"
type="javax.sql.DataSource" url="jdbc:jtds:sqlserver://<MY_SERVER_IP>:143;lastUpdateCount=true;useLOBs=false"
username="XXXXX" password="qqqTyIDd7Rd4JahLDjHJH6LvQ=="
testOnBorrow="true" validationQuery="SELECT 1" closeMethod="close"/>
<Manager pathname=""/>
</Context>
上面是一个文件contact.xml中的内容,我需要在其中获取密码值qqqtyid7rd4jahldjh6lvq==
我如何通过一项负责任的任务来实现这一点?您可以使用
有一个“检索和显示”的示例
有一个python模块需要与pip一起安装,但应该做到这一点
或:
当您想要获得值时,只需调用{{output.stdout}如果您想尝试使用包含的模块,您可以使用slurp模块和set_fact模块,使用jinja2,您可以使用类似以下的正则表达式提取密码:
- name: Slurp file
slurp:
src: /your/file
register: passwordfile
- name: Set Password
set_fact:
your_password: "{{ passwordfile['content'] | b64decode | regex_findall('\bpassword\b\=\"(.+)\"') }}"
谢谢@Darney,这个模块真的很有用,但是它抛出了下面的错误。错误:加载YAML脚本时出现语法错误,例如.yml注意:错误可能实际出现在以下位置之前:第20行,第94列设置事实:您的密码:{{{passwordfile['content']}b64decode{124; regex\u findall('\bpassword\b\=\'(.+)\'}我删除了“\”before=然后它不会评估regexp并打印消息,因为regexp被赋予了您的密码:“{{passwordfile['content']\b64decode{124; regex\u findall('\bpassword\b=\”(.+)\“)}”我想,regex_findall和regex_搜索函数是在2.1.0中添加的:#14696但我使用的是ansible 1.5Ahh,我没有意识到@DevarajuThis返回一个数组,所以有必要获取第一个也是唯一一个项。有关详细信息,请参见中的我的评论。但ansible1.5是我当前的版本shell:cat your_file.xml | grep-e username-e password | awk-F''''{print$4}register:output,如果您不想获得值,只需调用{{output.stdout}感谢ady8531;我想使用ansible模块,但没有任何模块能够满足需要;我只使用shell命令
- name: Slurp file
slurp:
src: /your/file
register: passwordfile
- name: Set Password
set_fact:
your_password: "{{ passwordfile['content'] | b64decode | regex_findall('\bpassword\b\=\"(.+)\"') }}"