从远程服务器复制文件并使用 ansible 替换字符串

Dan*_*515 6 ansible

我知道您可以使用带有 fetch 模块的ansible 将文件从远程服务器复制到本地。。您甚至可以使用与您从中获取的节点相关的一些变量来命名该文件。我想要做的是在复制该文件后替换该文件上的某些字符串,最好使用库存变量。这可能吗?有没有不涉及后处理 bash 脚本的解决方法?

β.ε*_*.βε 3

获取文件后,只需通过任务使用对本地主机的delegate_to委托replace

给定/tmp/somefile

foo bar baz
This is an example string
Lorem Ipsum
The quick brown fox jumps over the lazy dog
Run Code Online (Sandbox Code Playgroud)

和剧本:

foo bar baz
This is an example string
Lorem Ipsum
The quick brown fox jumps over the lazy dog
Run Code Online (Sandbox Code Playgroud)

它产生了回顾:

PLAY [all] *********************************************************************************************************

TASK [fetch] *******************************************************************************************************
changed: [host1]

TASK [replace] *****************************************************************************************************
changed: [host1 -> localhost]

PLAY RECAP *********************************************************************************************************
host1                      : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
Run Code Online (Sandbox Code Playgroud)

并给出文件/tmp/fetched/host1/tmp/somefile包含:

foo bar baz
host1
Lorem Ipsum
The quick brown fox jumps over the lazy dog
Run Code Online (Sandbox Code Playgroud)