Information about Ansible: magic variables
المتغيرات السحرية
يمكنك الوصول إلى معلومات حول عمليات Ansible ، بما في ذلك إصدار Python المستخدم والمضيفين والمجموعات الموجودة في المستودع وأدلة كتب التشغيل (playbooks) والأدوار باستخدام المتغيرات "السحرية". مثل متغيرات الاتصال ، المتغيرات السحرية هي متغيرات خاصة. أسماء المتغيرات السحرية محجوزة - لا تقم بتعيين متغيرات بهذه الأسماء. البيئة المتغيرة محجوزة أيضًا.
المتغيرات السحرية الأكثر استخدامًا هي hostvars ، و groups ، و group_names ، و inventory_hostname. باستخدام hostvars ، يمكنك الوصول إلى المتغيرات المحددة لأي مضيف في play، في أي وقت في playbook. يمكنك الوصول إلى حقائق Ansible باستخدام متغير hostvars أيضًا ، ولكن فقط بعد جمع الحقائق (أو تخزينها مؤقتًا).
إذا كنت ترغب في تكوين خادم قاعدة البيانات باستخدام قيمة "حقيقة" من عقدة أخرى ، أو قيمة متغير مخزون مخصص لعقدة أخرى ، فيمكنك استخدام hostvars في قالب أو في سطر إجراء:
{{ hostvars['test.example.com']['ansible_facts']['distribution'] }}
مع groups، قائمة بجميع المجموعات (والمضيفين) في المخزون ، يمكنك تعداد جميع المضيفين داخل المجموعة. على سبيل المثال:
{% for host in groups['app_servers'] %}
# something that applies to all app servers.
{% endfor %}
يمكنك استخدام groups و hostvars معًا للعثور على جميع عناوين IP في المجموعة.
{% for host in groups['app_servers'] %}
{{ hostvars[host]['ansible_facts']['eth0']['ipv4']['address'] }}
{% endfor %}
يمكنك استخدام هذا الأسلوب لتوجيه خادم وكيل للواجهة الأمامية إلى جميع المضيفين في مجموعة خوادم التطبيقات ، لإعداد قواعد جدار الحماية الصحيحة بين الخوادم ، وما إلى ذلك. يجب عليك إما تخزين الحقائق مؤقتًا أو جمع الحقائق لهؤلاء المضيفين قبل المهمة التي تملأ القالب.
{% if 'webserver' in group_names %}
# some part of a configuration file that only applies to webservers
{% endif %}
يمكنك استخدام المتغير السحري inventory_hostname ، اسم المضيف كما تم تكوينه في مخزونك ، كبديل لـ ansible_hostname عند تعطيل جمع الحقائق. إذا كان لديك FQDN طويل ، فيمكنك استخدام inventory_hostname_short ، والذي يحتوي على الجزء حتى الفترة الأولى ، بدون باقي النطاق.
Other useful magic variables refer to the current play or playbook. These vars may be useful for filling out templates with multiple hostnames or for injecting the list into the rules for a load balancer.
ansible_play_hosts is the list of all hosts still active in the current play.
ansible_play_batch is a list of hostnames that are in scope for the current ‘batch’ of the play.
The batch size is defined by serial, when not set it is equivalent to the whole play (making it the same as ansible_play_hosts).
ansible_playbook_python is the path to the python executable used to invoke the Ansible command line tool.
inventory_dir is the pathname of the directory holding Ansible’s inventory host file.
inventory_file is the pathname and the filename pointing to the Ansible’s inventory host file.
playbook_dir contains the playbook base directory.
role_path contains the current role’s pathname and only works inside a role.
ansible_check_mode is a boolean, set to True if you run Ansible with --check.
Author