LoginSignup
1

More than 3 years have passed since last update.

適当にansibleでrootパスワードを変えようとしたらアクセスできなくなった件

Last updated at Posted at 2020-03-14

下記のような適当なtaskを書いたら、rootにログインできなくなり、
ほかのユーザを作成していなかった為、何もできなくなってしまいました。。。

ansible:2.9.3
ターゲットノードOS:CentOS7.7

root_passwd_change.yml
---
- hosts: test
  become: true
  tasks:
    - name: passwd change
      user:
        name: root
        password: newpassword

これを実行すると、

[root@ansible test]# ansible-playbook -i host root_passwd_change.yml

PLAY [test] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [192.168.1.136]

TASK [passwd change] ***********************************************************
[WARNING]: The input password appears not to have been hashed. The 'password'
argument must be encrypted for this module to work properly.

changed: [192.168.1.136]

PLAY RECAP *********************************************************************
192.168.1.136              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[WARNING]: The input password appears not to have been hashed. The 'password'
argument must be encrypted for this module to work properly.

で怒られますが、OKの記述がでて、変更できたように見えます。
ですが実際は
想定しているパスワードだとsshできなくなり、コンソールからのログインもできなくなります。
playbook再度実行しても変更できなくなるので、レスキューモードでパスワードの再設定などが必要になります。

[root@ansible test]# ansible-playbook -i host root_passwd_change.yml

PLAY [test] *********************************************************************

TASK [Gathering Facts] *********************************************************
fatal: [192.168.1.136]: UNREACHABLE! => {"changed": false, "msg": "Invalid/incorrect password: Permission denied, please try again.", "unreachable": true}

PLAY RECAP *********************************************************************
192.168.1.136              : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0

原因

[WARNING]: The input password appears not to have been hashed. The 'password'
argument must be encrypted for this module to work properly.

で怒られている通りパスワードがhashされないでべた書きされているからです。
以下のように書けば想定通りに変更できます。

root_passwd_change.yml
---
- hosts: test
  become: true
  tasks:
    - name: passwd change
      user:
        name: root
        password:  "{{ 'newpassword' | password_hash('sha512') }}"

下記ドキュメントに記載の通りにhash化して書いてあげないといけませんでした。
※Linuxの/etc/shadowはsha512形式の暗号化してあげないといけない

上記を実行すると、

[root@ansible test]# ansible-playbook -i host root_passwd_change_fixed.yml

PLAY [test] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [192.168.1.136]

TASK [passwd change] ***********************************************************
changed: [192.168.1.136]

PLAY RECAP *********************************************************************
192.168.1.136              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

想定していた通りに変更できました。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1