介绍
Ansible 是一个强大的自动化工具,它简化了对远程服务器的管理。Ansible 的一个关键方面是能够建立与这些服务器的安全 SSH 连接。在本教程中,我们将探讨如何为 Ansible 连接设置默认 SSH 用户,从而确保你的服务器管理任务拥有流畅而高效的工作流程。
Ansible 是一个强大的自动化工具,它简化了对远程服务器的管理。Ansible 的一个关键方面是能够建立与这些服务器的安全 SSH 连接。在本教程中,我们将探讨如何为 Ansible 连接设置默认 SSH 用户,从而确保你的服务器管理任务拥有流畅而高效的工作流程。
在为 Ansible 配置 SSH 用户之前,我们需要确保 Ansible 已正确安装并在我们的系统上设置完毕。
让我们从安装 Ansible 开始。打开一个终端并运行以下命令:
sudo apt update
sudo apt install -y ansible
安装完成后,通过检查 Ansible 的版本来验证它是否已正确安装:
ansible --version
你应该看到类似于以下的输出:
ansible [core 2.12.x]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/labex/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
ansible collection location = /home/labex/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.10.x (main, Ubuntu, x86_64)
jinja version = 3.0.3
libyaml = True
接下来,让我们为我们的 Ansible 项目创建一个目录结构。这将帮助我们保持文件的组织性:
mkdir -p ~/project/ansible/inventory
cd ~/project/ansible
Ansible 使用清单文件来定义它管理的宿主机。让我们创建一个简单的清单文件:
echo "[webservers]
localhost ansible_connection=local" > ~/project/ansible/inventory/hosts
这个清单文件定义了一个名为 webservers
的组,其中只有一个宿主机 localhost
,并告诉 Ansible 对此宿主机使用本地连接而不是 SSH。
现在,让我们验证我们的清单:
ansible -i inventory/hosts --list-hosts all
你应该看到类似这样的输出:
hosts (1):
localhost
这表明 Ansible 识别了我们的清单以及其中定义的宿主机。
Ansible 主要使用 SSH 连接到远程宿主机并执行命令。让我们探讨 Ansible 如何建立 SSH 连接以及我们如何配置默认的 SSH 用户。
默认情况下,Ansible 尝试使用你当前的系统用户来建立 SSH 连接。这意味着如果你以 labex
用户的身份登录并运行 Ansible 命令,Ansible 将尝试以 labex
用户的身份连接到远程宿主机。
这种默认行为可能并不总是你想要的。例如:
Ansible 使用配置源的层次结构来确定使用哪个 SSH 用户:
让我们创建一个基本的 Ansible 配置文件来更好地理解这一点:
cat > ~/project/ansible/ansible.cfg << 'EOF'
[defaults]
inventory = ./inventory/hosts
host_key_checking = False
EOF
此配置文件告诉 Ansible:
让我们回顾一下我们刚刚创建的文件:
cat ~/project/ansible/ansible.cfg
你应该看到我们刚刚添加到文件的内容。
现在让我们运行一个基本的 Ansible 命令来查看当前用户:
cd ~/project/ansible
ansible localhost -m command -a "whoami"
输出应该显示:
localhost | CHANGED | rc=0 >>
labex
这确认了 Ansible 正在以当前用户 (labex
) 的身份执行命令。
现在我们了解了 Ansible 如何使用 SSH,让我们探索设置连接默认 SSH 用户的不同方法。
设置全局默认 SSH 用户最简单的方法是通过 Ansible 配置文件。让我们修改我们的 ansible.cfg
文件:
cat > ~/project/ansible/ansible.cfg << 'EOF'
[defaults]
inventory = ./inventory/hosts
host_key_checking = False
remote_user = ansible_user
EOF
在此配置中,我们添加了 remote_user
参数,该参数告诉 Ansible 将 ansible_user
用作所有连接的默认 SSH 用户。
另一种方法是在清单文件中定义 SSH 用户。这种方法允许你为不同的宿主机或组设置不同的用户。
让我们修改我们的清单文件:
cat > ~/project/ansible/inventory/hosts << 'EOF'
[webservers]
localhost ansible_connection=local
[dbservers]
db.example.com ansible_user=db_admin
[all:vars]
ansible_user=default_user
EOF
在这个例子中:
dbservers
的新组,其中包含一个宿主机 db.example.com
,并指定 Ansible 在连接到此宿主机时应使用 db_admin
用户。ansible_user=default_user
,该变量适用于所有宿主机,除非被覆盖。你还可以在运行 Ansible 命令时直接在命令行中指定 SSH 用户:
ansible localhost -m command -a "whoami" -u specific_user
-u
选项告诉 Ansible 对此特定命令使用 specific_user
,从而覆盖在配置文件或清单中定义的任何用户。
在使用 Ansible 剧本时,你可以在 play 级别指定 SSH 用户:
让我们创建一个简单的剧本来演示这一点:
cat > ~/project/ansible/user_demo.yml << 'EOF'
---
- name: Demonstrate user configuration
hosts: localhost
remote_user: playbook_user
tasks:
- name: Show current user
command: whoami
register: current_user
- name: Display current user
debug:
msg: "Current user is {{ current_user.stdout }}"
EOF
在此剧本中,remote_user
参数将 SSH 用户设置为 playbook_user
,用于此 play 中的所有任务。
了解这些不同方法的优先级非常重要:
-u
标志)具有最高优先级ansible.cfg
中的 remote_user
)这意味着更具体的用户设置会覆盖更通用的设置。
现在我们已经以不同的方式配置了默认的 SSH 用户,让我们测试并验证我们的配置。
首先,让我们运行一个简单的 Ansible 命令,看看我们的配置文件设置是否已应用:
cd ~/project/ansible
ansible localhost -m command -a "whoami"
由于我们使用 ansible_connection=local
连接到 localhost,因此无论 remote_user
设置如何,Ansible 仍将以当前用户身份运行命令。但是,如果这是一个远程连接,Ansible 将尝试使用我们在配置中指定的 ansible_user
用户。
Ansible 提供了一个名为 ansible-inventory
的命令,它允许我们查看 Ansible 如何解释我们的清单,包括 SSH 用户设置:
ansible-inventory --list
此命令输出清单的 JSON 表示形式,显示与每个宿主机关联的所有变量,包括 SSH 用户设置。
让我们运行我们在上一步中创建的剧本:
ansible-playbook user_demo.yml
输出将显示类似内容:
PLAY [Demonstrate user configuration] ******************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Show current user] *******************************************************
changed: [localhost]
TASK [Display current user] ****************************************************
ok: [localhost] => {
"msg": "Current user is labex"
}
PLAY RECAP *********************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
输出显示该命令仍然以 labex
用户身份执行,这是预期的,因为我们使用 ansible_connection=local
连接到 localhost。
让我们创建一个更实际的示例,演示如何为远程宿主机设置 SSH 用户配置。我们将创建一个新的剧本:
cat > ~/project/ansible/remote_user_example.yml << 'EOF'
---
- name: Example of remote user configuration
hosts: all
remote_user: admin_user
tasks:
- name: This task uses the play-level user (admin_user)
debug:
msg: "This would run as admin_user"
- name: This task uses a specific user
debug:
msg: "This would run as operator_user"
remote_user: operator_user
- name: This task uses become to elevate privileges
debug:
msg: "This would run as the root user"
become: true
become_user: root
EOF
在此剧本中:
remote_user: admin_user
,这默认适用于所有任务remote_user: operator_user
覆盖了此设置become: true
将权限提升到 root
用户此示例显示了你可以在 Ansible 中配置 SSH 用户的不同级别。
现在我们已经介绍了为 Ansible 设置默认 SSH 用户的基础知识,让我们探索一些高级技术和故障排除步骤。
连接到远程宿主机时,最佳实践是使用 SSH 密钥身份验证而不是密码。让我们看看如何在 Ansible 中配置 SSH 密钥身份验证:
cat > ~/project/ansible/ssh_key_example.yml << 'EOF'
---
- name: Example using SSH key authentication
hosts: all
remote_user: secure_user
vars:
ansible_ssh_private_key_file: ~/.ssh/id_rsa
tasks:
- name: Show SSH connection details
debug:
msg: "Connecting as {{ ansible_user }} using key {{ ansible_ssh_private_key_file }}"
EOF
在这个例子中:
remote_user: secure_user
设置为默认的 SSH 用户ansible_ssh_private_key_file
指定要使用的 SSH 私钥文件在实际场景中,你可能希望为不同的环境(开发、预发布、生产)使用不同的 SSH 用户。让我们看看如何实现这一点:
mkdir -p ~/project/ansible/group_vars
现在,让我们为不同的环境创建组变量文件:
cat > ~/project/ansible/group_vars/development << 'EOF'
---
ansible_user: dev_user
EOF
cat > ~/project/ansible/group_vars/production << 'EOF'
---
ansible_user: prod_user
ansible_ssh_private_key_file: ~/.ssh/prod_key
EOF
更新清单文件以包含这些环境组:
cat > ~/project/ansible/inventory/hosts << 'EOF'
[webservers]
localhost ansible_connection=local
[development]
dev.example.com
[production]
prod1.example.com
prod2.example.com
[all:vars]
ansible_user=default_user
EOF
使用此配置:
development
组中的宿主机将使用 dev_user
SSH 用户production
组中的宿主机将使用 prod_user
SSH 用户和特定的 SSH 密钥default_user
SSH 用户如果你在 Ansible 中遇到 SSH 用户配置问题,以下是一些故障排除步骤:
要查看 Ansible 如何解释你的清单,包括所有变量值:
ansible-inventory --list
使用增加的详细模式运行 Ansible 可以帮助识别连接问题:
ansible localhost -m ping -vvv
-vvv
标志增加了详细级别,显示有关 SSH 连接过程的详细信息。
你可以手动测试 SSH 连接,以验证用户和密钥是否有效:
ssh -i ~/.ssh/id_rsa username@hostname
常见的 SSH 连接错误包括:
让我们创建一个简单的剧本来测试我们的 SSH 用户配置:
cat > ~/project/ansible/test_ssh_config.yml << 'EOF'
---
- name: Test SSH user configuration
hosts: all
gather_facts: no
tasks:
- name: Display connection information
debug:
msg: |
Connected to: {{ inventory_hostname }}
User: {{ ansible_user | default('not set') }}
SSH Key: {{ ansible_ssh_private_key_file | default('not set') }}
EOF
运行此剧本以查看每个宿主机的连接信息:
ansible-playbook -i inventory/hosts test_ssh_config.yml
此剧本将向你显示 Ansible 正在为每个宿主机使用的 SSH 用户和密钥,这可以帮助识别配置问题。
在这个实验中,你已经学习了如何使用各种方法配置 Ansible 连接的默认 SSH 用户。你现在了解如何:
这些技能将使你能够使用 Ansible 更有效地管理你的基础设施,确保与远程服务器的安全可靠连接。通过正确配置 Ansible 连接的 SSH 用户,你可以在不同的环境中实施一致的自动化工作流程,同时保持适当的安全实践。