Use of ansible
Introduction:
Ansible is designed for convenient and quick configuration management. With ansible, You can simplify and standardize complex configurations and make them easier to control. Ansible can achieve 100 and 1000 batch deployment.
Ansible features:
(1) easy to deploy. You only need to deploy the ansible environment on the master and the control end without any operations. (Ansible only needs to run on a common server, and does not need to install the client on the managed server)
(2) Use the SSH protocol to manage devices.
(3) It is easier to maintain the data written in Python.
Operation:
Install ansible on centos7
[[email protected] ~]# yum install ansible –yrpm -Uvh http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
# Image source. you can install the image source by pressing the image source before installing ansible. Here, the image source is directly installed.
After installation, ansible's default configuration file path is
[[Email protected] ~] # Ls/etc/ansible
Ansible. cfg hosts roles
- Password-free login is performed between ansible host and managed machine to facilitate management of managed machines
[[email protected] ~]# ssh-keygenGenerating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa): yes
[[Email protected] ~] # Cd/root /. SSH [[email protected]. SSH] # lsauthorized_keys id_rsa id_rsa.pub known_hosts [[email protected]. SSH] # ssh-copy-ID-I id_rsa.pub 111.231.144.197 # copy the generated public key to a remote machine.
4. simple use of ansible
[[email protected] ~]# cd /etc/ansible/[[email protected] ansible]# lsansible.cfg hosts roles[[email protected] ansible]# vim hosts
# You can configure groups in the default hosts. We can define various IP addresses and rules. Add the following content to hosts:
[Manage-Other]
111.231.144.197
127.0.0.1
Run the following command on the command line to view disk usage:
[[Email protected] ansible] # ansible manage-other-M shell-A 'df-H' # view the disk usage of machines in the Custom group in the hosts file [[email protected] ansible] # ansible all-M shell-A 'df-H' # view the disk usage of machines in all groups in the hosts file
Command: ansible group-M Module name-module A Parameters
(1) execute commands on a remote machine
[[email protected] ansible]# ansible manage-other -m shell -a uptime[[email protected] ansible]# ansible manage-other -m command -a uptime
(2) execute shell scripts on the remote host (array. Sh is on the machine where ansible is located) similar to SCP + shell commands.
[[email protected] SHELL]# ansible manage-other -m script -a array.sh
(3) copy files from the master to the target host (the host where ansible is located Copies files from the controlled remote host, similar to the SCP command)
[[email protected] SHELL]# ansible manage-other -m copy -a "src=array.sh dest=/root/array.sh"[[email protected] SHELL]# ll /root/array.sh -rw-r--r-- 1 root root 323 Oct 25 13:01 /root/array.sh
(4) The master node can copy files to the target host (the host where ansible is located Copies files to the controlled remote machine, similar to the SCP command) and modify the file permissions.
[[Email protected] shell] # ansible manage-other-M copy-a "src = array. sh DEST =/root/array. sh owner = root group = root mode = 777"
[[Email protected] shell] # ll/root/array. sh-rwxrwxrwx 1 Root 323 Oct 25 13:01/root/array. sh # permission change note: the remote host user exists and the group exists; otherwise, the copy fails.
(5) download the specified URL content to the remote host.
[[email protected] SHELL]# ansible manage-other -m get_url -a "url=http://www.baidu.com dest=/root/index.html"
Summary:
Command: shell command with remote execution permission.
Script: Execute the control script file on the remote host.
Shell: Execute the shell script file on the remote host on the control side.
Ansible O & M automation-configuration management tools