创建并编辑Ansible问题(授权密钥)中的文件

问题描述 投票:0回答:1

我正在用ansible做两个简单的任务。

首先,我创建一个包含内容的新文件(我将发挥作用):

- name: Add keys to authorized_keys
  blockinfile:
        owner: user
        group: user
        mode: '0600'
        create: yes
        path: /home/user/.ssh/authorized_keys
        block: |
                line if text
                other line
                more lines

第二任务(我将扮演第二角色):

- name: Add more keys to authorized_keys root
  blockinfile:
        path: /home/user/.ssh/test_keys
        block: |
                other and more keys

问题是,在执行第二个任务时,文件中的现有行将被删除,而仅保留第二个任务的行。我该怎么做才能添加新行而不删除现有行?

ansible edit createfile authorized-keys
1个回答
0
投票

Q:“文件中的现有行将被删除,仅保留第二个任务中的那些行。我应该怎么做才能添加新行而不删除现有行?”

A:设置块的唯一marker_beginmarker_end。例如

- name: Add keys to authorized_keys
  blockinfile:
    marker_begin: "BEGIN BLOCK1"
    marker_end: "END BLOCK1"
    owner: user
    group: user
    mode: '0600'
    create: yes
    path: /home/user/.ssh/authorized_keys
    block: |
      line if text
      other line
      more lines

- name: Add more keys to authorized_keys root
  blockinfile:
    marker_begin: "BEGIN BLOCK2"
    marker_end: "END BLOCK2"
    path: /home/user/.ssh/authorized_keys
    block: |
      other and more keys
© www.soinside.com 2019 - 2024. All rights reserved.