CentOS 8
Sponsored Link

Ansible : Use Playbook (variables)2020/03/17

 
This is the usage of variables in Ansible Playbook.
[1] This is the example to use variables.
[cent@dlp ~]$
vi playbook_sample.yml
- hosts: target_servers
  become: yes
  become_method: sudo
  tasks:
  - name: General packages are installed
    yum:
      name: "{{ packages }}"
      state: present
    vars:
      packages:
      - tar
      - wget
      - unzip
    tags: General_Packages

[cent@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

# verify

[cent@dlp ~]$
ansible target_servers -m shell -a "which tar; which wget; which unzip;"

10.0.0.51 | CHANGED | rc=0 >>
/usr/bin/tar
/usr/bin/wget
/usr/bin/unzip
10.0.0.52 | CHANGED | rc=0 >>
/usr/bin/tar
/usr/bin/wget
/usr/bin/unzip
[2] When running Playbook, [GATHERING FACTS] task is always executed, it is the function which Ansible gets informations of target hosts and set them in variables. You can refer and use them in Playbooks. If you'd like to confirm which kinds of variables set, it's possible to output with [setup] module like follows.
[cent@dlp ~]$
ansible 10.0.0.51 -m setup

10.0.0.51 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "10.0.0.51"
        ],
        "ansible_all_ipv6_addresses": [
            "fe80::5054:ff:fe67:a927"
        ],
        "ansible_apparmor": {
            "status": "disabled"
        },
.....
.....

# example of using variables from [GATHERING FACTS]

[cent@dlp ~]$
vi playbook_sample.yml
# refer to [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 }}"

[cent@dlp ~]$
ansible-playbook playbook_sample.yml


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

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

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

TASK [debug] *******************************************************************
ok: [10.0.0.51] => {
    "msg": "CentOS 8.1"
}
ok: [10.0.0.52] => {
    "msg": "CentOS 8.1"
}

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
Matched Content