2015-11-22 8 views
14

में एकल कार्य पर मैं का उपयोग करते समय ansible में एकाधिक स्थिति का मूल्यांकन करना चाहते हैं कई स्थितियों के लिए जांच की जा रही है, यहाँ मेरी प्लेबुक है:का उपयोग कर "जब" ansible

:

- name: Check that the SSH Key exists 
    local_action: 
    module: stat 
    path: "/home/{{ login_user.stdout }}/{{ ssh_key_location }}" 
    register: sshkey_result 

- name: Generating a new SSH key for the current user it's not exists already 
    local_action: 
     module: user 
     name: "{{ login_user.stdout }}" 
     generate_ssh_key: yes 
     ssh_key_bits: 2048 
    when: sshkey_result.rc == 1 and (github_username is undefined or github_username |lower == 'none') 

यहां संदर्भ के लिए मेरी वर फ़ाइल है

TASK: [test | Check that the SSH Key exists] ********************************** 
ok: [localhost -> 127.0.0.1] 

TASK: [test | Generating a new SSH key for the current user it's not exists already] *** 
fatal: [localhost] => error while evaluating conditional: sshkey_result.rc == 1 and (github_username is undefined or github_username |lower == 'none') 

     FATAL: all hosts have already failed -- aborting 

किसी ने मुझसे बाहर बिंदु सकता है:

--- 
vpc_region: eu-west-1 
key_name: my_github_key 
ssh_key_location: .ssh/id_rsa.pub 

जब मैं इस प्लेबुक निष्पादित करने के लिए प्रयास करते हैं, मैं इस त्रुटि हो रही है कि हम एकल कार्य पर उत्तरदायी के साथ कई स्थितियों का उपयोग कैसे कर सकते हैं।

धन्यवाद

उत्तर

3

समस्या अपने सशर्त साथ, इस हिस्से sshkey_result.rc == 1 में है क्योंकि sshkey_resultrc विशेषता शामिल नहीं है और पूरे सशर्त विफल रहता है।

यदि आप जांचना चाहते हैं कि फ़ाइल मौजूद है तो exists विशेषता देखें।

यहां आप read more about stat module and how to use it कर सकते हैं।

+2

@nvartolomei क्या कहा करने के लिए एक अनुवर्ती के रूप में, आप 'sshkey_result' की तरह एक चर है जब मैं यह अत्यंत जबकि एक प्लेबुक डिबगिंग/लेखन अपनी सेट के तुरंत बाद अपने मूल्य प्रदर्शित करने के लिए उपयोगी पाते हैं। '-debug: var = sshkey_result' कार्य में जोड़ना आपको दिखाएगा कि जैसा कि उन्होंने कहा था, इस संदर्भ में 'आरसी' मौजूद नहीं है, और यह आपको यह भी दिखाएगा कि उस चर के गुण * क्या मौजूद हैं। –

14

आप इस तरह उपयोग कर सकते हैं। आधिकारिक दस्तावेज़ों पर वापस

when: condition1 == "condition1" or condition2 == "condition2" 

लिंक: The When Statement

कृपया भी देखें। सार यूआरएल https://gist.github.com/marcusphi/6791404