Ansible,将可变内容保存到新文件
我试图创建一个Ansible任务,将变量的内容保存到一个新文件中 使用Ansible 2.5.13和Python 2.7.5 我已经尝试将变量的内容复制到应该创建文件的目标路径Ansible,将可变内容保存到新文件,ansible,Ansible,我试图创建一个Ansible任务,将变量的内容保存到一个新文件中 使用Ansible 2.5.13和Python 2.7.5 我已经尝试将变量的内容复制到应该创建文件的目标路径 - name: Save alert rule example file to Prometheus copy: content: "{{ alert_rule_config }}" dest: /opt/compose/prom/alert_rule.yml 还尝试在复制变量内容之
- name: Save alert rule example file to Prometheus
copy:
content: "{{ alert_rule_config }}"
dest: /opt/compose/prom/alert_rule.yml
还尝试在复制变量内容之前创建文件
- name: Create alert rule file
file:
path: /opt/compose/prom/alert_rule.yml
state: touch
- name: Save alert rule example file to Prometheus
copy:
content: "{{ alert_rule_config }}"
dest: /opt/compose/prom/alert_rule.yml
试图用引号括起目标路径。。。
但是不管创建了什么目录/opt/compose/prom/alert\u rule.yml/
变量的内容类似于
alert_rule_config:
groups:
- name: debug-metrics
rules:
- alert: DebugAlert
expr: test_expression
我希望创建该文件(因为它不存在),并将变量的内容保存到新创建的文件中,但任务失败
失败!=>{“changed”:false,“msg”:“不能使用目录为dest的内容”}
我希望避免发出命令,并希望使用Ansible模块。如果在运行任务时目录
/opt/compose/prom/alert\u rule.yml
已经存在,则会发生这种情况
要将其作为任务的一部分删除,请添加
- file:
path: /opt/compose/prom/alert_rule.yml
state: absent
在执行其他任务之前。您需要创建目标目录而不是目标文件,否则您将获得第一个选项中的目标目录/opt/compose/prom不存在或
错误,无法触摸目标:[Errno 2]没有这样的文件或目录:'/opt/compose/prom/alert_rule.yml'
,在第二个选项中
- name: Create alert rule containing directory
file:
path: /opt/compose/prom/
state: directory
- name: Save alert rule example file to Prometheus
copy:
content: "{{ alert_rule_config }}"
dest: /opt/compose/prom/alert_rule.yml
但正如@Calum Halpin所说,如果在测试过程中出现错误并创建了一个目录
/opt/compose/prom/alert\u rule.yml/
,则需要在之前将其删除。alert\u rule.yml正在创建中(作为剧本中的一个post\u任务),docker compose中的一个角色中有一个卷映射了该文件名。问题似乎是docker容器是在复制文件之前启动的,因此,我相信目录是由docker compose创建的,因为卷。。。我最终创建了您建议的pre_任务,因此,当docker容器启动时,文件已经存在,并且卷映射没有问题。谢谢,我相信你的解决方案也能奏效。我怀疑该目录是由docker compose在一个角色中创建的。我最终将添加文件的任务作为pre_任务移动