2017-08-04 116 views
0

我試圖執行現有的ansible劇本時屬性不存在錯誤,我得到這個錯誤:執行ansible劇本

fatal: [default]: FAILED! => {"failed": true, "msg": "ERROR! 'unicode object' has no attribute 'regexp'"} 

在執行ansible劇本的這一部分:

- name: "Add access to pg_hba.conf for DB users" 
    become: yes 
    become_user: postgres 
    lineinfile: dest="{{ PATH_PG_HBA }}" regexp="{{ item.regexp }}" line="{{ item.line }}" state=present create=yes 
    with_items: "{{ DATABASE_ACL }}" 
    notify: restart postgresql 

顯然,正則表達式不存在於對象中。但是因爲我的經驗是有限的,所以我想知道這是否是一個與lineinfile參數有關的通用錯誤,或者是這個可怕的yaml文件的特定錯誤。

DATABASE_ACL: 
- "local {{ DB_NAME }} {{ DB_USER }} md5" 
- "host {{ DB_NAME }} {{ DB_USER }} 127.0.0.1/32 md5" 
- "host {{ DB_NAME }} {{ DB_USER }} 10.0.2.2/32 md5" 

在同一文件夾中被聲明的DB_NAME和DB_USER(字符串):

爲{{DATABASE_ACL}}變量在group_vars夾聲明。

+2

請將您的'DATABASE_ACL'變量納入考慮範圍,您是在哪裏聲明的?內部劇本,group_vars,額外變量,庫存? – Andrew

+0

@Andrew我編輯了這個問題。 – user1919

回答

1

DATABASE_ACL from your group vars是一個字符串列表,並且item in循環是一個unicode字符串。顯然,如你所說,這個錯誤告訴你這個 - unicode字符串沒有屬性regexp。這種精確的錯誤可以在IPython中被複制,如:

In [1]: a = unicode("some string") 

In [2]: a.regexp 
--------------------------------------------------------------------------- 
AttributeError       Traceback (most recent call last) 
<ipython-input-2-26893273be72> in <module>() 
----> 1 a.regexp 

AttributeError: 'unicode' object has no attribute 'regexp' 

如果你想使用你當前的任務,你必須的DATABASE_ACL數據結構改變類型的字典列表在YAML,例如像

DATABASE_ACL: 
- line: "local {{ DB_NAME }} {{ DB_USER }} md5" 
    regexp: "local" 
- line: "host {{ DB_NAME }} {{ DB_USER }} 127.0.0.1/32 md5" 
    regexp: "host .* 127" 
- line: "host {{ DB_NAME }} {{ DB_USER }} 10.0.2.2/32 md5" 
    regexp: "host .* 10.0.2.2" 

由此產生的item將字典,結構爲{line: "your line", regexp: "your regexp"},這將在您的情況下正常工作。