如何在Ansible中为所有主机设置默认的ssh用户?


11

Ansible 2.1版

我有一个清单文件 hosts

[nodes]
host1
host2
...

和一个简单的剧本 site.yml

---
- hosts: all
  tasks:
    - include: tasks/main.yml

如果我刚开始玩,

ansible-playbook -i hosts site.yml -vvvv

我收到所有主机的错误消息,

ESTABLISH SSH CONNECTION FOR USER: None
fatal: [host1]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh.", "unreachable": true}
...

但是,在阅读Ansible库存文档后,我将其添加ansible_user到了hosts文件中,

[nodes]
host1    ansible_user=root
host2    ansible_user=root
...

这样可以解决SSH CONNECTION UNREACHABLE错误。但是,是否必须ansible_user=root在所有主机旁边添加?还是有更简单的方法来做到这一点?

Answers:


11

检查示例/默认ansible.cfg文件,您将在[defaults]以下位置找到它:

# default user to use for playbooks if user is not specified
# (/usr/bin/ansible will use current user as default)
#remote_user = root

取消注释remote_user并将用户设置为您要登录的身份。

Ansible到哪里去ansible.cfg?同一文件说明:

# nearly all parameters can be overridden in ansible-playbook 
# or with command line flags. ansible will read ANSIBLE_CONFIG,
# ansible.cfg in the current working directory, .ansible.cfg in
# the home directory or /etc/ansible/ansible.cfg, whichever it
# finds first

感谢@Deltik,您还能回答这个问题吗?
霍华德·李

@HowardLee:您的其他问题主要是基于意见的,不适合超级用户。
Deltik '16

4

另一种方法是用于--user定义远程ssh用户。输入ansible-playbook --help以阅读更多内容。这是我的典型命令:

ansible-playbook -i hosts site.yml --user <user> --ask-pass -vvvv

--ask-pass 将提示输入密码 --user


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.