问题描述
我遇到了一些带有意外“(”的奇怪问题。
- name: save results to csv
become: yes
shell: |
res=$(istat "{{ var_path }}" | grep Protection | xargs)
echo "\"{{ policyname }}\",\"{{ itemId }}\",\"{{ description }}\",\"$res\"" >> output.csv
此任务适用于大多数输入。但是当描述变量包含这样的内容时:
“等等等等做这个和那个(某事)某事”
"std_err": "/bin/sh[3]: 0403-057 Syntax error at line 3: `(' is not expected."
这仅适用于包含“()”的 {{ description }}
值。当我尝试删除这些字符时,一切正常。
如果重要,目标主机是 aix 7.1
解决方法
不应使用 shell
模块附加到文件,而应使用 lineinfile module:
- name: get what you need
become: yes
shell: 'istat "{{ var_path }}" | grep Protection | xargs'
register: res
- name: insert it at the end of CSV
lineinfile:
line: '"{{ policyname }}","{{ itemId }}","{{ description }}","{{ res.stdout }}"'
path: '/root/output.csv'
become: yes
如果需要,您可以使用 regexp
参数来确保不会将行插入到文件中,如果它已经存在的话。 (检查上面提到的文档)