Actual combat--Playbook batch change password

Playbook introduction

Playbook is a mode different from using Ansible command line execution mode, and its functions are more powerful and flexible. Simply put, a playbook is a very simple configuration management and multi-host deployment system, which is different from any existing model and can be used as a basis for deploying complex applications. Playbook can be customized and configured, can be executed in an orderly manner according to the specified operation steps, and supports synchronous and asynchronous modes. It is worth noting that the playbook is described and defined in the YAML format.


1. The host names of the current two servers:

192.168.20.40

[root@docker02 ~]# hostname

docker02

192.168.20.39

[root@slavedb tmp]# hostname

slavedb


2. Ansible configuration

root@docker02 ~]# vim /etc/ansible/hosts

 [all]

k8s-master ansible_ssh_host=192.168.20.40

k8s-node3  ansible_ssh_host=192.168.20.39


3. Create a playbook script

[root@k8s-master ~]# more passwd.yml 

---

- hosts: all

  tasks:

  - name: change passwd

    user: name={{ item.user }} password={{ item.password | password_hash('sha512') }} update_password=always

    with_items:

        - { user: 'root', password: '123456' }


    register: result

  - debug: var=result

4. Run the playbook script

[root@k8s-master ~]# ansible-playbook  passwd.yml 

"item": {

                    "password": "123456", 

                    "user": "root"

                }, 

                "move_home": false, 

                "name": "root", 

                "password": "NOT_LOGGING_PASSWORD", 

                "shell": "/bin/bash", 

                "state": "present", 

                "uid": 0

            }

image

image

image

image.png

5. Log in to the server again, is there a prompt to update the password?

Connecting to 192.168.20.40:22...

Connection established.

To escape to local shell, press 'Ctrl+Alt+]'.


WARNING! The remote SSH server rejected X11 forwarding request.

Last failed login: Fri Jan 22 09:08:12 EST 2021 from 192.168.20.7 on ssh:notty

There were 2 failed login attempts since the last successful login.

Last login: Fri Jan 22 09:04:30 2021 from docker02

image.png

Related Reading:

1. Actual combat--Playbook batch deployment of zabbix-agent

2. Actual combat-Playbook batch change server hostname

3. Deploy Docker in batches on Playbook

4. Welcome to join the technical exchange


Welcome to join the QQ technology exchange group: 653256902, please share it in the circle of friends to let more attention, study and progress together!


image


Guess you like

Origin blog.51cto.com/15127516/2657646