Ansible:如何使用Shell处理引号和空格

问题描述 投票:0回答:1

我正在使用下面的2.8.2版本上的相应剧本创建一个剧本以在“ / home”目录中列出用户:

---
- name: Check users in /home directory
  hosts: REDHAT
  tasks:
  - name: List users
      shell: cat /etc/passwd | grep "/home" |cut -d ":" -f1| tr '\n' ' '
      register: output
      ignore_errors: yes

我遇到的问题是在命令的结尾加上引号和空格:'\n' ' '

我曾尝试使用双引号", {} and [],但我仍然有语法错误

我使用---syntax-check时的结果:

> The offending line appears to be:

  - name: List users
      shell: cat /etc/passwd | grep "/home" |cut -d ":" -f1| tr "'\n' ' '"
           ^ here
We could be wrong, but this one looks like it might be an issue with
unbalanced quotes. If starting a value with a quote, make sure the
line ends with the same set of quotes. For instance this arbitrary
example:

    foo: "bad" "wolf"

Could be written as:

    foo: '"bad" "wolf"'

在这种情况下,我什么时候必须使用引号

linux ansible redhat quotes
1个回答
0
投票

花Y分钟阅读Learn yaml in Y minute,您将在其中学习:

[[Yaml是] JSON的严格超集,并添加了语法上重要的换行符和缩进,例如Python。但是,与Python不同,YAML不允许使用制表符字符缩进。

您的问题根本不是用引号引起来的,而是在shell任务中代码的缩进:

---
- name: Check users in /home directory
  hosts: REDHAT
  tasks:
    - name: List users
      shell: cat /etc/passwd | grep "/home" |cut -d ":" -f1| tr '\n' ' '
      register: output
      ignore_errors: yes
© www.soinside.com 2019 - 2024. All rights reserved.