You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Ansible Playbook创建挂载目录任务后修改主机密码?

解决方案

完全不用大改现有配置,直接在Playbook的「Create mount directory」任务后面加一个修改密码的任务就行——毕竟这时候Ansible还能用原密码welcome1连上主机执行操作。

修改后的完整Playbook如下:

---
  - name: Image Server(s)
    hosts: lites
    gather_facts: no
    tasks:
    - name: Wait for 5 minutes
      pause:
        minutes: 5

    - name: Create mount directory
      file:
        path: "{{mountpt}}"
        state: directory

    - name: 修改root密码为welcome2
      user:
        name: root
        password: "{{ 'welcome2' | password_hash('sha512') }}"
        update_password: always

关键说明:

  • 用Ansible自带的user模块修改密码,目标是root用户
  • password_hash('sha512')是把明文密码welcome2转换成Linux系统认可的哈希格式,这是系统存储用户密码的标准方式
  • update_password: always保证不管当前密码是什么,都会执行更新操作(如果想只在密码变化时更新,也可以改成on_create,但这里明确要改密码,用always更稳妥)

后续注意:

密码改完之后,要是之后还要用这个Inventory连接主机,得把里面的ansible_ssh_pass从welcome1改成welcome2,不然会连不上。

内容的提问来源于stack exchange,提问作者Akash-BLR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 18:59:59