Hello, everyone. My name is Xie Wei. I'm a programmer.
Theme of this section: Ansible + Reclass
If you've been following me continuously, you should know that my job is to install and deploy the PaaS platform and to deploy components on multiple nodes, I'll use Ansible.
If you want to know the basics of Ansible, check out Ansible's official website.
Or:
Through the understanding of the official website, we know that if we deal with slightly complex actions, we will decompose the processing actions of the managed nodes into roles, each role completes a simple task. It is a way to decompose complex tasks into small problems that can be handled.
The problem now is that in general, we may define many node variables in the / etc/ansible/hosts file, such as the node's IP, the node's login username or password.
If the parameters of the nodes we need are dynamically generated, or the parameters of the nodes are very large.
This way of writing to death in the / etc/ansible/hosts file is neither safe nor friendly.
reclass is a library for organizing the information of nodes. The term is: node classifier.
The purpose of an ENC is to allow a system administrator to maintain an inventory of nodes to be managed, completely separately from the configuration of the automation tool.
The purpose of reclass is to allow system administrators to better manage the list of nodes, while completely independent of configuration files.
the external node classifier completely replaces the tool-specific inventory (such as site.pp for Puppet, ext_pillar/master_tops for Salt, or /etc/ansible/hosts).
So how to use reclass?
- Installation of reclass
- The concept of reclass
- The command line of reclass
- reclass configuration file
- reclass and Ansible
Let's look at it one by one.
Note: Ansible + reclass demo is under Linux system.
1. Installation of reclass
Because reclass is actually a python library, it's easy to install.
pip install reclass
Verification: Input reclass in terminal
reclass
Successful installation is indicated by a prompt.
2. The concept of reclass
reclass concept | introduce |
---|---|
node | Node (the host you need to operate on) |
class | A category, tag, feature, or role that applies to a node, node group |
application | Collection of actions (role playbook) |
parameter | Node parameter |
In these concepts, class may be a little difficult to understand.
3. reclass command line
After successful installation of reclass, enter reclass - help in the terminal.
reclass --help
Common commands:
reclass -i reclass --inventory reclass -i -o json
Others won't. Look at the help documentation when you need it.
In fact, the core command is this: reclass-i
The following is a demonstration.
4. Configuration files
reclass-config.yml
The reclass command defaults to find files in the / etc / reclass, / etc / reclass / nodes folder.
Configuration information is needed without default, mainly including these:
storage_type: yaml_fs pretty_print: True output: yaml inventory_base_uri: ./reclass
Focus on these two:
- output specifies the format of command echo, including two types: json, yaml
- The inventory_base_uri configuration file is the same level as the reclass file, which can be defined as reclass.
5. Reclass + Ansible
Directory structure:
reclass ├── classes │ ├── download.yml │ ├── op-cli │ │ └── init.yml │ └── update.yml └── nodes └── localhost.yml
Matters needing attention:
- reclass folder nouns are immutable
- classes folder name is immutable
- nodes folder name is immutable
- Other folders, arbitrary name, arbitrary hierarchy
Specific contents of each document:
# download.yml --- applications: - download parameters: ssh: info: name: ssh-name para: token server: "127.0.0.1"
# op-cli/init.yml --- applications: - op-cli parameters: op_cli: path: name: "/home/xiewei/xiewei" file: "/home/xiewei/xiewei.text"
# update.yml --- applications: - update parameters: info: name: Age: 18 LastName: wei firstName: xie
# localhost.yml --- classes: - update - download - op-cli parameters: ip: info: info: localhost net: path: xiewei
It can be seen that the basic definition of reclass classes, applications, parameters is:
The main definition under folder classes is a set of node parameters.
For example, above, we defined parameters for three groups: download, op-cli, update
The main parameters of the node are defined under the folder nodes.
For example, the ip of the node above is localhost, i.e. local. The group on the node includes download, op-cli, update, and some parameters of the node.
With configuration parameters configured, the current project directory structure is as follows:
├── reclass │ ├── classes │ │ ├── download.yml │ │ ├── op-cli │ │ │ └── init.yml │ │ └── update.yml │ └── nodes │ └── localhost.yml └── reclass-config.yml
Execute the command (configuration file level): reclass-i gets all the parameters.
__reclass__: timestamp: Sat May 26 00:30:57 2018 applications: download: - localhost op-cli: - localhost update: - localhost classes: download: - localhost op-cli: - localhost update: - localhost nodes: localhost: __reclass__: environment: base name: localhost node: ./localhost timestamp: Sat May 26 00:30:57 2018 uri: yaml_fs:///home/xiewei/golearn/src/gopher/Ansible-api/reclass/nodes/./localhost.yml applications: - update - download - op-cli classes: - update - download - op-cli environment: base parameters: info: name: Age: 18 LastName: wei firstName: xie ip: info: info: localhost net: null path: xiewei op_cli: path: file: /home/xiewei/xiewei.text name: /home/xiewei/xiewei ssh: info: name: ssh-name para: token server: 127.0.0.1
The parameters defined in the above file are organized by reclass.
6. reclass combines ansible
How can the two be combined?
├── ansible │ ├── hosts │ ├── playbook │ │ └── deploy.yml │ └── roles │ └── op-cli │ ├── tasks │ │ ├── deploy.yml │ │ └── main.yml │ └── vars │ └── main.yml ├── ansible.cfg ├── reclass │ ├── classes │ │ ├── download.yml │ │ ├── op-cli │ │ │ └── init.yml │ │ └── update.yml │ └── nodes │ └── localhost.yml └── reclass-config.yml
For demonstration purposes, only one role is written in ansible
There are several specific things that need to be done:
- Copy ansible.py To the ansible folder for hosts
- Write roles under roles such as op-cli
- Configure the ansible configuration file ansible.cfg (inventory and roles_path) in the same directory of ansible
Look at what the op-cli role does.
# tasks/deploy.yml --- - name: echo hello world shell: "echo hello world" - name: is path exist stat: path: "{{ op_cli_path_name }}" register: rt - name: debug debug: msg: rt.stat.exists - name: create path file: path: "{{ op_cli_create_path_file }}" state: touch mode: "u=rw,g=r,o=r" when: not rt.stat.exists
To determine whether a file exists or not, create a file if it does not exist.
# vars/main.yml --- op_cli_path_name: "{{ op_cli.path.name }}" op_cli_create_path_file : "{{ op_cli.path.file }}"
In vars, we usually store the parameters needed in the role task. In general, we organize all the parameters in the vars/main.yml file. But in combination with reclass, we can define the parameters as variables. For example, what is the value of the specific variable op_cli_path_name above?
As we mentioned above, relcass is used to classify nodes and organize them better. The parameters of each role in all roles are derived from the reclass model.
For example: reclass/classes/op-cli/init.yml
--- applications: - op-cli parameters: op_cli: path: name: "/home/xiewei/xiewei" file: "/home/xiewei/xiewei.text"
So: op_cli_path_name is op_cli.path.name equals "/ home/xiewei/xiewei"
This means that parameters can be defined in the reclass model.
Look at the final playbook
# ansible/playbook/deploy.yml - hosts: op-cli_hosts roles: - "op-cli"
How to know the hosts of the node?
- nodes/localhost.yml file name specifies localhost
- localhost.yml contains classes: op-cli, download, update
In fact, you can know the node information by running the following command:
ansible/hosts --list #The ansible.py above is renamed hosts and placed under the ansible folder
Result:
download: - localhost download_hosts: - localhost op-cli: - localhost op-cli_hosts: - localhost update: - localhost update_hosts: - localhost
Configure ansible.cfg.
[defaults] # some basic default values... inventory = ansible/hosts roles_path = ansible/roles
Run ansible-playbook
ansible-playbook ansible/playbook/deploy.yml
Result:
PLAY [op-cli_hosts] ******************************************************************************************************************************************************************** TASK [Gathering Facts] ***************************************************************************************************************************************************************** ok: [localhost] TASK [op-cli : echo hello world] ******************************************************************************************************************************************************* changed: [localhost] TASK [op-cli : is path exist] ********************************************************************************************************************************************************** ok: [localhost] TASK [op-cli : debug] ****************************************************************************************************************************************************************** ok: [localhost] => { "msg": "rt.stat.exists" } TASK [op-cli : create path] ************************************************************************************************************************************************************ changed: [localhost] PLAY RECAP ***************************************************************************************************************************************************************************** localhost : ok=5 changed=2 unreachable=0 failed=0
Namely: the home/xiewei/xiewei.text file is created
7. summary
- ansible is responsible for dividing the problem into multiple role s, each of which completes a system task
- reclass is responsible for the classification of node inventory, including the organization of parameters
- Some information about reclass-config.yml configuring reclass
- Some information about ansible.cfg configuring ansible
- Host queries node information
Note: Using Ansible and reclass under Linux
Note: Recass is seldom talked about on the whole network, not to mention reclass combined with ansible. If you usually do the operation and maintenance work, responsible for operating many servers, the above methods hope to inspire you.
Note: The project organization and configuration file paths need to be correct.
Note: The way to deal with complex systems is to automatically generate files under reclass/nodes and playbook combined with automation. With the help of hosts inventory and playbook, the task can be accomplished automatically.
If you have problems in using it, you can look at the following examples Ansible + Reclass