Ubuntu 22.04
Sponsored Link

Ansible : Playbook का उपयोग करें (variables)2023/09/27

 
Ansible Playbook में वेरिएबल्स का उपयोग निम्नलिखित है।
[1] यह वेरिएबल का उपयोग करने का उदाहरण है।
ubuntu@dlp:~$
vi playbook_sample.yml
- hosts: target_servers
  become: yes
  become_method: sudo
  tasks:
  - name: General packages are installed
    apt:
      name: "{{ packages }}"
      state: present
    vars:
      packages:
      - tar
      - wget
      - unzip
    tags: General_Packages

ubuntu@dlp:~$
ansible-playbook playbook_sample.yml --ask-become-pass

BECOME password:

PLAY [target_servers] **********************************************************

TASK [Gathering Facts] *********************************************************
ok: [10.0.0.51]
ok: [10.0.0.52]

TASK [General packages are installed] ******************************************
changed: [10.0.0.52]
changed: [10.0.0.51]

PLAY RECAP *********************************************************************
10.0.0.51                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
10.0.0.52                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

# सत्यापित करें

ubuntu@dlp:~$
ansible target_servers -m shell -a "which tar; which wget; which unzip;"

10.0.0.52 | CHANGED | rc=0 >>
/usr/bin/tar
/usr/bin/wget
/usr/bin/unzip
10.0.0.51 | CHANGED | rc=0 >>
/usr/bin/tar
/usr/bin/wget
/usr/bin/unzip
[2] Playbook चलाते समय, [ATHERING FACTS] कार्य हमेशा निष्पादित होता है, यह वह फ़ंक्शन है जो Ansible लक्ष्य होस्ट की जानकारी प्राप्त करता है और उन्हें चर में सेट करता है। आप उन्हें Playbooks में संदर्भित और उपयोग कर सकते हैं। यदि आप यह पुष्टि करना चाहते हैं कि किस प्रकार के वेरिएबल सेट हैं, तो निम्न प्रकार से [setup] मॉड्यूल के साथ आउटपुट करना संभव है।
ubuntu@dlp:~$
ansible 10.0.0.50 -m setup

10.0.0.50 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "10.0.0.50"
        ],
        "ansible_all_ipv6_addresses": [
            "fe80::5054:ff:fef6:f1b2"
        ],
        "ansible_apparmor": {
            "status": "disabled"
        },
        "ansible_architecture": "x86_64",
        "ansible_bios_date": "04/01/2014",
        "ansible_bios_vendor": "SeaBIOS",
.....
.....

# [GATHERING FACTS] से वेरिएबल का उपयोग करने का उदाहरण

ubuntu@dlp:~$
vi playbook_sample.yml
# [ansible_distribution], [ansible_distribution_version] का संदर्भ लें

- hosts: target_servers
  tasks:
  - name: Refer to Gathering Facts
    command: echo "{{ ansible_distribution }} {{ ansible_distribution_version }}"
    register: dist
  - debug: msg="{{ dist.stdout }}"

ubuntu@dlp:~$
ansible-playbook playbook_sample.yml


PLAY [target_servers] **********************************************************

TASK [Gathering Facts] *********************************************************
ok: [10.0.0.52]
ok: [10.0.0.51]

TASK [Refer to Gathering Facts] ************************************************
changed: [10.0.0.52]
changed: [10.0.0.51]

TASK [debug] *******************************************************************
ok: [10.0.0.51] => {
    "msg": "Ubuntu 22.04"
}
ok: [10.0.0.52] => {
    "msg": "Ubuntu 22.04"
}

PLAY RECAP *********************************************************************
10.0.0.51                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
10.0.0.52                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
मिलान सामग्री