通过Ansible在GitLab中创建新文件



我的ansible作业的脚本位于gitlab存储库中
例如:/Ansible/job.yaml

我想从我的Ansible作业创建一个新文件,该文件包含我在与作业脚本相同的位置运行的另一个Ansible任务的响应
例如:/Ansible/ouput.txt

有可能吗?通常我会把文件放在服务器主机上,但这次我需要它在GitLab中。

考虑到GitLab确实允许您在该文件夹中进行编写,您可以:

  • 将要写入的任务委派给localhost
  • 使用特殊变量playbook_dir将其写入与剧本相同的文件夹中

所以,类似于:

- copy:
content: "{{ some_other_registered_task_output }}"
dest: "{{ playbook_dir }}/output.txt"
delegate_to: localhost

请注意,如果您在该剧本中针对多个主机,则必须聚合所有已注册输出的节点,否则最终只能获得一个节点的输出。

你可以通过以下方式实现:

- copy:
content: "{{
hostvars
| dict2items
| selectattr('key', 'in', ansible_play_hosts)
| map(attribute='value.some_other_registered_task_output')
| join('nn')
}}"
dest: "{{ playbook_dir }}/output.txt"
delegate_to: localhost

例如,两个任务:

- command: echo '{{ inventory_hostname }}'
register: some_other_registered_task_output
- copy:
content: "{{
hostvars
| dict2items
| selectattr('key', 'in', ansible_play_hosts)
| map(attribute='value.some_other_registered_task_output.stdout')
| join('nn')
}}"
dest: "{{ playbook_dir }}/output.txt"
delegate_to: localhost

在名为node1node2node3的节点上运行,将在控制器上与剧本相同的文件夹中创建一个文件output.txt,其中包含:

node3
node1
node2

最新更新