Ansible:转义引号和列出用户密码到期信息

时间:2017-05-28 06:07:02

标签: ansible

如何在shell模块引用中转义? 我尝试过如下:

- name: UList
  shell: "cut -f 1 -d: /etc/passwd | sudo xargs -n 1 -I {} bash -c \" echo {} ; chage -l {}\""

- name: UList
  shell: "cut -f 1 -d: /etc/passwd | sudo xargs -n 1 -I {} bash -c \' echo {} ; chage -l {}\'"

哪里出错?

1 个答案:

答案 0 :(得分:1)

这本剧本对我有用,希望这对你也有帮助。如果您有问题转义'引用

,则可以使用单引号"作为命令

要么这样做

'cut -f 1 -d: /etc/passwd | sudo xargs -n 1 -I {} bash -c " echo {} ; chage -l {}"' 或者

"cut -f 1 -d: /etc/passwd | sudo xargs -n 1 -I {} bash -c ' echo {} ; chage -l {}'"

两者都在工作,我测试了它。

---
- name: Set my hosts variable
  hosts: localhost
  tasks:
  - name: UList
    shell: 'cut -f 1 -d: /etc/passwd | sudo xargs -n 1 -I {} bash -c " echo {} ; chage -l {}"'
    register: result
  - name: debug
    debug:
     msg: "{{result}}"

对于您期望的输出,您可以使用

awk -F':' '{ system("echo " $1 " && chage -l " $1) }' /etc/passwd

Command explanation

---
- name: Set my hosts variable
  hosts: localhost
  tasks:
  - name: UList
    shell: "awk -F':' '{ system(\"echo \" $1 \" && chage -l \" $1)  }' /etc/passwd"
    register: result
  - name: debug
    debug:
     msg: "{{result}}"