Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Ansible-- variable

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Variables in ansible mainly come from the following scenarios:

Inventory (host vars,group vars)

In playbook

Command Lin

Playbook produces results during the execution of task, which can be register and used as a variable for the following task

In roles

From the facts on nodes

Generally speaking, 1-4 is defined by users, while 5 and facts are mainly pulled from nodes by ansible. Of course, facts can also be defined by itself.

Variables in Inventory

/ etc/ansible/hosts

[salt]

Salt-master http_port=80 # # Host variable

10.240.162.112 ansible_connection=paramiko

[salt:vars] # # Group of variables

Mysql_port=3306

/ etc/ansible/host_vars/salt-master # # define the host variable in the file, and the file name and host name should be the same.

Lxc:lixc

/ etc/ansible/group_vars/salt # # Group variables defined in the file. The file name should be the same as the group name.

Lss:

-liss

-ansible

Variables in playbooks

-

-hosts: all

User: lixc

Vars:

Time: 120 # # variable

Port: 80

Keeplive: 100

Vars_files:

-/ vars/test.yml # # can also import variables from external files

Variables can be defined in playbooks, or they can be imported into external files

Variables on the command line

Command line, there are about three ways to pass variables to playbooks

The first way is relatively simple.

Cat command_vars.yml

-

-hosts: salt-master

Remote_user: '`uservar`' # # command line variable

Tasks:

-name: run this command and ignore the result

Shell: echo `echovar`

-debug: msg=' `result`.`stdout``

Ansible-playbook command_vars.yml-e "uservar=lixc echovar=hellomysql" # # pass in variable parameters

The second way is to pass variables in the format of json.

Ansible-playbook command_vars.yml-e'{"uservar": "lixc", "echovar": "hellomysql"}'

# # when the form of a variable is complex, it is more convenient to use the json format. Generally, you can use the first method directly.

Third, pass in the json file. The second and third methods are generally used in situations where the form of variables is more complex. However, it is generally estimated to be rarely used, because it is not very useful to pass variables on the command line, and the first way of passing variables on the command line is a little more common.

Cat test.json # # json file

Uservar: lixc

Echocar: hellomysql

Ansible-playbook command_vars.yml-e'@ test.json' # # add the json file with @

In the fourth way, the result of register task, as a variable, is used by the following task

-

-hosts: slat-master

Tasks:

-name: test

Command: ls / home

Register: result

-debug: msg=' `result`.`stdout``

The fifth way is defined in roles.

├── group_vars # # Global group variables

│ └── salt

├── hosts

├── roles

│ ├── mysql

│ │ ├── handlers

│ └── main.yml

│ │ ├── tasks

│ ├── configure.yml

│ └── main.yml

│ │ ├── templates

│ └── my.cnf

│ │ └── vars # # current role variable

│ │ └── main.yml

The sixth way is to get the grains in facts,facts and salt-stack from nodes, mainly a system information of nodes itself, bios information, network, hard disk and so on. However, facts is better than the information obtained by grains in salt-stack. A little more.

Ansible slat-master-m setup | wc-l # # View facts

two hundred and eighty four

Salt'* 'grains.items | wc-l # # View grains

sixty-five

Custom facts

There are many ways to customize facts. Students who have the ability can modify it directly, setup this module. However, let's not change the official things casually.

The second method, defined on nodes, is defined in / etc/ansible/fact.d/*fact by default.

Define the format of the file, about three, are ini format, json format, or executable file, but they must return the json format.

Lixc.fact

{"json": ["hello", "world"]} # # this file is in json format

Lss.fact

#! / usr/bin/env python # # this file is a python script and returns a dictionary in json format

Import json

Dic = {"ansible_addresses": ["10.240.161.139", "192.168.115.164"]}

Print json.dumps (dic)

Lxc.fact

[ini] # # ini format

Lixc=hello

Lss=world

Create a folder on node and transfer the test fact file to it. Modify the attribute of lss.fact file to the executable permission of other groups, otherwise it will not be executed later. If you try it with the-s parameter, you will not be able to execute it.

Ansible salt-master-a "mkdir-p / etc/ansible/fact.d"-s

For file in `ls `; do ansible salt-master-m copy-a "src=$ {file} dest=/etc/ansible/facts.d/"-done > / dev/null

Ansible salt-master-m file-a "dest/etc/ansible/facts.d/lss.fact mode=755" > / dev/null-s

# # modify the attributes of the python script above, which will be executed later

Ansible salt-master-m setup-a "filter=ansible_local"

The custom module, but this module, returns a result in facts format.

In the module of ansible, customize a folder, put your own module, and throw it to the custom module.

Mkdir / usr/share/ansible/custom

Cat chengge

#! / usr/bin/env python

Import json

Dic = {"ansible_facts": {'chengge':' Hello ansible','liss': "Hello salt=stack"}}

Print json.dumps {dic}

# # key of the dictionary should be ansible_facts, otherwise facts cannot be used in playbooks

Cp chengge / usr/share/ansible/custom

Ansible salt-master-m chengge

How to use the custom module:

In fact, we want to use the facts returned in the module, so obviously, before we use it, we must first execute the following custom module

Take a look at playbooks first.

-

-hosts: slat-master

Tasks:

-name: test custom module

Action: chengge # # execute the custom module first

-debug: msg=' `chengge` # # using facts

Take another look at the implementation result.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report