kurgulus
kurgulus

Reputation: 75

Ansible: escape quotes and listing users password expiry information

How to escape in shell module quotation? I've tried as follows:

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

and

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

Where is a mistake?

Upvotes: 2

Views: 2033

Answers (1)

Sahil Gulati
Sahil Gulati

Reputation: 15141

This playbook worked for me, hope this will be helpful for you too. You can use single quotes' for a command if you have an issue escaping " quote

Either do it this way

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

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

Both are working i have tested it.

---
- 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}}"

Or

For the output you are expecting for that you can use

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}}"

Upvotes: 1

Related Questions