Info Security Memo
  • Blog
  • Sitemap
    • Categories
  • Contact
  • About
  • Resources
  • Tools
  • 51sec.org

Build Confidence

Focusing on Information Security 

Info Security Notes

Ansible Tutorial - Install Ansible and Run Simple Command Remotely

11/5/2020

0 Comments

 
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


Centos

1
yum install ansible -y
Ubuntu

1
2
3
4
5
sudo apt-get update
sudo apt-get install software-properties-common
sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update
sudo apt-get install ansible


2. Configure the controlled machines


The configuration of the controlled machine is at /etc/ansible/hosts

1
2
3
4
[webserver]
20.189.72.94 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=51sec
100.248.77.45 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=51sec

[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
20.189.72.94  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

1
host_key_checking = False
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:

1
ansible webserver -m shell -a "ping 1.1.1.1 -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 1.1.1.1 -c 3"   -a specifies the command to be executed later, here is ping 1.1.1.1 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

1
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.


References


https://www.91yun.co/archives/42901











via Blogger https://ift.tt/3mX4Pca
November 05, 2020 at 01:33PM Network
0 Comments



Leave a Reply.

    Categories

    All
    Architecture
    Blog
    Checkpoint
    Cisco
    Cloud
    CyberArk
    F5
    Fortigate
    Guardium
    Juniper
    Linux
    Network
    Others
    Palo Alto
    Qualys
    Raspberry Pi
    Security
    SIEM
    Software
    Vmware
    VPN
    Wireless

    Archives

    March 2024
    February 2024
    January 2024
    December 2023
    November 2023
    October 2023
    September 2023
    August 2023
    July 2023
    June 2023
    May 2023
    April 2023
    March 2023
    February 2023
    January 2023
    December 2022
    November 2022
    October 2022
    September 2022
    August 2022
    July 2022
    June 2022
    May 2022
    April 2022
    March 2022
    February 2022
    January 2022
    December 2021
    November 2021
    October 2021
    September 2021
    August 2021
    July 2021
    June 2021
    May 2021
    April 2021
    March 2021
    February 2021
    January 2021
    December 2020
    November 2020
    October 2020
    September 2020
    August 2020
    July 2020
    October 2019
    September 2019
    June 2019
    July 2018
    May 2018
    December 2017
    August 2017
    April 2017
    March 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015
    September 2015
    August 2015
    July 2015
    June 2015
    May 2015
    April 2015
    March 2015

    Print Page:

    RSS Feed

    Email Subscribe
Powered by Create your own unique website with customizable templates.
  • Blog
  • Sitemap
    • Categories
  • Contact
  • About
  • Resources
  • Tools
  • 51sec.org