将两个文件与Ansible进行比较(Compare two files with Ansible)
我正在努力找出如何比较两个文件。 尝试了几种方法,包括错误的方法:
失败! => {“msg”:“在配置的模块路径中找不到模块diff,此外,缺少核心模块,如果这是结帐,请运行'git pull --rebase'来解决此问题。
这是比较两个文件并确保内容相同或有更好方法的最佳实践吗?
提前致谢。
我的剧本:
- name: Find out if cluster management protocol is in use ios_command: commands: - show running-config | include ^line vty|transport input register: showcmpstatus - local_action: copy content="{{ showcmpstatus.stdout_lines[0] }}" dest=/poc/files/{{ inventory_hostname }}.result - local_action: diff /poc/files/{{ inventory_hostname }}.result /poc/files/transport.results failed_when: "diff.rc > 1" register: diff - name: debug output debug: msg="{{ diff.stdout }}"
I am struggling to find out how to compare two files. Tried several methods including this one which errors out with:
FAILED! => {"msg": "The module diff was not found in configured module paths. Additionally, core modules are missing. If this is a checkout, run 'git pull --rebase' to correct this problem."}
Is this the best practice to compare two files and ensure the contents are the same or is there a better way?
Thanks in advance.
My playbook:
- name: Find out if cluster management protocol is in use ios_command: commands: - show running-config | include ^line vty|transport input register: showcmpstatus - local_action: copy content="{{ showcmpstatus.stdout_lines[0] }}" dest=/poc/files/{{ inventory_hostname }}.result - local_action: diff /poc/files/{{ inventory_hostname }}.result /poc/files/transport.results failed_when: "diff.rc > 1" register: diff - name: debug output debug: msg="{{ diff.stdout }}"
原文:https://stackoverflow.com/questions/50609345
最满意答案
您正在接收没有任何换行符的JSON值; 你必须自己手动插入换行符:
response = requests.get(...) json_str = response.content json_str = json_str.replace('},', '},\n') with open('fb_remodel.txt', 'w') as json_file: json.file.write(json_str)
我在代码中用'json'替换了'html',并稍微改进了文件处理(在上面的代码中,
with
语句会自动关闭文件对象)。该代码使用字符串替换来替换所有
},
字符串},\n
,有效地添加原始响应没有的额外换行符。You are receiving a JSON value without any newlines; you'll have to manually insert newlines yourself:
response = requests.get(...) json_str = response.content json_str = json_str.replace('},', '},\n') with open('fb_remodel.txt', 'w') as json_file: json.file.write(json_str)
I replaced 'html' with 'json' in your code and improved the file handling a little (in the above code the
with
statement auto-closes the file object for you).The code uses string replacement to replace all
},
strings with},\n
, effectively adding in extra newlines the original response did not have.
相关问答
更多-
awk 'BEGIN{FS=" "}{print $3}' filename FS选择字段分隔符,在你的情况是空间,然后你选择字段“3” 您可以将输出保存到一个数组中 array=$(awk 'BEGIN{FS=" "}{print $3}' filename) 然后遍历它。 使用-v选项添加一个变量,在awk中声明它: awk -v var=example 'BEGIN{FS=" "}{print $3 " " var}' filename 这会给你 另一种方式是 var='example' aw ...
-
:%s/$/\*/g 应该工作 所以应该:%s/$/*/g因为MrWiggles正确指出。 :%s/$/\*/g should work. So should :%s/$/*/g as MrWiggles points out correctly.
-
如何在Ruby的每一行的末尾添加字符串“\ n”?(How to add string “\n” literally at the end of each line in Ruby?)[2023-08-21]
String#gsub / String#gsub! 加上一个非常简单的正则表达式可以用来实现: str = "line1 line2 line3" str.gsub!(/$/, ' \n') puts str 输出: line1 \n line2 \n line3 \n String#gsub/String#gsub! plus a very simple regular expression can be used to achieve tha ... -
如何在跳过最后N个字符后在每行末尾添加文本(How to add text at the end of each line after skipping Last N characters)[2022-07-10]
像这样的东西: sed -r“s /([0-9] + {1}:[0-9] + {1} [AZ] {2}。* $)/ 2016年10月1日\ 1 / g”test.txt> new 。文本 text.txt是您的日志文件,“2016年10月1日”是您的文本,\ 1是从组匹配中插入的时间。 Something like this: sed -r "s/([0-9]+{1}:[0-9]+{1}[A-Z]{2}.*$)/01 Oct 2016 \1/g" test.txt > new.txt Where text. ... -
读到'\ n'(行尾)(Reading until '\n' (End of Line))[2021-09-29]
>>丢弃空格,因此Word不会包含换行符。 如果要读取直到行尾,则应使用getline函数而不是>> 。 >> discards white space, so Word won't ever contain a line break. If you want to read until the end of the line, you should use the getline function rather than >>. -
您正在接收没有任何换行符的JSON值; 你必须自己手动插入换行符: response = requests.get(...) json_str = response.content json_str = json_str.replace('},', '},\n') with open('fb_remodel.txt', 'w') as json_file: json.file.write(json_str) 我在代码中用'json'替换了'html',并稍微改进了文件处理(在上面的代码中, wit ...
-
将\ n添加到每行的末尾(Adding \n to the end of each line)[2022-04-28]
您可以在输入文本'.'替换句点的每个出现位置'.' 使用句号后跟换行符'.\n' : NSString *str = @"Hello Stackoverflow. I am having a good day."; str = [str stringByReplacingOccurrencesOfString:@"." withString:@".\n"]; 这是一个非常简单的示例,它假设您的所有行都以句点结束,并且在一行文本中没 ... -
您可以在每行末尾扩展线控制器绘制系列标签: var chartData = { datasets: [{ label: 'serie1', backgroundColor: 'rgba(255, 255, 255, 0.0)', borderColor: 'rgba(0, 119, 290, 0.6)', data: [{x: 1, y: 10}, {x: 2, y: 12}, {x: 3, y: 18}, {x: 4, y: 5}, {x: 5, y: 25} ...
-
换行不是'\ n'(Newline is not '\n')[2023-03-31]
听起来你的新行是\r\n (Windows风格的)。 一种选择是首先使用此命令删除它们: tr -s '\r\n' ' ' < file -s开关表示输入中存在的每个字符序列仅由单个空格替换。 感谢格伦杰克曼指出这一点。 略微猜测你的意图,你可能想要使用这样的东西,用逗号替换所有空格,包括换行符: tr -s '[:space:]' ',' < file 然后,如果需要,您可以将此管道传输给sed以删除尾随逗号。 Sounds like your newlines are \r\n (Windows-s ... -
在每行末尾添加逗号(Add a comma at the end of every line)[2022-08-19]
使用以下方法在每行末尾添加逗号: sed 's/$/,/' file > out.txt S /正则表达式/更换/标志 Use the following approach to add comma at the end of each line: sed 's/$/,/' file > out.txt s/regexp/replacement/flags