python Automation Ansible Installation Tutorial

  • 2021-06-28 13:02:04
  • OfStack

Installation of Ansible for python Automation is illustrated with examples.Share it for your reference, as follows:

1 Point of Fine

Ansible only needs to deploy the environment on the management side, which is recommended yum Source approach to deployment.

2 Install Ansible

Simply install on the primary server (host)


[root@localhost dev]# yum install ansible -y

3 Tests

1 Modify host configuration file/etc/ansible/hosts


## green.example.com
## blue.example.com
192.168.0.101
192.168.0.102
[webservers]
## alpha.example.org
## beta.example.org
192.168.0.101
192.168.0.102

2 Do the following

The connectivity of the host is tested by the ping module, and the ping operation is performed on a single host and a group, respectively. The results show that the installation and test are successful.


[root@localhost ansible]# ansible 192.168.0.101 -m ping -k
SSH password:
192.168.0.101 | SUCCESS => {
  "changed": false,
  "ping": "pong"
}
[root@localhost ansible]# ansible webservers -m ping -k
SSH password:
192.168.0.102 | FAILED! => {
  "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."
}
192.168.0.101 | SUCCESS => {
  "changed": false,
  "ping": "pong"
}

3 Description

Since the master and the controlled host do not have SSH certificate trust configured, the -k parameter needs to be added when the ansible command is executed, requiring the root (default) account password, which is entered when prompting "SSH password:".

4 Configure Linux host SSH passwordless access

1 Point of Fine

To avoid entering the target host password when issuing instructions from Ansible, it is a good scheme to achieve SSH no password through certificate signature. It is recommended to use ssh-keygen and ssh-copy-id for fast certificate generation and public key issuance, in which ssh-keygen generates a pair of keys and sshcopy-id generates a public key.

Step 1: Configure key authentication support with the target device.


[root@localhost home]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:9/pGNxnQVWAWpss7PYtJcUDyHsCexgYY6NGWy/oOhTg root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|   o.+ .o ..*++|
|  o = . .=.=. |
|  . + . + .=.  |
|  ...o  *o +. |
| E ... So. = .o |
|  ...  . ..=+ |
|  ..   .=.o. |
|   ..  o.+ o |
|   ..  .o+ . |
+----[SHA256]-----+

Private key files can be stored in the default path'~/.ssh/id_rsa".

Step 2: Next synchronize the public key file id_rsa.pub to target host, ssh-copy-id public key copy tool recommended


[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.0.102
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Kernel \r on an \m
root@192.168.0.102's password:
Number of key(s) added: 1
Now try logging into the machine, with:  "ssh 'root@192.168.0.102'"
and check to make sure that only the key(s) you wanted were added.

More information about Python can be found in this site's topics: Python Socket Programming Skills Summary, Python Data Structure and Algorithms Tutorial, Python Function Usage Skills Summary, Python String Operation Skills Summary, Python Introduction and Advanced Classic Tutorial, and Python Files and Directories Operation Skills Summary.

I hope that the description in this paper will be helpful to everyone's Python program design.


Related articles: