【问题标题】:Create and then edit file in Ansible problems (authorized_keys)在 Ansible 问题 (authorized_keys) 中创建然后编辑文件
【发布时间】:2019-12-10 08:38:44
【问题描述】:

我正在使用 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】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 2013-01-31
    • 1970-01-01
    相关资源
    最近更新 更多