【发布时间】:2020-03-19 14:59:18
【问题描述】:
我们向使用 Ansible 的用户推出了一些文件,但现在我们想用新文件替换这些文件,但前提是这些文件中没有特定信息。
文件:
测试用户文件.txt
DATA=1000
OTHERDATA=SOMESTUFF
test2 用户文件.txt
DATA=1000000
所以我想保留test的file.txt,并替换test2的file.txt
这是我的 replace.yml 文件
- hosts: localhost
tasks:
- name: Find which users file.txt needs to be replaced
shell: grep -Fx "DATA=1000" '/home/{{ item.name }}/file.txt'
with_items:
- name: test
- name: test2
register: find_output
ignore_errors: True
- debug:
var: find_output
- name: Replace file.txt for only users that need it
copy:
src: 'file_orig.txt'
dest: '/home/{{ item.name }}/file.txt'
owner: '{{ item.name }}'
mode: 0644
with_items: "{{ find_output.results }}"
when:
- find_output.rc == 1
- debug:
var: find_output
第一部分找到文件,但第二部分出错
fatal: [localhost]: FAILED! => {
"msg": "The conditional check 'find_output.rc == 1' failed. The error was: error while
evaluating conditional (find_output.rc == 1): 'list object' has no attribute 'rc'\n\nThe error appears
to be in '/home/user/replace.yml': line 43, column 5, but may\nbe elsewhere in the file depending on the
exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: test\n ^ here\n"
}
这是调试输出
ok: [localhost] => {
"find_output": {
"changed": true,
"failed": true,
"msg": "All items completed",
"results": [
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "grep -Fx \"DATA=1000\" '/home/test/file.txt'",
"delta": "0:00:00.001330",
"end": "2019-11-24 00:19:27.046976",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "grep -Fx \"DATA=1000\" '/home/test/file.txt'",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"item": {
"name": "test"
},
"rc": 0,
"start": "2019-11-24 00:19:27.045646",
"stderr": "",
"stderr_lines": [],
"stdout": "DATA=1000",
"stdout_lines": [
"DATA=1000"
]
},
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "grep -Fx \"DATA=1000\" '/home/test2/file.txt'",
"delta": "0:00:00.001332",
"end": "2019-11-24 00:19:27.118452",
"failed": true,
"invocation": {
"module_args": {
"_raw_params": "grep -Fx \"DATA=1000\" '/home/test2/file.txt'",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"item": {
"name": "test2"
},
"msg": "non-zero return code",
"rc": 1,
"start": "2019-11-24 00:19:27.117120",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
]
}
}
【问题讨论】:
标签: linux automation scripting ansible