Variable definitions and references in Ansible playbook

Source: Internet
Author: User
Tags json vars vmware fusion ansible playbook

There are many ways to define and reference variables in Ansible playbook, which are tested in the local environment and collated as follows. test Environment:

Using your own Mac notebook to install Ansible as the control server, the VMware Fusion virtual machine runs on a CENTOS7 virtual machine (192.168.243.146) as a remote controlled host tested with Playbook as follows:

Cat Test.yml
---
-hosts:test
  remote_user:root
  tasks:
      -Name:debug
        debug:msg= "the {{ Inventory_hostname}} Value is {{keyvalue}} "
1 Defining variables in the inventory file

Define a variable in the inventory file (the default is the Etc/ansible/hosts file) and define a variable named KeyValue as follows:

---
# define variables for a single host
192.168.243.146 keyvalue=centos

[test]
192.168.243.146

# Group definition variables
[ Test:vars]
keyvalue=vmware centos7

As shown above, there are two ways of defining variables and group definitions for a single host.
1) group-defined variables are scoped to all hosts under the group
2) When both definitions are present, Ansible takes precedence over a single host-defined variable value of 2 to define variables by Host_vars and Group_vars directories

The/etc/ansible/directory is the ansible default profile directory on a Linux system (the default configuration directory for Mac Systems is/usr/local/etc/ansible/), where Host_vars and Group_ are created in this directory VARs Two directories are used to store files that define variables. Such as:
Variables for a single host

Cat host_vars/192.168.243.146
---
keyvalue:192.168.243.146@host_vars

Variables for the test group

Cat Group_vars/test
---
keyvalue:test@group_vars

Similarly, a single host-defined variable takes precedence over a group-defined variable. In comparison with the variable definition method described in 1, the ansible uses variables from high to low in order of precedence:
Define variables under Host_vars
A single host-defined variable in inventory
Define variables under Group_vars
Inventory group definition Variable 3 passed through the Ansible-playbook command line

When executing the playbook command, pass in the parameters via the-E option:

Ansible-playbook test.yml-e "keyvalue=inputed"

In addition, Ansible-playbook supports the way in which Yaml and JSON files pass in variables:

Cat vars.yml
---
keyvalue:vars@yaml
Cat Vars.json
{"KeyValue": "Vars@json"}


The variables passed in this way are higher than those defined in the variables described in 1 and 2. 4 using the VARs field definition in the Playbook Yaml file

Cat Test.yml
---
-hosts:test
  remote_user:root
  vars:
      keyvalue:vars in Playbook
  tasks:
      -Name:debug
        debug:msg= "The {{inventory_hostname}} Value is {{keyvalue}}"

The variables are defined directly in the Playbook through the VARs field. The priority is higher than the 1 and 2 definition methods, less than the 3 command-line incoming method. 5 using the Vars_files field definition in the Playbook Yaml file

Cat Test.yml
---
-hosts:test
  remote_user:root
  vars_files:
      -vars.yml
  tasks:
      - Name:debug
        debug:msg= "The {{inventory_hostname}} Value is {{keyvalue}}"

Through the Vars_files field, the file defining variables is introduced directly into the playbook, supporting the definition variables of YAML and JSON two file formats. The priority is higher than the 1 and 2 definition methods, less than the 3 command-line incoming method. 6 passing variables using register

The Register method is used to pass variables between tasks.

Cat Register.yml
---
-hosts:test
  remote_user:root
  tasks:
      -name:register test
        Shell: Hostname
        Register:info
      -name:display info
        debug:msg= "hostname is {info}}"


The info variable defined by the register is used in the second task to view the results of the hostname command executed in the previous task. As you can see in the results of the playbook run, info returns a Python dictionary data that can be referenced by info[' stdout ' if you want to see only the stdout part of the information.

Cat Register.yml
---
-hosts:test
  remote_user:root
  tasks:
      -name:register test
        Shell: Hostname
        Register:info
      -name:display info
        debug:msg= "hostname is {{info[' stdout '}}"

7 using Vars_prompt to interactively pass in variables

By defining the variable name and interactive cue information for vars_prompt in playbook, you can implement the value of the passed-in variable by interacting when you run playbook.

Cat Prompt.yml
---
-hosts:test
  remote_user:root
  vars_prompt:
      -Name: "Var1"
        prompt: "input Value for var1 "
        private:no
      -Name:" Var2 "
        prompt:" Input value for var2 "
        private:yes
        default: ' tes T var2 '
  tasks:
      -name:display var1
        debug:msg= "The value of Var1 is {{var1}}"
      -Name:display var2< C15/>debug:msg= "The value of Var2 is {{var2}}"

The result of the operation is:

The private field is used to define whether the input value is echoed when the interaction is made, and the default private is Yes;default to define the default value for the variable.

Resources:
"Ansible Automation operation and maintenance technology and practice" section 4.2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.