CentOS 7
Ads

Ansible : Use Playbook#2
2015/07/15
 
It's possible to use 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={{ item }} state=installed
    with_items:
      - vim-enhanced
      - wget
      - unzip
    tags: General_Packages

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

SUDO password:

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

GATHERING FACTS ***************************************************************
ok: [10.0.0.52]
ok: [10.0.0.51]

TASK: [General packages are installed] ****************************************
changed: [10.0.0.52] => (item=vim-enhanced,wget,unzip)
changed: [10.0.0.51] => (item=vim-enhanced,wget,unzip)

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

# confirm settings

[cent@dlp ~]$
ansible target_servers -m shell -a "rpm -qa | grep -E 'vim-enhanced|wget|unzip'" --ask-become-pass

SUDO password:
10.0.0.51 | success | rc=0 >>
vim-enhanced-7.4.160-1.el7.x86_64
wget-1.14-10.el7_0.1.x86_64
unzip-6.0-15.el7.x86_64

10.0.0.52 | success | rc=0 >>
vim-enhanced-7.4.160-1.el7.x86_64
wget-1.14-10.el7_0.1.x86_64
unzip-6.0-15.el7.x86_64
[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.
# display the contents of "GATHERING FACTS" with "setup" module

[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::216:36ff:fe29:7f1c"
        ],
        "ansible_architecture": "x86_64",
        "ansible_bios_date": "01/01/2007",
        "ansible_bios_version": "0.5.1",
.....
.....

# the 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] *********************************************************

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 msg="{{ dist.stdout }}"] *****************************************
ok: [10.0.0.51] => {
    "msg": "CentOS 7.1.1503"
}
ok: [10.0.0.52] => {
    "msg": "CentOS 7.1.1503"
}

PLAY RECAP ********************************************************************
10.0.0.51                  : ok=3    changed=1    unreachable=0    failed=0
10.0.0.52                  : ok=3    changed=1    unreachable=0    failed=0
 
Tweet