CentOS 6
Sponsored Link

Ansible : Use Playbook#22015/04/22

 
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 | egrep 'vim-enhanced|wget|unzip'" --ask-become-pass

SUDO password:
10.0.0.52 | success | rc=0 >>
wget-1.12-5.el6_6.1.x86_64
vim-enhanced-7.2.411-1.8.el6.x86_64
unzip-6.0-2.el6_6.x86_64

10.0.0.51 | success | rc=0 >>
wget-1.12-5.el6_6.1.x86_64
vim-enhanced-7.2.411-1.8.el6.x86_64
unzip-6.0-2.el6_6.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.52]
ok: [10.0.0.51]

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

TASK: [debug msg="{{ dist.stdout }}"] *****************************************
ok: [10.0.0.51] => {
    "msg": "CentOS 6.6"
}
ok: [10.0.0.52] => {
    "msg": "CentOS 6.6"
}

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