Ansible is a radically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs. The good thing about Ansible is that he doesn’t need to install any software on the controlled server. The only thing needed is an ordinary ssh protocol.
Today I will show you one simplest usage, running commands in batches on multiple machines.
1. Install Ansible
The configuration of the controlled machine is at
[webserver]It is the group name of the server, and the name can be customized. Put the servers with the same characteristics in a group, which is convenient for batch operation later
22.214.171.124Server address, which can be a domain name
ansible_ssh_userThe user name used to log in, I use the root login for convenience
ansible_ssh_portssh port, if you are not using 22, you can specify this parameter. If it is 22, you can omit
ansible_ssh_private_key_fileIf you are using a key to log in, this parameter can specify the address of the key file
3. Allow Password Login
In general, for security reasons, Ansible is best to log in with a key, but if you usually use a password to manage the server, then you need to modify the configuration file and run a password to log in.
Originally this line was commented out, remove the preceding #.
4. Execute batch commands
Ok, the preparations are all done. Let’s execute the first command:
webserverIt is the group name of the server group defined in hosts above.
-m shellSpecify ansible to execute the shell module. There are many modules in ansible, and tutorials will be written in the future. Here we use the shell module to execute shell commands as an example.
-a "ping 126.96.36.199 -c 3"-a specifies the command to be executed later, here is ping 188.8.131.52 3 times
After the execution, it will immediately return the execution results of all servers. As shown below:
If you have many commands, you can write him as a script. For example
/home/1.shwrote the script in two lines
Now we use ansible’s script module to execute this script
allall means to execute all servers in the hosts file.
-m scriptInstructions for using ansible’s script module
-a "/home/1.sh"-a followed by the location of the script to be executed
The essence of this module is to transfer the script to the controlled server and execute it.
After execution, you can see the execution result:
Learned to execute commands and execute scripts, is it more convenient when managing multiple servers?
You can digest it carefully. More advanced usages will be explained to you later when you have time. If you have any questions, you can leave a message. I will try my best to answer.