使用ansible批量修改操作系统管理员账号密码

一、ansible server端配置

1、对于Linux主机配置免密登录ssh-copy-id -i ~/.ssh/id_rsa.pub root@remote_ip

2、在/etc/ansible/hosts文件中添加相应主机IP

3、对于Windows主机需要在/etc/ansible/hosts文件中进行以下配置

复制代码
192.168.83.132 ansible_ssh_user=administrator ansible_ssh_pass=2008.Com ansibe_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore ansible_winrm_transport=ntlm

二、Windows客户端配置

1、确保.NET 4.0+和powershell 3.0+

Microsoft .NET Framework 4.5下载地址:https://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_setup.exe

更新PowerShell 2.0到3.0的脚本:https://github.com/ansible/ansible/blob/devel/examples/scripts/upgrade_to_ps3.ps1

2、查看并设置powershell执行策略为remotesigned:

3、查看powershell版本号,如果低于3.0请执行upgrade_to_ps3.ps1

4、执行ConfigureRemotingForAnsible.ps1(下载地址:https://github.com/ansible/ansible/blob/devel/examples/scripts/ConfigureRemotingForAnsible.ps1

5、防火墙开启5985、5986端口或者关闭防火墙

三、在ansible server执行修改管理密码的操作

1、修改Linux:

2、修改Windows:

四、脚本展示:

bash 复制代码
#pwd-linux.yml
---
- hosts: linux
  gather_facts: false
  tasks:
  - name: change password for root
user: name=root password={{ chpass | password_hash('sha512') }} update_password=always
bash 复制代码
#change-win-pwd.sh
#!/bin/bash
read -p "please input old password: " password1
read -p "please input new password: " password2
read -p "Retype new password: " password3

if [ $password2 = $password3 ]; then
    echo "net users administrator \"$password3\"" > pwd.bat
    ansible-playbook pwd-win.yml
    sed -i "s/$password1/$password3/g" /etc/ansible/hosts
    ansible-playbook file-win.yml
else
    echo "try again"
fi
bash 复制代码
#pwd-win.yml
---
- hosts: win
  tasks:
  - name: copy file
    win_copy: src=/root/pwd.bat dest=C:\\pwd.bat
  - name: change password
win_shell: C:\pwd.bat
bash 复制代码
#file-win.yml
---
- hosts: win
  tasks:
  - name: delete file
    win_shell: "del C:\\pwd.bat"
相关推荐
迷茫的大专生11 小时前
服务器部署与 MySQL 运维学习笔记:从 Ansible 到高可用与性能优化
mysql·nginx·ansible·mysql优化·keepalive
杜子不疼.3 天前
Ansible 批量部署 Node Exporter:从单机安装流程到远程主机统一执行
ai·ansible
运维全栈笔记4 天前
Ansible 自动化 Nginx 集群部署实战:动态 Inventory、滚动发布与 Role 工程化
nginx·自动化·ansible
User_芊芊君子9 天前
让异构增量同步飞起来-KFS全链路并行同步方案
运维·自动化·ansible
あ-11 天前
ansible-playbook安装seaweedfs三节点集群
ansible
Tassel_YUE13 天前
非 ansible 主机批量执行命令方法:psssh 和 pscp(随手记)
linux·网络·ssh·scp·ansible
亿朝亿夕22 天前
ansible安装
ansible
.柒宇.23 天前
运维常见面试题_07_配置管理与监控(Ansible · Prometheus · Alertmanager)· 容器编排(Kubernetes)
运维·k8s·ansible·prometheus
相顾若初见23 天前
Ansible部署k8s
容器·kubernetes·ansible
wzq11_66623 天前
ansible部署项目——基于tidb数据库安装zabbix_server
ansible·zabbix·tidb