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

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

4

1 回答 1

1

Q:“文件中已有的行被删除了,只剩下第二个任务的行了。我应该怎么做才能增加新的行而不删除已有的行?”

- 答:设置独特的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
于 2019-12-10T08:57:12.443 回答