ansible
准备环境,连接xshell,安装yum。
安装ansible
1.安装epel
[root@localhost ~]# yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
2.安装python-jinja2-2.7.2-4.el7
在网址下载Python安装包:https://centos.pkgs.org/7/centos-x86_64/python-jinja2-2.7.2-4.el7.noarch.rpm.html
[root@localhost ~]# yum install -y python-jinja2-2.7.2-4.el7.noarch.rpm
3.安装ansible。
[root@localhost ~]# yum install -y ansible
4.检查安装版本。
[root@localhost ~]# ansible --version
ansible 2.9.1
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
生产公钥/私钥。
[root@localhost ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:
b4:81:b2:50:71:7f:37:7d:14:1d:e1:17:76:e0:9c:d2 root@localhost
The key's randomart image is:
+--[ RSA 2048]----+
| o.. .BB|
| . . o =oo+|
| . . . + . + E.o|
| . o . + . o ..|
| . S |
| |
| |
| |
| |
+-----------------+
默认生成在root家目录.ssh目录下。
[root@localhost ~]# ls -ad .ssh
.ssh
私钥设置,下次登录就不需要密码了。
[root@localhost ~]# ssh-copy-id -i root@192.168.1.145
/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
root@192.168.1.145's password:
Permission denied, please try again.
root@192.168.1.145's password:
Permission denied, please try again.
root@192.168.1.145's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.1.145'"
and check to make sure that only the key(s) you wanted were added.
验证登录不需要密码。
[root@localhost ~]# ssh 192.168.1.145
Last login: Sat Dec 28 14:16:25 2019 from 192.168.1.145
[root@bogon ~]# exit 退出
logout
Connection to 192.168.1.145 closed.
编辑配置文件。
[root@localhost ~]# vim /etc/ansible/hosts
ping自己的组名。
[root@localhost ~]# ansible -m ping "zx"
验证。
[root@localhost ~]# ansible zx -u root -a ls