【问题标题】:How to make Ansible display the host name before asking for the sudo password?如何让 Ansible 在询问 sudo 密码之前显示主机名?
【发布时间】:2020-04-28 00:48:12
【问题描述】:

我有一个 Ansible 手册来更新我基于 Debian 的服务器。出于简单和安全的原因,我不想使用保管库来保存密码,也不想将它们存储在可公开访问的配置文件中。所以我用

询问每个客户的密码
become: yes
become_method: sudo

现在,当 playbook 运行时,Ansible 做的第一件事似乎是询问 sudo 密码,但我不知道哪个服务器(密码不同)。 有没有办法让 Ansible 在询问密码之前打印当前主机名?

更新剧本是这样的:

---
- hosts:
    all
  gather_facts: no
  vars:
    verbose: false
    log_dir: "log/dist-upgrade/{{ inventory_hostname }}"
  pre_tasks:
    - block:
        - setup:
      rescue:
        - name: "Install required python-minimal package"
          raw: "apt-get update && apt-get install -y --force-yes python-apt python-minimal"
        - setup:
  tasks:
    - name: Update packages
      apt:
        update_cache: yes
        upgrade: dist
        autoremove: yes
      register: output

    - name: Check changes
      set_fact:
        updated: true
      when: not output.stdout | search("0 upgraded, 0 newly installed")

    - name: Display changes
      debug:
        msg: "{{ output.stdout_lines }}"
      when: verbose or updated is defined

    - block:
      - name: "Create log directory"
        file:
          path: "{{ log_dir }}"
          state: directory
        changed_when: false

      - name: "Write changes to logfile"
        copy:
          content: "{{ output.stdout }}"
          dest: "{{ log_dir }}/dist-upgrade_{{ ansible_date_time.iso8601 }}.log"
        changed_when: false

      when: updated is defined
      connection: local

(来源:http://www.panticz.de/Debian-Ubuntu-mass-dist-upgrade-with-Ansible

【问题讨论】:

    标签: ansible


    【解决方案1】:

    您的上述 become 配置不会让 ansible 要求您输入密码:它只是建议它与 sudo 方法一起使用 become (例如,如果您配置了正确的密钥,则无需任何密码即可工作)。

    如果您被要求输入密码,那是因为(这是一个猜测,但我相当有信心...)您在运行 ansible-playbook 时使用了 --ask-become-pass 选项。

    在这种情况下,仅在 playbook 操作开始时提示您一次,并且此默认密码将用于您连接到的所有服务器,除非您在清单中为特定主机/组定义了另一个服务器。

    如果您根据您的机器有不同的密码,您实际上没有其他选择:您需要在您的清单中声明这些密码(强烈建议使用 ansible-vault 加密)或使用其他一些将它们从外部应用程序中取出的机制(hashicorp vault、动态库存、cyberark...)

    【讨论】:

    • 是的,我使用 --ask-become-pass。这有点暗示。感谢您的回答,即使 Ansible 不支持这一点非常令人失望。
    猜你喜欢
    • 1970-01-01
    • 2018-10-17
    • 2020-12-20
    • 2023-03-08
    • 2016-09-22
    • 2014-03-19
    • 1970-01-01
    相关资源
    最近更新 更多