2017-04-06 76 views
1

一個csv字符串我有這樣Ansible |如何構建從字典

customers: 
    abc: 
    id: 1 
    status: active 
    def: 
    id: 2 
    status: inactive 
    ghi: 
    id: 3 
    status: active 
    jkl: 
    id: 4 
    status: active 

字典什麼是構建一個字符串像下面只有活動的客戶的最佳方式?

abc,ghi,jkl 

回答

2

例如:

--- 
- hosts: localhost 
    gather_facts: no 
    vars: 
    customers: 
     abc: 
     id: 1 
     status: active 
     def: 
     id: 2 
     status: inactive 
     ghi: 
     id: 3 
     status: active 
     jkl: 
     id: 4 
     status: active 
    tasks: 
    - debug: 
     msg: "{{ customers | dictsort | selectattr('1.status','equalto','active') | map(attribute='0') | join(',') }}" 
+0

還有一個問題,我怎麼能還追加一個字符串(例如cust_),使輸出看起來像這樣? '''cust_abc,cust_ghi,cust_jkl ''' – GMaster

+0

''在'join'前加'map('regex_replace','^','cust _')' –

+0

太棒了!非常感謝。你爲我節省了幾個小時的工作。 – GMaster