Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Latest commit

 

History

History
385 lines (287 loc) · 10.9 KB

variables.md

File metadata and controls

385 lines (287 loc) · 10.9 KB

Variables in Ansible

Variables in Ansible

  • Several different types:
    • Inventory variables
    • Play scoped variables
    • Task variables
    • Extra variables

Inventory Variables

  • Scoped to a host throughout the execution of a playbook
  • Assigned in
    • The inventory file
    • Specific subdirectories
      • host_vars/host
      • group_vars/group
  • Can be referenced directly when executing play on host
  • Available in hostvars dictionary

Variables in Inventory File

  • Assign variable to a single host
    
    

[web] web1.mycompany.com ansible_host=152.240.43.12 opt2=arg2 web2.mycompany.com .

  • Assign variables to groups of hosts
    
    

[web] web1.mycompany.com ansible_host=152.240.43.12 web2.mycompany.com [web:vars] proxy=web.mycompany.com

Using the host_vars directory

  • Ansible will automatically load files under host_vars directory
    • Files with same name as particular host
    • Any files in a directory with the same name as a host
      
      [web]
      web1.myhost.com
      web2.myhost.com
      
      
      $INTRO_ANSIBLE_DIR/working-with-playbooks
      └── host_vars
          ├── web1.myhost.com.yml
          └── web2.myhost.com
              └── vars.yml
      
_.yml_ or _.yaml_ suffix is optional. Files can also be JSON
Exercise: create host inventory
  • Create inventory variables for myserver host
  • Try either way:

     cd $INTRO_ANSIBLE_DIR/working-with-playbooks
     mkdir -p host_vars
     gedit host_vars/myserver.yml

     mkdir -p host_vars/myserver
     gedit host_vars/myserver/vars.yml

    # variables for myserver
    ---
    foo: bar

Using group_vars

  • Similar to host_vars except for groups
    • Files with same name as particular group
    • Any files in a directory with the same name as a group
      
      [web]
      web1.myhost.com
      [app]
      app1.myhost.com
      
      
      $INTRO_ANSIBLE_DIR/working-with-playbooks
      └── group_vars
          ├── web.yml
          └── app
              └── vars.yml
      
Exercise: Create group_vars for our app
  • Create a group_vars directory and variable file

    cd $INTRO_ANSIBLE_DIR/working-with-playbooks/
    mkdir -p group_vars
    gedit group_vars/web.yml
    

    mkdir -p group_vars/web
    gedit group_vars/web/vars.yml
    

    # variables for "web" group
    ---
    bizz: buzz

Using inventory variables

  • Inventory variables available to Ansible throughout playbook run
$ less $INTRO_ANSIBLE_DIR/working-with-playbooks/templates/index.html.j2
.
.
{% if foo is defined %}
<p>
The value for foo is <em>{{ foo }}</em>
</p>
{% endif %}
{% if bizz is defined %}
<p>
The value for bizz is <em>{{ bizz }}</em>
</p>
{% endif %}
.
.

Using Inventory Variables

ansible-playbook static-site.yml
  • Run the ansible playbook again
  • Note behaviour of Copy up static website html task
  • Reload website when finished

hostvars

  • Dictionary with variables from each host (including inventory variables)
  • Indexed on hostname from inventory
    • hostvars['myserver']['foo'] == hostvars.myserver.foo
  • Can be accessed across plays in the same playbook run

Using hostvars

  • Have a look at use-hostvars.yml
  • Run static-site.yml and use-hostvars.yml
    ansible-playbook static-site.yml use-hostvars.yml
    
  • Change foo to hostvars.myserver.foo in playbook-localhost.yml and run playbooks
Exercise: View a group variable form hostvars
  • The bizz variable still appears as undefined in playbook run
  • How would you find that in hostvars?
  • A group variable will be defined for every host in a group
- name: Output of bizz
  debug:
    var: hostvars.myserver.bizz

Play-scoped Variables

  • Declared in control section of a play
    • vars - static declaration
    • vars_prompt - interactively prompt user when running playbook
  • Can be referenced directly while in play

Play-scoped Variables

  • Open static-site.yml in editor
  • Edit as shown:
  • Rerun ansible-playbook

- name: Set up static website with nginx
  hosts: myserver
  become: true
  vars_prompt:
    - name: "participant_name"
      prompt: "What is your name"
      private: no
  vars:
    base_path: "/"
    course: Introduction to ansible
    lesson:
      name: Working with play variables
      session: 1
  tasks:
  .
  .

Task Variables

  • Contain data discovered while executing a task
  • Gathering facts phase of play execution
  • Tasks that gather user input
  • Become part of the host scope once defined
  • In a play they can be directly referenced
  • In subsequent plays are available in hostvars dictionary for host

Assigning Variables with set_fact

tasks:
  - name: Assign an arbitrary variable
    set_fact:
      person_name: Homer Simpson

  - name: Assign dictionary
    set_fact:
      person_details:
        street: 754 Evergreen Terrace
        city: Springfield
        state: ST

Assigning Variables with register

- name: Execute code and capture the output
  command: uptime
  register: uptime_output

  .
  .
- name: do something with output of uptime
  debug:
    var: {{ uptime_output.stdout }}
Exercise: Assign Task Variables

    tasks:
        - name: set name of person
          set_fact:
            person_name: Homer Simpson

        - name: Get output of uptime
          command: uptime
          register: uptime_output
        .
        .
  • Use set_fact and register in static-site.yml to assign task variables
  • Rerun ansible playbook

Gathering facts

  • Plugin run before play executes
    • setup module
    • Collects detailed information about host and attaches to host scope
    • You can get an idea by running
      • ansible myserver -m setup
    • Filter facts to find variables you are interested in:
      • ansible myserver -m setup -a 'filter=ansible_distribution*'
Exercise: Display System Facts
  • Uncomment area in templates/index.html.j2
  • Re-run ansible-playbook and visit static website
<!--   remove line
{% if ansible_distribution is defined %}
    <p>  
    System facts: {{ ansible_distribution }} {{ ansible_distribution_major_version }} {{ ansible_architecture }}
    </p>
{% endif %}
-->   remove line
Exercise: Turn off fact gathering
  • Edit static-site.yml as follows
  • Re-run ansible-playbook and visit static website

    - name: Set up static website with nginx
      hosts: myserver
      become: true
      gather_facts: false
      tasks:
        .
        .

Extra Variables

  • Variables passed to ansible or ansible-playbook via command line
    • ansible-playbook --extra-vars="key1=value1 key2=value2" playbook.yml
    • ansible-playbook -e key1=value1 -e key2=value2 playbook.yml
    • ansible-playbook -e @extra-variables.yml playbook.yml

Variable Precedence

Exercise: Variable Precedence
  • Pass extra variables to ansible playbook to override previous examples
  • For example you can create a new file called override-vars.yml
    
       person_name: Marge Simpson
       lesson:
         name: Override variables
         session: 2
    

  • Then add this on the command line
    ansible-playbook static-site.yml -e @override-vars.yml
    

Summary

  • There are several types of variables in Ansible
    • inventory
    • play scoped
    • task variables
    • extra