我在ansible.cfg
# default user to use for playbooks if user is not specified
# (/usr/bin/ansible will use current user as default)
remote_user = ansible
但是我有一部剧本bootstrap.yaml
,其中我与root
而不是ansible
相连
---
- hosts: "{{ target }}"
become: no
gather_facts: false
remote_user: root
vars:
os_family: "{{ osfamily }}}"
roles:
- role: papanito.bootstrap
但是似乎remote_user: root
被忽略了,因为我总是收到连接错误,因为它使用用户ansible
而不是root
进行ssh连接
fatal: [node001]: UNREACHABLE! => {"changed": false,
"msg": "Failed to connect to the host via ssh:
ansible@node001: Permission denied (publickey,password).",
"unreachable": true}
我能找到的唯一解决方法是用-e ansible_user=root
调用剧本。但这并不方便,因为我想使用site.yaml
调用多个剧本,其中第一个剧本必须使用ansible_user
root
运行,而其他剧本必须使用{{1} }
ansible
有什么建议我会丢失或如何解决?
答案 0 :(得分:1)
Q:“远程用户:root被忽略”
A:剧本按预期运行
- hosts: test_01
gather_facts: false
become: no
remote_user: root
tasks:
- command: whoami
register: result
- debug:
var: result.stdout
给予
"result.stdout": "root"
但是,变量可以在清单中被覆盖。例如广告资源
$ cat hosts
all:
hosts:
test_01:
vars:
ansible_connection: ssh
ansible_user: admin
结果是
"result.stdout": "admin"
使用命令仔细检查库存
$ ansible-inventory --list
可能还需要仔细检查角色- role: papanito.bootstrap
答案 1 :(得分:-1)
尝试
使用“ remote_user:ansible”和其他“ become:yes”,“ become_user:root”,“ become_method:sudo或su”代替“ remote_user:root”