Ansible:如何在文件中提取字典键值
好吧,我以为会是wasy,但我最终还是抓伤了头 我有一个字典格式的Ansible:如何在文件中提取字典键值,ansible,Ansible,好吧,我以为会是wasy,但我最终还是抓伤了头 我有一个字典格式的tracker.txt文件: /oracle/db/19.0.0: 回降: 30621255: 29213893: 29867728: 29802382: 28318139: 适用于: 28318139: 29213893: 28788272: 31431771: 32044280: 我试图使文件内容最终看起来像这样: /oracle/db/19.0.0: 回降: 30621255:成功 29213893:成
tracker.txt
文件:
/oracle/db/19.0.0:
回降:
30621255:
29213893:
29867728:
29802382:
28318139:
适用于:
28318139:
29213893:
28788272:
31431771:
32044280:
我试图使文件内容最终看起来像这样:
/oracle/db/19.0.0:
回降:
30621255:成功
29213893:成功
29867728:成功
29802382:成功
28318139:成功
适用于:
28318139:
29213893:
28788272:
31431771:
32044280:
这是我的uf.yml
剧本:
---
-主机:本地主机
收集事实:不
变量:
oracle\u home:/oracle/db/19.0.0
rblist:[30621255、29213893、29867728、29802382、28318139]
任务:
-名称:更新文件
包括:upd.yml
有以下项目:
-“{{rblist}默认值([])}”
回路控制:
循环变量:plist\u项
时间:plist_item |默认值([])
这是upd.yml
包含的任务文件
---
-调试:msg={plist_item}
-名称:更新状态
local_action replace:path=tracker.txt regexp=“{plist_item}}:”replace=“{{plist_item}}:Success”在='rollback'之后,在='apply'之前
但是当我运行这个时,我会出错
[oracle@anstrlsrv lib]$ ansible-playbook uf.yml
PLAY [localhost] ************************************************************************************************************************************************************************************************************************
TASK [update file] **********************************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"reason": "We were unable to read either as JSON nor YAML, these are the errors we got from each:\nJSON: No JSON object could be decoded\n\nSyntax Error while loading YAML.\n mapping values are not allowed in this context\n\nThe error appears to be in '/stage/ap/ansible/lib/upd.yml': line 4, column 94, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n- name: update the status\n local_action replace: path=tracker.txt regexp=\"{{ plist_item }}:\" replace=\"{{ plist_item }}: Success\" after='rollback' before='apply'\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \"{{ foo }}\"\n"}
fatal: [localhost]: FAILED! => {"reason": "We were unable to read either as JSON nor YAML, these are the errors we got from each:\nJSON: No JSON object could be decoded\n\nSyntax Error while loading YAML.\n mapping values are not allowed in this context\n\nThe error appears to be in '/stage/ap/ansible/lib/upd.yml': line 4, column 94, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n- name: update the status\n local_action replace: path=tracker.txt regexp=\"{{ plist_item }}:\" replace=\"{{ plist_item }}: Success\" after='rollback' before='apply'\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \"{{ foo }}\"\n"}
fatal: [localhost]: FAILED! => {"reason": "We were unable to read either as JSON nor YAML, these are the errors we got from each:\nJSON: No JSON object could be decoded\n\nSyntax Error while loading YAML.\n mapping values are not allowed in this context\n\nThe error appears to be in '/stage/ap/ansible/lib/upd.yml': line 4, column 94, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n- name: update the status\n local_action replace: path=tracker.txt regexp=\"{{ plist_item }}:\" replace=\"{{ plist_item }}: Success\" after='rollback' before='apply'\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \"{{ foo }}\"\n"}
fatal: [localhost]: FAILED! => {"reason": "We were unable to read either as JSON nor YAML, these are the errors we got from each:\nJSON: No JSON object could be decoded\n\nSyntax Error while loading YAML.\n mapping values are not allowed in this context\n\nThe error appears to be in '/stage/ap/ansible/lib/upd.yml': line 4, column 94, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n- name: update the status\n local_action replace: path=tracker.txt regexp=\"{{ plist_item }}:\" replace=\"{{ plist_item }}: Success\" after='rollback' before='apply'\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \"{{ foo }}\"\n"}
fatal: [localhost]: FAILED! => {"reason": "We were unable to read either as JSON nor YAML, these are the errors we got from each:\nJSON: No JSON object could be decoded\n\nSyntax Error while loading YAML.\n mapping values are not allowed in this context\n\nThe error appears to be in '/stage/ap/ansible/lib/upd.yml': line 4, column 94, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n- name: update the status\n local_action replace: path=tracker.txt regexp=\"{{ plist_item }}:\" replace=\"{{ plist_item }}: Success\" after='rollback' before='apply'\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \"{{ foo }}\"\n"}
PLAY RECAP ******************************************************************************************************************************************************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=5 skipped=0 rescued=0 ignored=0
我不知道我会错在哪里。我认为的另一个选择是这里记录的东西,但由于我还没有完全理解它,所以还没有大胆尝试:
感谢您的帮助。YAML解析器有问题。您可以通过将该行置于引号中来避免这种情况:
-名称:更新状态
local_action:“replace path=tracker.txt regexp=”{{plist_item}}:“replace=”{{{plist_item}}}:Success''after='rollback'after='apply'”
但为了便于阅读,我建议您将其重写为块:
-名称:更新状态
将_委托给:127.0.0.1#这将替代本地_操作
替换:
路径:tracker.txt
regexp:{{plist_item}}:
替换:“{plist_item}}:成功”
之后:回滚
之前:申请
请注意,此代码不适用于之前
和之后
。我能让这两个都能用,但不知何故,当这两个都存在时,Ansible没有做出任何改变,就好像没有对手一样。你可以从这里开始。下面的剧本
-hosts:localhost
收集事实:错误
变量:
oracle\u home:/oracle/db/19.0.0
rblist:[30621255、29213893]
任务:
-包括以下变量:
文件:tracker.txt
姓名:tracker
-副本:
dest:tracker.txt
内容:|
{
“{oracle_home}}}”
{{tracker[oracle_home]| combine({'rollback':rollback}}}
}
变量:
成功:{{dict(rblist | product(['success']))}
回滚:{{tracker[oracle_home]['rollback']| combine(rsucces)}”
-主机:本地主机
收集事实:错误
任务:
-包括以下变量:
文件:tracker.txt
姓名:tracker
-调试:
跟踪器
给予
跟踪器:
/oracle/db/19.0.0:
适用于:
“28318139”:null
“28788272”:null
“29213893”:null
“31431771”:null
“32044280”:空
回降:
“28318139”:null
‘29213893’:成功
“29802382”:null
“29867728”:null
“30621255”:成功
该文件存储在JSON中
shell>cat tracker.txt
{/oracle/db/19.0.0:{“回滚”:{“30621255”:“成功”,“29213893”:“成功”,“29867728”:null,“29802382”:null,“28318139”:null,“30621255”:“成功”,“29213893”:“成功”},“应用”:{“28318139”:null,“28788272”:null,“31431771”:null,“32044280”:null}}
问:“保留track.txt文件……因为它的可读性更好。” A:有可能。关于如何使用,有两个选项。创建完整的字典跟踪器并使用过滤器
-复制:
dest:tracker.txt
内容:|
{{tracker}to_nice_yaml}
下一个选项是格式化内容并将其发送到_nice_yaml。这两个选项都会使代码更复杂,更容易出错。我会的。有一个问题。比如说
shell>cat tracker.txt | jq。
{
“/oracle/db/19.0.0”:{
“回滚”:{
“30621255”:“成功”,
“29213893”:“成功”,
“29867728”:空,
“29802382”:空,
“28318139”:空
},
“适用”:{
“28318139”:空,
“29213893”:空,
“28788272”:空,
“31431771”:空,
“32044280”:空
}
}
}
“理解代码”
-包括变量:
文件:tracker.txt
姓名:tracker
-调试:
跟踪器
给予
跟踪器:
/oracle/db/19.0.0:
适用于:
28318139:null
28788272:null
29213893:null
31431771:null
32044280:空
回降:
28318139:null
29213893:null
29802382:null
29867728:空
30621255:空
-调试:
var:rsuccess
变量:
成功:{{dict(rblist | product(['success']))}
给予
r成功:
29213893:成功
30621255:成功
-调试:
msg:“{{rollback}}”
变量:
成功:{{dict(rblist | product(['success']))}
回滚:{{tracker[oracle_home]['rollback']| combine(rsucces)}”
给予
回滚:
28318139:null
29213893:成功
29802382:null
29867728:空
30621255:成功