Linux

是否可以使用 ansible 在帶內更改目前使用者的密碼?

  • April 23, 2018

我有一個新安裝的 Linux 發行版。它有一個具有預設密碼的普通使用者和一個鎖定的 root 帳戶(root 上沒有密碼 - 不能直接 SSH)。我希望能夠執行 ansible playbook 來配置此伺服器,並通過更改其密碼來鎖定此預設帳戶,而無需執行額外的帶外步驟。

我的劇本所做的大部分設置都需要 root,因此它被配置為以預設使用者身份 ssh 進入,成為 root 並執行所有命令。這很好用。問題是更改密碼。

如果我嘗試使用使用者模組更改使用者的密碼,它會成功,但之後的所有任務都會失敗。即使我將更改作為劇本中的最後一步,所有應該在最後執行的處理程序都會失敗。

對於類似的問題,我在網上遇到了 2 條建議:

  • 使用 RSA 密鑰
  • 使用初始遊戲以某種方式測試並更改密碼

使用 RSA 密鑰需要您提前設置 RSA,因此不是帶內密鑰。

我一直在嘗試使用初始播放來以某種方式做到這一點,但到目前為止,第二次初始播放無法使用預設密碼登錄,ansible 退出並出現錯誤,並忽略其他任務,以及下一次播放。

還有一種方法可以使用 ssh-pass 來做到這一點,如此處所示它應該可以工作,但這似乎更像是一種 hack,並且 ssh-pass 並不總是很容易獲得,就像在執行 ansible 的 MacOS 機器上一樣。

十多年前在這裡討論了一個類似的話題,但看起來 Dave 通過將 Linux 設置為在您嘗試以預設使用者身份登錄時顯示“帳戶已禁用”消息並改為更改使用者來解決這個問題,這聽起來也像它應該可以工作,但與簡單地更改預設使用者的密碼不同。

ansible 可以做些什麼來更改它正在執行的使用者的密碼,而無需斷開連接?

有沒有辦法讓 Ansible 嘗試使用預設密碼,如果失敗則繼續執行 playbook?

這是一個範例,它與您提到的內容不完全匹配,但可能是一個起點。

---
# SYNOPSIS
# try authentication using keys, if that fails, fall back to default credentials

- import_playbook: ../bootstrap.yml

- hosts: linux_systems
 gather_facts: no
 become: yes
 any_errors_fatal: true
 vars: 
   ansible_user_first_run: vagrant
   ansible_pass_first_run: vagrant
 tasks:

 - block:
   - name: Check if connection is possible using keys
     command: ssh -F {{project_dir}}/.ssh/ansible_ssh_config -o User={{ ansible_user }} -o ConnectTimeout=10 -o PreferredAuthentications=publickey -o PubkeyAuthentication=yes {{ ansible_host }} /bin/true
     register: result
     connection: local
     ignore_errors: yes
     changed_when: False

   - name: If using user_first_run
     connection: local
     set_fact:
       using_first_run: true
     when: result is failed

   - name: If no connection, change ansible_user
     connection: local
     set_fact:
       ansible_user: "{{ ansible_user_first_run }}"
     when: result is failed

   - name: If no connection, change ansible_ssh_pass
     connection: local
     set_fact:
       ansible_ssh_pass: "{{ ansible_pass_first_run }}"
     when: result is failed

   - name: If no connection, change ansible_become_pass
     connection: local
     set_fact:
       ansible_become_pass: "{{ ansible_pass_first_run }}"
     when: result is failed

   # since any_errors_fatal this should fail the play
   # if we still cannot reach all hosts.
   - name: Check if connection is possible
     raw: /bin/true
     changed_when: False

   tags:
   - always

 - name: Perform a ansible ping
   ping: {}

引用自:https://serverfault.com/questions/909047