पाठ 15 / 26

register, Tags और Blocks

नतीजे पकड़ें, tags से चुने tasks चलाएँ और blocks से errors सँभालें।

नतीजे, चयन, सुधार

register: task का नतीजा (stdout, return code, changed स्थिति) बाद के tasks के लिए variable में रखता है। Tags tasks को लेबल करते हैं ताकि आप --tags और --skip-tags से उपसमूह चला या छोड़ सकें। block: tasks को समूहित करता है, और rescue: / always: भाग सुधारने योग्य विफलताओं के लिए try/catch/finally जैसा व्यवहार देते हैं। Command-शैली के tasks के लिए Ansible को बताने को changed_when: और failed_when: उपयोग करें कि क्या बदलाव या विफलता गिना जाए।

Registered output, चलाकर

सिर्फ़ info tag चलाने पर एक हानिरहित command का registered stdout दिखता है।

ansible-playbook -i inventory.ini vars.yml --tags info

Output:

"out.stdout": "hello"

rescue वाला block

जोखिम वाला task विफल हो तो rescue task चलता है और play आगे बढ़ता है। (उदाहरण; यहाँ चलाया नहीं गया।)

- name: Deploy with rollback
  block:
    - name: Switch to new release
      ansible.builtin.command: /opt/app/switch_release.sh {{ release }}
  rescue:
    - name: Roll back to previous release
      ansible.builtin.command: /opt/app/switch_release.sh {{ previous_release }}
  always:
    - name: Record the attempt
      ansible.builtin.debug:
        msg: "deploy attempt finished"

त्वरित जाँच: `--tags info` क्या करता है?

  • सिर्फ़ info tag वाले tasks चलाता है
  • info tag वाले tasks हटाता है
  • info tag वाले tasks छोड़ता है
  • Tags encrypt करता है
Answer

सिर्फ़ info tag वाले tasks चलाता है — Tag चयन run को चुने tasks तक सीमित करता है।