`remote_user`在剧本和角色中被忽略

时间:2019-12-14 11:32:06

标签: ansible ansible-2.x

我在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

有什么建议我会丢失或如何解决?

2 个答案:

答案 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


笔记

答案 1 :(得分:-1)

尝试

使用“ remote_user:ansible”和其他“ become:yes”,“ become_user:root”,“ become_method:sudo或su”代替“ remote_user:root”