【问题标题】:Ansible become not switching userAnsible 变成不切换用户
【发布时间】:2020-06-18 11:02:12
【问题描述】:

我正在尝试创建一个必须完成以下任务的剧本:

  • 从文件file中检索主机名和版本
  • 一一连接到这些主机名
  • 在每个主机中检索另一个文件 another_file 的内容,该文件将为我们提供环境(dev、qa、prod)

所以我的首要任务是检索我需要连接的主机的名称

- name: retrieve nodes
  shell: cat file | grep ";;" | grep foo | grep -v "bar" | sort | uniq
  register: nodes

- adding nodes:
  add_host:
     name: "{{ item }}"
     group: "servers"
  with_items: "{{ nodes.stdout_lines }}"

下一个任务连接到主机

- hosts: "{{ nodes }}"
  become: yes
  become_user: user1
  become_method: sudo
  gather_facts: no
  tasks:
    - name: check remote file
      slurp:
        src: /xyz/directory/another_file
      register: thing
    - debug: msg="{{ thing['content'] | b64decode }}"

但它不起作用,当我添加详细信息时,我仍然看到正在执行任务的用户正在运行 playbook (local_user)

<node> ESTABLISH SSH CONNECTION FOR USER: local_user

我做错了什么?

ansible 版本是 rhel 6.1 上的 2.7.1

更新

我也用过 remote_user: user1

- hosts: "{{ nodes }}"
  remote_user: user1
  gather_facts: no
  tasks:
    - name: check remote file
      slurp:
        src: /xyz/directory/another_file
      register: thing
    - debug: msg="{{ thing['content'] | b64decode }}"

到目前为止没有运气,同样的错误

【问题讨论】:

  • 你能从 cli 打开 ssh 连接吗? ssh user1@host?这看起来像是 SSH 问题。
  • 是的,ssh user1@host 无需密码也能正常工作
  • 你能添加你的剧本的输出吗

标签: ansible


【解决方案1】:

你需要设置remote_user,它是控制连接服务器时使用什么用户名的那个。只有建立连接后,才会使用become_user

https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html#hosts-and-users

【讨论】:

  • 最好在答案中包含链接中的相关信息。链接可能会过期,这意味着未来的读者可能无法访问这些信息
  • 我也试过了,但仍然在使用 local_user 运行,将其设置为角色和任务,仍然选择 local_user :(
【解决方案2】:

如果你使用become_user,它就像在远程机器上使用sudo command。 您想以特定用户的身份连接到您的机器。并在建立连接后发出带有sudo 的命令。 This question 有一些应该对您有所帮助的答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多