Ansible:替换无法找到本地现有的文件

Ansible:替换无法找到本地现有的文件

test.conf我正在尝试使用 Ansible替换配置文件中的单个字符串。

这是我的职责的两项任务:

- name: Check for conf locally
  local_action: stat path="{{ role_path }}/files/test.conf"
  register: stat_config

# Place test_var within the test conf file
- replace:
    dest: "{{ role_path }}/files/test.conf"
    regexp: 'to_be_replaced'
    replace: "{{test_var}}"
  when: stat_config.stat.exists == True

在第一个中,我检查文件是否存在(本地),在第二个中,我尝试to_be_replaced用 var替换"{{test_var}}"。以下是输出:

 TASK [app : Check for conf locally] *********************************************************
task path: /home/ubuntu/src/share/playbooks/roles/app/tasks/main.yml:27
ok: [app001 -> localhost] => {"changed": false, "stat": {"atime":  1537971131.8750823, "attr_flags": "e", "attributes": ["extents"],   "block_size": 4096, "blocks": 40, "charset": "us-ascii", "checksum":  "c33f45d95450d02266981b44370efbe482d2a995", "ctime": 1537954859.6513484, "dev": 64769, "device_type": 0, "executable": false, "exists": true, "gid": 1000, "gr_name": "ubuntu", "inode": 2827755, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mimetype": "text/plain", "mode": "0644", "mtime": 1537953479.766345, "nlink": 1, "path": "/home/ubuntu/src/share/playbooks/roles/app/files/test.conf", "pw_name": "ubuntu", "readable": true, "rgrp": true, "roth": true, "rusr": true, "size": 18231, "uid": 1000, "version": "18446744073111871316", "wgrp": false, "woth": false, "writeable": true, "wusr": true, "xgrp": false, "xoth": false, "xusr": false}}

TASK [app: replace] *******************************************************************************
task path: /home/ubuntu/src/share/playbooks/roles/app/tasks/main.yml:32
fatal: [app001]: FAILED! => {"changed": false, "msg": "Path /home/ubuntu/src/share/playbooks/roles/app/files/test.conf does not exist !", "rc": 257}

正如您所看到的,第一个任务在test.conf这里找到了该文件/home/ubuntu/src/share/playbooks/roles/app/files/test.conf,但是第二个任务在同一路径上找不到它..!

我做错了什么?

答案1

我们无法从输出中确定,但您的第二个任务并非专门在本地主机上。请尝试:

# Place test_var within the test conf file
- replace:
    dest: "{{ role_path }}/files/test.conf"
    regexp: 'to_be_replaced'
    replace: "{{test_var}}"
  when: stat_config.stat.exists == True
  delegate_to: localhost

相关内容