01.按密码方式使用
#安装后修改配置文件
# uncomment this to disable SSH key host checking
host_key_checking = False
# host key checking setting above.
[root@master ansible_offline]# egrep -v "^$|^#" /etc/ansible/hosts
[k8s]
192.168.221.134 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123'
192.168.221.136 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123'
#测试
[root@master ansible_offline]# vim /etc/ansible/ansible.cfg
[root@master ansible_offline]# ansible k8s -m command -a 'hostname'
192.168.221.134 | CHANGED | rc=0 >>
node01
192.168.221.136 | CHANGED | rc=0 >>
node02
02.按密钥方式使用
#注释掉/etc/ansible/hosts配置文件
[root@master ~]# 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:NK0HN17PpumJI0eafXt2y3gciIrbUlqPTwkR6c6mmxw root@master
The key's randomart image is:
+---[RSA 2048]----+
| .. |
| o. |
| =.+ . |
| . B.o o |
| S.o . = |
| Bo..= . |
| EB=+oo . .|
| .=Bo=+..=o.|
| =++o++=.+.|
+----[SHA256]-----+
[root@master ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.221.136
/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
root@192.168.221.136's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.221.136'"
and check to make sure that only the key(s) you wanted were added.
[root@master ~]# cd /data/
[root@master data]# mkdir ansible
[root@master data]# cd ansible
[root@master ansible]# vim hosts
[k8s]
192.168.221.134
192.168.221.136
[root@master ansible]# ansible k8s -i hosts -m command -a 'hostname'
192.168.221.134 | CHANGED | rc=0 >>
node01
192.168.221.136 | CHANGED | rc=0 >>
node02