linux – 是否可以使用ansible在带内更改当前用户的密码?

我有一个新安装的 Linux发行版.它有一个普通用户,默认密码和锁定的root帐户(root上没有密码 – 不能直接SSH).我希望能够运行ansible playbook来配置此服务器,并通过更改其密码来锁定此默认帐户,而无需执行额外的带外步骤.

我的playbook所做的大部分设置都需要root,因此它被配置为ssh in作为默认用户,成为root,并运行所有命令.这很好.问题是改变密码.

如果我尝试使用用户模块更改用户的密码,则会成功,但之后的所有任务都会失败.即使我将更改作为我的playbook的最后一步,所有应该在最后运行的处理程序也会失败.

我在网上搜索到类似问题的2条建议:

>使用RSA密钥
>使用初始播放以某种方式测试,并更改密码

使用RSA密钥要求您提前设置RSA,因此不是带内.

我一直在尝试使用初始播放以某种方式执行此操作,但到目前为止,第二个初始播放无法使用默认密码登录,ansible退出并显示错误,并忽略其他任务,以及下一个播放.

还有一种方法可以使用ssh-pass来实现这一点,正如here所示,它应该可以工作,但这看起来更像是一个黑客,并且ssh-pass并不总是很容易获得,就像在运行ansible的MacOS机器上一样.

十多年前在here讨论了一个类似的主题,但看起来Dave通过设置Linux来显示“帐户已禁用”消息,当您尝试以默认用户身份登录并改为改变用户时,看起来像Dave它应该工作,但不同于简单地更改默认用户的密码.

有没有什么可以通过ansible来更改其运行的用户的密码,而不断开连接?

解决方法

Is there a way to make Ansible try the default password,and keep running the playbook if that fails?

这是一个例子,它与你提到的不完全匹配,但可能是一个起点.

---
# 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: {}

相关文章

linux常用进程通信方式包括管道(pipe)、有名管道(FIFO)、...
Linux性能观测工具按类别可分为系统级别和进程级别,系统级别...
本文详细介绍了curl命令基础和高级用法,包括跳过https的证书...
本文包含作者工作中常用到的一些命令,用于诊断网络、磁盘占满...
linux的平均负载表示运行态和就绪态及不可中断状态(正在io)的...
CPU上下文频繁切换会导致系统性能下降,切换分为进程切换、线...