Ansible 在 playbook 中使用大变量选择在粘贴原始数据时失败,或者使用其他较小的变量将 运行
Ansible Using large variable in playbook opts to fail while pasting the raw data in, or using other smaller variables will run
我想使用 ansibles uri 模块将 ios_facts 推送到 gitlab。
- name: get ios facts
ios_facts:
gather_subset: all
register: ios_facts
- name: commit to gitlab
delegate_to: localhost
uri:
url: http://gitlab/api/v4/projects/2/repository/commits
method: POST
body_format: json
status_code: 201
headers:
PRIVATE-TOKEN: "xxxxxxxxxxxxxx"
Content-Type: "application/json"
body: |
{
"branch": "master",
"commit_message": "{{ ansible_net_hostname }} update",
"actions": [
{
"action": "update",
"file_path": "conf/{{ ansible_net_hostname }}",
"content": "{{ ansible_net_config }}"
}
]
}
如果我使用 ansible_net_config 以外的任何其他变量,或者如果我 粘贴 [=25] 的原始内容,那么 Playbook 工作正常=] 而不是使用 jinja2 参考。 ansible_net_config 是一个大字符串,使用 \n 作为换行符,包含一些特殊字符。我想问题的发生是因为剧本解析时我没有得到有效的json。
然后我收到 HTTP 错误 400:错误请求
是否有我可以应用的过滤器或我可能遗漏的任何其他东西?
我设法解决了这个问题:
当变量包含“\n”时,API 调用失败。我可以通过用转义的 "\\n":
替换 "\n" 来让它工作
...
"content": {{ ansible_net_config | replace('\n','\n') }}
...
我想使用 ansibles uri 模块将 ios_facts 推送到 gitlab。
- name: get ios facts
ios_facts:
gather_subset: all
register: ios_facts
- name: commit to gitlab
delegate_to: localhost
uri:
url: http://gitlab/api/v4/projects/2/repository/commits
method: POST
body_format: json
status_code: 201
headers:
PRIVATE-TOKEN: "xxxxxxxxxxxxxx"
Content-Type: "application/json"
body: |
{
"branch": "master",
"commit_message": "{{ ansible_net_hostname }} update",
"actions": [
{
"action": "update",
"file_path": "conf/{{ ansible_net_hostname }}",
"content": "{{ ansible_net_config }}"
}
]
}
如果我使用 ansible_net_config 以外的任何其他变量,或者如果我 粘贴 [=25] 的原始内容,那么 Playbook 工作正常=] 而不是使用 jinja2 参考。 ansible_net_config 是一个大字符串,使用 \n 作为换行符,包含一些特殊字符。我想问题的发生是因为剧本解析时我没有得到有效的json。
然后我收到 HTTP 错误 400:错误请求
是否有我可以应用的过滤器或我可能遗漏的任何其他东西?
我设法解决了这个问题: 当变量包含“\n”时,API 调用失败。我可以通过用转义的 "\\n":
替换 "\n" 来让它工作...
"content": {{ ansible_net_config | replace('\n','\n') }}
...