DevOps Classroom Series – 14/Mar/2020

Ansible variables

  • Ansible variables can be defined

    • Playbooks
    • Inventory
  • Consider this inventory and playbook

#inventory
172.31.25.93
172.31.25.22

#playbook
- hosts: all
  become: yes
  tasks:
    - name: Install apache2
      apt:
        name: apache2
        update_cache: yes
        state: present
      when: ansible_os_family == "Debian"
    - name: Install httpd
      yum:
        name: httpd
        state: present
      when: ansible_os_family == "RedHat"
    - name: Enable apache2
      service:
        name: apache2
        enabled: yes
        state: started
      when: ansible_os_family == "Debian"
    - name: Enable httpd
      service:
        name: httpd
        enabled: yes
        state: started
      when: ansible_os_family == "RedHat"
  • In the above playbooks tasks very redundant, so we can generalize/paramterize thes steps by making use of variables in ansible Refer Here
  • Defining variables in inventory
172.31.25.93  apache_package=apache2
172.31.25.22  apache_package=httpd
  • Whenenver you would like to use a variable, follow the syntax {{ variable_name }}
  • Lets apply this to playbook
---
- hosts: all
  become: yes
  tasks:
    - name: variable value
      debug:
        var: "{{ apache_package }}"
        verbosity: 1
    - name: install apache
      package:
        name: "{{ apache_package }}"
        state: present
    - name: enable and start apache
      service:
        name: "{{ apache_package }}"
        enabled: yes
        state: started
  • Variable values can be overriten while executing
ansible-playbook -i hosts -e 'apache_package=tomcat8' ex2.yaml
# this will overrite the variable name from apache_package=apache2 to tomcat8
  • To be using variables effeciently, we should be good at
    • defining varaibles:
      • What are different ways of defining variables
    • using varibales:
      • How to use variable in the module
    • Changing/Overriting Variables:
      • If the same variable with different value is defined at multiple places which will be applied in playbook (which will have more priority)
  • Ansible Playbook command line Refer Here

Inventory

  • Two kinds of inventory
    • Static
    • Dynamic

Ansible Static Inventory

  • Ansible inventory has
    • groups: logical collection of hosts
    • hosts: individual machines ip address/hostname
  • host can be present in multiple groups
  • Now consider the below sample organization Preview
  • The inventory for this in standard (ini)
[webservers]
host1
host2
host3

[appservers]
host4
host5
host6

[dbservers]
host7
host8
host9

[monitoring]
host10

[ubuntu]
host1
host2
host3
host7
host8
host9

[redhat]
host10
host4
host5
host6

  • In the playbook, hosts section group name can be used
# Inventory
[acs]
localhost apache_package=apache2

[webserver]
172.31.25.93  apache_package=apache2
172.31.25.22  apache_package=httpd

# Playbook
---
- hosts: webserver
  become: yes
  tasks:
    - name: variable used in this playbook
      debug:
        msg: "apache_package = {{ apache_package }}"
    - name: Install apache
      package:
        name: "{{ apache_package }}"
        state: present
    - name: installed apache
      debug:
        msg: "apache is installed"
    - name: Enable apache
      service:
        name: "{{ apache_package }}"
        enabled: yes
        state: started

  • Variables defined at individual hosts are called as hostvars and defined at group level are group vars

Experiments with group and host variables

  1. Same variable name in all hosts defined as hostvars

    • Ensure you have following inventory and playbook
    # Inventory
    [acs]
    localhost package_name=test1
    
    [webserver]
    172.31.25.93  package_name=test1
    172.31.25.22  package_name=test1
    
    ## Playbook
    ---
    - hosts: all
    tasks:
        - name: display variable value
        debug:
            msg: " package_name = {{ package_name }} "
    
    
    • Execute the following commands and observe the outputs
    ansible-playbook -i demohosts demo.yaml --list-hosts
    ansible-playbook -i demohosts demo.yaml
    
  2. Variable defined at group level

    • Create a group var for webservers group
    [acs]
    localhost package_name=test1
    
    [webserver]
    172.31.25.93
    172.31.25.22
    
    [webserver:vars]
    package_name=test2
    
    
    • Execute the following commands and observe the outputs
    ansible-playbook -i demohosts demo.yaml --list-hosts
    ansible-playbook -i demohosts demo.yaml
    # Output
    ok: [localhost] => {
        "msg": " package_name = test1 "
    }
    ok: [172.31.25.93] => {
        "msg": " package_name = test2 "
    }
    ok: [172.31.25.22] => {
        "msg": " package_name = test2 "
    }
    
    
  3. Try with the following inventory

[acs]
localhost package_name=test1

[webserver]
172.31.25.93
172.31.25.22  package_name=test1

[webserver:vars]
package_name=test2

  1. use the same inventory and execute the following command
ansible-playbook -e 'package_name=test3' -i demohosts demo.yaml
# output
ok: [localhost] => {
    "msg": " package_name = test3 "
}
ok: [172.31.25.93] => {
    "msg": " package_name = test3 "
}
ok: [172.31.25.22] => {
    "msg": " package_name = test3 "
}

  • Organizing variable can be optimized by creating host_vars and group_vars folder wherever your inventory is present. Refer Here for more info. Preview

  • Inventories can be organized in yaml files as well Preview

---
all:
  children:
    webserver:
      hosts:
        192.168.10.100:
        192.168.10.102:
        test-01.qt.internal:
    dbserver:
      hosts:
        192.168.10.104:
        192.168.10.106:
    appserver:
      hosts:
        192.168.10.108:
        192.168.10.110:

  • testing hosts can be done by using –list-hosts
ansible-playbook -i hosts.yaml demo.yml --list-hosts
  • Exercise-1: Try to define variables in yaml based inventory
  • Exercise-2: Write a ansible playbook to install lamp server on ubuntu machines based on documentation over here. skip mysql installations.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner