用远程机器上具有绝对路径的另一个文件的内容替换word



我在远程机器上有一个kickstart文件,我想在没有本地pub文件的情况下向其中添加远程机器用户的ssh密钥。我的计划是在ansible剧本中添加一个shell部分来执行此任务。到目前为止,我的测试有2个文件,并使用sed替换所需的部分。我有以下内容:

文件1:

aaa
bbb
text "REPLACE"
ccc

文件2:

1233456

使用CCD_ 1来替换文本。

然而,当我添加绝对路径sed -i "s@REPLACE@$(cat /home/me/test/file2)@g" /home/me/test/file1时,该命令失败。

什么可以解决这个问题?

edit:为了澄清ansible是为各种任务提供远程机器,其中包括PXE引导服务器。在这种情况下,使用ansible替换的解决方案非常好。

给定文件

shell> cat /tmp/file1
aaa
bbb
text "REPLACE"
ccc
shell> cat /tmp/file2
1233456

使用模块replace来更新文件。例如,战术手册

- hosts: localhost
tasks:
- name: Read file2
command: cat /tmp/file2
register: file2
- name: Update file1
replace:
path: /tmp/file1
regexp: '^(.*)REPLACE(.*)$'
replace: 'g<1>{{ file2.stdout }}g<2>'

更新文件

shell> cat /tmp/file1
aaa
bbb
text "1233456"
ccc

使用--diff模式运行剧本会产生

shell> ansible-playbook pb.yml -D
PLAY [localhost] *****************************************************************************
TASK [Read file2] ****************************************************************************
changed: [localhost]
TASK [Update file1] **************************************************************************
--- before: /tmp/file1
+++ after: /tmp/file1
@@ -1,4 +1,4 @@
aaa
bbb
-text "REPLACE"
+text "1233456"
ccc
changed: [localhost]
PLAY RECAP ***********************************************************************************
localhost: ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

最新更新