我想使用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之外的任何其他变量,或者如果我粘贴ansible_net_config的原始内容而不是使用jinja2引用,则Playbook工作正常。 ansible_net_config是一个大字符串,使用\ n作为新行并包含一些特殊字符。我想问题出现是因为我在playbook解析时没有得到有效的json。
然后我得到HTTP错误400:错误请求
我可以申请任何过滤器或我可能错过的任何其他东西吗?
我设法解决了这个问题:当变量包含“\ n”时,API调用失败。我可以通过用“\\ n”替换“\ n”来使其工作:
...
"content": {{ ansible_net_config | replace('\n','\\n') }}
...