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
yum install ansible–y
sudo apt–getinstall software–properties–common
sudo apt–add–repository ppa:ansible/ansible
sudo apt–getinstall ansible
2. Configure the controlled machines
The configuration of the controlled machine is at /etc/ansible/hosts
[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
126.96.36.199 Server address, which can be a domain name
ansible_ssh_user The user name used to log in, I use the root login for convenience
ansible_ssh_port ssh port, if you are not using 22, you can specify this parameter. If it is 22, you can omit
ansible_ssh_pass Login password
ansible_ssh_private_key_file If 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:
ansible webserver–mshell–a“ping 188.8.131.52 -c 3”
webserver It is the group name of the server group defined in hosts above.
-m shell Specify 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 184.108.40.206 -c 3" -a specifies the command to be executed later, here is ping 220.127.116.11 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
I /home/1.shwrote the script in two lines
Now we use ansible’s script module to execute this script
ansible]# ansible all -m script -a “/home/1.sh”
all all means to execute all servers in the hosts file.
-m script Instructions 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.