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 automatic operation and maintenance

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Different roles used in Ansible (consumer, Ansible toolset, action object)

Lnventory is the configuration file for Ansible to manage host information and is stored in / etc/ansible/hosts by default.

The Ansible toolset contains (lnventrol,Modules,Pluginx and APL)

Lnventrol: list of managed hosts

Modules: execute command module

APl: interface

Ansible: core execution tool

Direct experiment!

Three linux servers, including one Ansible server.

192.168.1.1 192.168.1.2 192.168.1.3

Take one as an example, turn off the firewall, selinux

The configuration on the server is as follows:

One, yum installs Ansible

Second, enter the configuration file and add the node ip

Vim / etc/ansible/hosts

Managed host ip

Third, create ssh interactive-free login (that is, direct login does not require a password)

(1) generate a key pair from the ansible host

(2) copy the public key to the two hosts to be managed (here is a demonstration)

Then enter: ssh 192.168.1.2 can be switched directly without the need for a password

(3) to check whether it is alive at all times, the order is as follows

The result of the execution is generally represented in three colors: red and green.

Red indicates that there is an exception in the execution process, indicating that the target has changed after execution, and green indicates that the execution is successful and no changes have been made to the target host.

(4) command module

The command module executes commands on the remote host and does not support shell features such as pipe characters and redirects.

Shows the users of the two hosts

(5) shell module

The shell module executes commands on the remote host, which differs from the command module in that it supports shell features, such as pipe characters, redirects, etc.

Web includes two hosts in the list (multiple hosts can be added in the list) and creates two HELLOW,txt files under the two hosts / tmp

(6) copy module

Used to copy files from the specified host to another host

Dest: indicates the location of the target file

Src: indicates the path to the source file

Create a directory on this host and copy it directly to / tmp on both nodes.

(7) hostname module

Hostname used to remotely manage the host

Changed the hostname to asd

(8) yum module

The host used to install the remote host management package must be configured with yum, which only provides remote commands, such as installing http, as follows

State= (present,latest,absent) (install, install latest, uninstall)

(9) service module

Use and manage services on remote hosts, turn on, turn off, view or boot automatically

(10) user module

User accounts used to manage remote hosts such as create user, uid number, user's group, user's additional group

Fourth, Playbook configuration file

Write scripts to manage remote hosts

Ansible scripts must be aligned

Modify the configuration file in advance: vim / etc/ansible/hosts

Standard

This is a syntax check, which means there are no errors.

This is the script to create the user, as follows:-

Hosts: web1

Remote_user: root

Tasks:name: adduser

User: name=user2 state=present

Tags:aaaname: addgroup

Group: name=root system=yes

Tags:bbbhosts: web2

Remote_user: root

Tasks:name: copy file to web

Copy: src=/etc/passwd dest=/home

Tags:ccc

...

Execute script

Fifth, trigger

Write a script to change the port number of the remote host and restart hosts: web1

Remote_user: root

Tasks:name: change port

Command: sed-I 's/Listen\ 80/Listen\ 8080 Universe g' / etc/httpd/conf/httpd.conf

Notify:restart httpd server

Handlers:name: restart httpd server

Service: name=httpd state=restarted

...

Six, role

Roles are generally stored in the / etc/ansible/roles/ directory, such as writing a create database and creating a table in the database

First create multiple directories to store

Then go to tasks in the mariadb folder to create a script

The content is: name: install mariadb

Yum: name=mariadb-server state=presentname: move config file

Shell: "[- e / etc/my.cnf] & & mv / etc/my.cnf / etc/my.cnf.bak" name: provide a new config file

Copy: src=my.cnf dest=/etc/my.cnfname: reload mariadb

Shell: systemctl restart mariadbname: create database testdb

Shell: mysql-u root-e "create database testdb;grant all on testdb.* to'test'@'192.168.8.%' identified by 'test123';flush privileges;"

Notify:restart mariadb

...

A full stop indicates-

Then go to the hanglers folder to write and script

The content is name: restart mariadb

Service: name=mariadb state=restarted

...

Then go to the files folder to copy the mysql configuration file

Finally, go to the ansible folder to create the script

Finally start the script

There is a testdb table in the mysql database on both hosts.

The experiment is completed

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

Servers

Wechat

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

12
Report