在 Ansible 问题中创建然后编辑文件 (authorized_keys)
Create and then edit file in Ansible problems (authorized_keys)
我正在使用 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
问题是在执行第二个任务时,文件中现有的行被删除,只剩下第二个任务的行。
我应该怎么做才能添加新行而不是删除现有行?
Q: "Existing lines in the file are deleted and only those of the second task remain. What should I do to add the new lines and not delete the existing ones?"
A:设置唯一的marker_begin
和marker_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
我正在使用 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
问题是在执行第二个任务时,文件中现有的行被删除,只剩下第二个任务的行。 我应该怎么做才能添加新行而不是删除现有行?
Q: "Existing lines in the file are deleted and only those of the second task remain. What should I do to add the new lines and not delete the existing ones?"
A:设置唯一的marker_begin
和marker_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