我正在使用ansible来读取安装在我的redhat主机上的RPM的列表,并且还想指定一种格式。如何在命令字符串中转义单个大括号?
- hosts: localhost
gather_facts: no
tasks:
- name: rpm -qa
command: rpm -qa --qf "{NAME}: %{VERSION}\n" | sort
register: rpmout
- debug:
msg: Your rpms are {{ rpmout.stdout }}
运行上述剧本的输出:
$ ansible-playbook -vvv ./a.yml
ansible-playbook 2.7.10
config file = None
configured module search path = [u'/home/ansible/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 2.7.5 (default, Apr 9 2019, 14:30:50) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
No config file found; using defaults
/etc/ansible/hosts did not meet host_list requirements, check plugin documentation if this is unexpected
/etc/ansible/hosts did not meet script requirements, check plugin documentation if this is unexpected
Parsed /etc/ansible/hosts inventory source with ini plugin
ERROR! Syntax Error while loading YAML.
mapping values are not allowed in this context
The error appears to have been in '/home/ansible/a.yml': line 5, column 34, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: rpm -qa
command: rpm -qa --qf "{NAME}: %{VERSION}\n" | sort
^ here
答案 0 :(得分:4)
要“混淆” YAML解析器,您需要将整个字符串放在单引号中:
command: 'rpm -qa --qf "{NAME}: %{VERSION}\n" | sort'
或者如果您需要换行符:
command: |
rpm -qa --qf "{NAME}: %{VERSION}
" | sort