कार्य केवल तभी करें जब होस्ट समूह से संबंधित न हो


106

यदि कोई वर्तमान समूह का होस्ट किसी निश्चित समूह से संबंधित नहीं है , तो मैं केवल एक कार्य को चलाने में सक्षम होना चाहूंगा । अर्ध छद्म कोड में:

- name: my command
  command: echo stuff
  when: "if {{ ansible_hostname }} not in {{ ansible_current_groups }}"

मुझे यह कैसे करना चाहिए?

जवाबों:


198

यहाँ यह करने का एक और तरीका है:

- name: my command
  command: echo stuff
  when: "'groupname' not in group_names"

group_namesदस्तावेज़ के रूप में एक जादुई चर है: https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#accessing-information-about-other-hosts-with-magic-variables :

group_names उन सभी समूहों की एक सूची (सरणी) है जो वर्तमान होस्ट में है।


3
+1 और यदि आप आसपास की बोली को शामिल नहीं करते हैं तो आपको त्रुटि मिलती है:This one looks easy to fix. It seems that there is a value started with a quote, and the YAML parser is expecting to see the line ended with the same kind of quote.
— पीटर अज़ताई

3
मुझे यह दृष्टिकोण अधिक पठनीय और लिखने में सुविधाजनक लगता है, लेकिन दोनों समान रूप से अच्छी तरह से काम करते हैं। when: inventory_hostname not in groups.certain_groups
— लियाम

4
यह तरीका अधिक मजबूत है inventory_hostname in groups['groupname']क्योंकि ऐसे मामलों में जहां ग्राउट स्वयं मौजूद नहीं होता है, तो Ansible एक त्रुटि फेंक देगा जैसे "सुनिश्चित करें कि आपके चर नाम में अमान्य वर्ण नहीं हैं जैसे '-': 'StrictUndefined' प्रकार का तर्क चलने योग्य नहीं है
— हैमक्स 0

20

आप group_vars/इस तरह से या सीधे मेजबान फ़ाइल में स्थित vars फ़ाइलों में एक नियंत्रण चर सेट कर सकते हैं :

[vagrant:vars]
test_var=true

[location-1]
192.168.33.10 hostname=apollo

[location-2]
192.168.33.20 hostname=zeus

[vagrant:children]
location-1
location-2

और इस तरह से कार्य चलाएं:

- name: "test"
  command: "echo {{test_var}}"
  when: test_var is defined and test_var

2
स्वीकृत उत्तर प्रश्न के लिए अधिक सटीक है, लेकिन इससे आपको एक बेहतर मार्ग मिल जाता है
— nik.shornikov
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.