Ansible Tutorial - Install Ansible and Run Simple Command Remotely - NETSEC


Learning, Sharing, Creating

Cybersecurity Memo

Thursday, November 5, 2020

Ansible Tutorial - Install Ansible and Run Simple Command Remotely

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


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  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.
vi /etc/ansible/ansible.cfg
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:
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 -c 3"   -a specifies the command to be executed later, here is ping 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
all   all means to execute all servers in the hosts file.
-m script  Instructions for using ansible's script module
-a "/home/"   -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.

YouTube Video:

No comments:

Post a Comment