【问题标题】:Error when trying to replace certain files when using a loop in ansible在ansible中使用循环时尝试替换某些文件时出错
【发布时间】: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


    【解决方案1】:

    问:"'list object' 没有属性 'rc'"

        find_output.rc == 1
    

    A:下面的任务应该可以完成这项工作

          - name: Replace file.txt for only users that need it
            copy:
              src: 'file_orig.txt'
              dest: '/home/{{ item.item.name }}/file.txt'
              owner: '{{ item.item.name }}'
              mode: 0644
            loop: "{{ find_output.results }}"
            when: item.rc == 1
    
    • find_output.results 是一个列表
    • 循环将列表中的每一项放入变量item
    • 变量item有属性
        item.item.name
        item.rc
    

    【讨论】:

    • 您好,感谢您的帮助,这解决了我的问题,我现在已经在我的现场测试环境中进行了测试,它按预期工作。
    猜你喜欢
    • 2022-12-22
    • 2020-02-07
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多