2017-07-15 44 views
1

我希望執行Salt狀態的時候並不總是在另一個狀態發生更改時發生,但僅適用於特定的更改。這看起來像我必須使onchanges/onchanges_in取決於具體的變化。根據已更改的內容製作salt'onchanges`必需條件

The respective bug report已關閉,稱「現在狀態已完全解決,州可以訪問正在運行的運行字典和低狀態」。但是,我沒有找到任何文件,也沒有任何關於「運行字典」的解釋。

所以我想這個問題也可以改寫爲「我如何訪問onchanges必要條件中的'運行字典'?」,但我願意接受針對原始問題的任何解決方案。謝謝你的幫助!


更新:註釋問一個具體的例子,所以這裏是我的使用情況:由於大多數國家的模塊,user.present既可以現有的(用戶)對象的更新域或創建一個新的。然後,我想運行第二個狀態模塊,當且僅當特定字段已更改和/或剛創建對象時。在Ansible中,爲了比較,我想register a variable並通過它訪問模塊的結果。

那麼,我爲什麼要這樣做呢?

本質上,我想在Linux上創建用戶帳戶,並讓他們能夠設置自己的密碼(通過SSH密鑰登錄時)。 user.present支持empty_password爲此目的,但它不與enforce_password很好地發揮。這意味着在手動設置密碼後,重複的狀態運行將再次清除該密碼。有人甚至可以認爲這是Salt中的一個錯誤,但不同的user.present字段之間的相互作用是錯綜複雜且有爭議的。

我的解決方案是先創建帳戶,然後運行module.run狀態,然後執行shadow.del_password。這是通過onchanges_in必要條件實現的。但是,不應該爲任何更改而觸發刪除密碼,但只有在創建用戶帳戶時纔會觸發,這也是我的狀態僅觸摸密碼的唯一情況。否則,將用戶添加到組中將會清除密碼。爲此,我想我將不得不查看user.present更改的詳細信息。

Create user account for dummy: 
    user.present: 
    - name: dummy 
    - gid_from_name: True 
    - remove_groups: False 
    # TODO: This should be made more specific 
    - onchanges_in: 
     - module: Allow dummy to set a password 

Allow dummy to set a password: 
    module.run: 
    - name: shadow.del_password 
    - m_name: dummy 
    # Make sure that this is not executed accidentally if no `onchanges_in` is present 
    - onchanges: [] 
    - require: 
     - user: Create user account for dummy 
+0

請你把這個問題更具體的寫你想要解決(什麼樣的變化的問題,什麼樣的行動?) – ahus1

+0

@ ahus1我已經添加了一個詳細的用例示例。 – F30

回答

2

我不知道具體的onchanges或「跑字典」,但是,對於您的特定用途的情況下,可以使用條件,以使您的密碼,結算狀態,僅在需要時,如:

Create user account for dummy: 
    user.present: 
    - name: dummy 
    - gid_from_name: True 
    - remove_groups: False 

{% if salt['user.info']('dummy') == {} %} 
# Only clear the password if the account didn't exist before 
Allow dummy to set a password: 
    module.run: 
    - name: shadow.del_password 
    - m_name: dummy 
    - require: 
     - user: Create user account for dummy 
{% endif %} 
+1

這感覺有點不好,不能完全泛化,但我想這是針對特定問題的最好的已知解決方案。 – F30

0

我想你在這種情況下要使用的是module.wait而不是module.run。默認情況下,module.wait不會做任何事情,除非有其他問題。另外,onchanges_in出於某種原因(我認爲this issue)對我而言不適合module.wait。我試過watch_in,它完成了這項工作。

我試過下面的代碼,它似乎工作得很好。它創建一個密碼爲空的用戶時,如果用戶已經在那裏沒有任何改變:

Create user account for dummy: 
    user.present: 
    - name: dummy 
    - gid_from_name: True 
    - remove_groups: False 
    # TODO: This should be made more specific 
    - watch_in: 
     - module: Allow dummy to set a password 

Allow dummy to set a password: 
    module.wait: 
    - name: shadow.del_password 
    - m_name: dummy 
    - require: 
     - user: Create user account for dummy 
+0

這仍然存在每次用戶帳號發生變化時都執行'shadow.del_password'的問題,例如,將它添加到一個組(確實不是我最小的例子,但在實際的代碼中)。 – F30

+0

關於'onchanges'和'run'與'watch'和'wait',有一個[鹽開發者的聲明](https://github.com/saltstack/salt/issues/29492#issuecomment-162967147)從2015年宣佈'等待'狀態將被棄用。 (在[此郵件列表主題]之前(http://grokbase.com/t/gg/salt-users/152q47sx85/should-wait-sates-be-deprecated-in-favor-of-onchanges)。) – F30

+1

對不起,你說得對,我誤解了你的問題。在這種情況下,我認爲它不是@ christophe-drevet-droguet建議的就是寫一個python狀態。 – alexK