ansible使用1

最近开始研究ansible,如下都是练习笔记

实验

1 生成秘钥对

ansible 172.16.62.17

[root@ansible .ssh]# ssh-keygen 
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:
SHA256:LeOsHiClWeULaWMOFKtpKHvxl+4kDODjyRkGPuSedbw root@ansible
The key's randomart image is:
+---[RSA 2048]----+
|  o.  .          |
| . . +           |
|o.o O .          |
|B+ X.o . .       |
|=X*.oo. S .      |
|B.O*...+ o       |
|.B. +E= o        |
| .   = o         |
|     o=          |
+----[SHA256]-----+
[root@ansible .ssh]# 
[root@ansible .ssh]# ll
total 12
-rw-r--r-- 1 root root  389 Mar 20  2017 authorized_keys
-rw------- 1 root root 1679 Nov 15 14:09 id_rsa
-rw-r--r-- 1 root root  394 Nov 15 14:09 id_rsa.pub
-rw-r--r-- 1 root root    0 Nov 15 14:07 known_hosts

2.将公钥传到客户端上

linux 172.16.62.19

2.1 公钥传到172.16.62.19上

[root@ansible .ssh]# ssh-copy-id   172.16.62.19
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '172.16.62.19 (172.16.62.19)' can't be established.
ECDSA key fingerprint is SHA256:4P7L2A57Ko7O8wz0FNodmNH0pgiWs13Qrx0n4fMuG1Q.
ECDSA key fingerprint is MD5:92:db:be:95:0b:49:b7:a3:3a:d6:d3:8a:93:73:4c:dc.
Are you sure you want to continue connecting (yes/no)? yes
/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@172.16.62.19's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '172.16.62.19'"
and check to make sure that only the key(s) you wanted were added.


# 测试免密登录
不用输入密码登录成功

[root@ansible .ssh]# ssh 172.16.62.19
Welcome to Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-111-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Wed Nov 18 03:58:01 UTC 2020

  System load:  0.7                Users logged in:        0
  Usage of /:   16.7% of 43.25GB   IP address for ens160:  172.16.62.19
  Memory usage: 32%                IP address for docker0: 172.17.0.1
  Swap usage:   0%                 IP address for cni0:    10.10.1.1
  Processes:    162

 * Introducing self-healing high availability clustering for MicroK8s!
   Super simple, hardened and opinionated Kubernetes for production.

     https://microk8s.io/high-availability

 * Canonical Livepatch is available for installation.
   - Reduce system reboots and improve kernel security. Activate at:
     https://ubuntu.com/livepatch

81 packages can be updated.
0 updates are security updates.

New release '20.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


*** System restart required ***
Last login: Wed Aug 19 06:44:57 2020 from 172.16.62.10
root@kubeadm-master2:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:50:56:a0:e5:e8 brd ff:ff:ff:ff:ff:ff
    inet 172.16.62.19/24 brd 172.16.62.255 scope global ens160
       valid_lft forever preferred_lft forever


#查看公钥信息,自动写入authorized_keys中
root@kubeadm-master2:~/.ssh# more authorized_keys 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDYuBlVG1/h0v8re9hujVa7uteiVPp5sXGoTwWBQ2YWpb2kM1DdgsjGy937thkfpxtcKgTQyeHA3JDupUlb5MxYIlXS0oLwD7cKihzciPzFjP+qdEepUNzwwEOjZFZnGr+DjwSYo6s7Yw6L1jtin/
fn97LlU3iyRHfwPXWunNBpn7CF4b6IwoJ2R/jJ/w9Us07CNOjwY3CyObJv2CN/BRnThyYkpY5HLlvG89AlSAutvUVnq510fIak/7HaPytZiRjNeExBlFzprrm/iDogrct4d4dU+3YDEPDf8udVVDQfVG0rPGFj8dAtA7OUJN5OrKMQ2a5zJS7afY0i
8q3o6xaZ root@ansible

2.2.配置免密登录ESXi
  • 172.16.62.240

  • 直接拷贝ansible上的authorized_keys 文件到ESXi的etc/ssh/keys-root中

[root@ansible .ssh]# scp authorized_keys  root@172.16.62.240:/etc/ssh/keys-root
Password: 
authorized_keys                                                                                                                                         100%  394   375.4KB/s   00:00  



#测试免密登录
[root@ansible .ssh]# ssh 172.16.62.240
The time and date of this login have been sent to the system logs.

WARNING:
   All commands run on the ESXi shell are logged and may be included in
   support bundles. Do not provide passwords directly on the command line.
   Most tools can prompt for secrets or accept them from standard input.

VMware offers supported, powerful system administration tools.  Please
see www.vmware.com/go/sysadmintools for details.

The ESXi Shell can be disabled by an administrative user. See the
vSphere Security documentation for more information.
[root@esxi-001:~]

3. ansible 测试
#测试查看IP 

[root@ansible .ssh]# ansible esxi -m shell -a  ' esxcli network ip interface ipv4 get '
[WARNING]: No python interpreters found for host 172.16.62.241 (tried ['/usr/bin/python', 'python3.7', 'python3.6', 'python3.5', 'python2.7', 'python2.6', '/usr/libexec/platform-
python', '/usr/bin/python3', 'python'])

172.16.62.241 | CHANGED | rc=0 >>
Name  IPv4 Address   IPv4 Netmask   IPv4 Broadcast  Address Type  Gateway      DHCP DNS
----  -------------  -------------  --------------  ------------  -----------  --------
vmk0  172.16.62.241  255.255.255.0  172.16.62.255   DHCP          172.16.62.1      true

[WARNING]: No python interpreters found for host 172.16.62.240 (tried ['/usr/bin/python', 'python3.7', 'python3.6', 'python3.5', 'python2.7', 'python2.6', '/usr/libexec/platform-
python', '/usr/bin/python3', 'python'])

172.16.62.240 | CHANGED | rc=0 >>
Name  IPv4 Address   IPv4 Netmask   IPv4 Broadcast  Address Type  Gateway      DHCP DNS
----  -------------  -------------  --------------  ------------  -----------  --------
vmk0  172.16.62.240  255.255.255.0  172.16.62.255   DHCP          172.16.62.1      true

[root@ansible .ssh]#

#查看服务
[root@esxi-001:/etc/ssh/keys-root] /etc/init.d/xorg snmpd
Usage: xorg {start|stop|status|restart}
[root@esxi-001:/etc/ssh/keys-root] /etc/init.d/snmpd status
snmpd is not running
[root@esxi-001:/etc/ssh/keys-root] /etc/init.d/xorg status
Xorg is not running
[root@esxi-001:/etc/ssh/keys-root]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值