Ansible-连接方法和细节
- 默认情况下,Ansible 使用本机 OpenSSH,因为它支持 ControlPersist(一项性能功能)、Kerberos 和 ~/.ssh/config 中的选项,例如 Jump Host 设置。,如果您的控制机器使用不支持 ControlPersist 的旧版本 OpenSSH,Ansible 将回退到名为“paramiko”的 OpenSSH Python 实现。
1. 设置远程用户
- 默认情况下,Ansible使用在控制节点上的用户名连接所有的远程设备,如果远程设备上不存在该用户名,可以设置一个不同的用户名用于连接。
---
- name: update webservers
hosts: webservers
remote_user: admin
tasks:
- name: thing to do first in this playbook
. . .
other1.example.com ansible_connection=ssh ansible_user=myuser
other2.example.com ansible_connection=ssh ansible_user=myotheruser
cloud:
hosts:
cloud1: my_backup.cloud.com
cloud2: my_backup2.cloud.com
vars:
ansible_user: admin
2. 设置ssh秘钥
- 默认情况下,Ansible假设使用SSH秘钥登录到远程机器,也可以通过
--ask-pass
选项输入密码登录,如果需要提权(sudo、 pbrun)可以使用--ask-become-pass
选项 - 使用ssh连接插件(默认设置)时,Ansible不会公开通道允许用户和ssh进程之间通过手动输入密码来解ssh秘钥,推荐使用ssh-agent
$ ssh-agent bash
$ ssh-add ~/.ssh/id_rsa
$ ssh-agent bash
$ ssh-add ~/.ssh/keypair.pem
3. 针对本地主机运行
$ ansible localhost -m ping -e 'ansible_python_interpreter="/usr/bin/env python"'
localhost ansible_connection=local ansible_python_interpreter="/usr/bin/env python"
4.检查主机秘钥
- Ansible 默认启用主机密钥检查。检查主机密钥可以防止服务器欺骗和中间人攻击,但它确实需要一些维护。
- 如果主机被重新安装并且在“known_hosts”中有不同的密钥,这将导致错误消息,直到更正。如果新主机不在“known_hosts”中,您的控制节点可能会提示确认密钥。
[defaults]
host_key_checking = False
$ export ANSIBLE_HOST_KEY_CHECKING=False