These are builtin variables which cannot be set by the user and which Ansible will always override. Special variables in Ansible PlaybookĪnother category of variables are special variables. Here, the variables will be accessible to all the hosts of the group called web_servers. # vim /etc/ansible/group_vars/web_servers Remember that the name of the file should be the same as the group name. This should be in the /etc/ansible directory. The second section can be defined as a file in the group_vars directory as shown. The group variable file name should match the group of the hostsĬonsider the inventory file below If the host systems share the same values, create a group variable file in the group_vars directory. Instead of specifying the variable ntp_server in the inventory file, create 2 host variable files in the host_vars directory with the file name matching each of the hostnames of the host systems: # vim /etc/ansible/host_vars/webserver_1 The host variable file is usually stored in the host_vars directory which is usually specified in /etc/ansible/ path.Ĭonsider the inventory file below where we have 2 servers each using different ntp servers In a host variable file, the variable applier only to one host system in the inventory file. This is where the host and group variable files come in. Host and Group variablesĪlthough you can specify variables in the inventory file, standard practice discourages storing variables in the inventory file. This comes as a very convenient method of applying variables that are common to multiple host systems. The first section defines the remote hosts which are webserver_1 and webserver_1, while the second section groups together the common attributes between the two servers. In the YAML playbook file, this would be represented as shown web_servers: The inventory file will appear as shown: Let’s assume we have 2 web servers: webserver_1 and webserver_2 which are both listening to port 443 and whose ntp server is us. These are used to assign the attributes which are common to all the hosts. If you have different host systems that share similar attributes or values, you can define what we call group variables. To access the dictionary, simply add the task below tasks: Using our previous example, we can have dictionary lists below - hosts: all Msg: "" Variables with dictionariesįrom the Array list, you can further define each of the entries using a key value pair forming what we call a dictionary list. You can access the student names through indexing ( as you would in arrays in any other programming language) For example, to retrieve the name ‘Arthur’ from the array list use the syntax students as shown below. You can use arrays and assign them to variables as shown in the syntax below:įor example, the playbook below contains an array of 5 student names stored in a variable called students. When executed, the playbook prints the message ‘ Hello guys’ on the terminal. In the playbook above, the variable name is salutations and the value is Hello world! When the playbook is run, the value of the playbook is accessed by placing the variable between curly braces as shown above. Assigning a value to a variable in a playbook is quite easy and straightforward.īegin by calling the vars keyword then call the variable name followed by the value as shown. In playbooks, variables are handy in determining how tasks can be executed. Let’s now look at different use cases of Variables in Ansible Variables in PlaybooksĪs you know, A playbook is a collection of plays or tasks to be performed on a remote system. The names below do not qualify as valid variable names A combination of any two or all of the aboveĪ variable SHOULD ALWAYS START WITH A LETTER and SHOULD NOT CONTAIN ANY SPACES.Įxamples of acceptable variable names include:.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |