SSH 提示密码过期,如何通过 ansible 批量更新线上服务器密码
起因
线上环境是在内网,登陆线上环境需要使用 VPN + 堡垒机 登陆,但是我日常登陆线上环境都是 VPN + 堡垒机 + Socks5常驻代理,在shell端只需要保存会话,会话使用socks5代理即可无缝ssh到线上服务器。
今天来发现无法登陆服务器,但是ssh *** -f -N ****@**** 建立后台socks的方式并不会把密码过期的问题暴露出来,经过多次尝试,手动登陆服务器才发现用于登陆服务器的密码已过期。
手动更新密码后问题恢复。
解决问题
由于这一批服务器都存在密码过期的问题,所以打算尝试使用 Ansible 来批量登陆服务器更新密码。
https://github.com/ansible/ansible/issues/1619 受到国外大神的解答,尝试进行处理。
使用 Ansible expect 模块来进行处理。
python 需要安装 pexpect 包。
pip install pexpect
正常手动操作的console返回信息如下:
You are required to change your password immediately (password aged)
Last login: Thu Aug 13 11:23:46 2020 from 1*.*.*7
WARNING: Your password has expired.
You must change your password now and login again!
Changing password for user sysadmin.
Changing password for sysadmin.
(current) UNIX password:
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
playbook task 内容:
- name: Set password if expired
delegate_to: 127.0.0.1
become: no
expect:
command: ssh {{ ansible_ssh_common_args }} {{ ansible_user }}@{{ inventory_hostname }}
timeout: 10
responses:
"password:":
# we couldn't keep the same old password
- "{{ ansible_ssh_pass_old }}"
- "{{ ansible_ssh_pass }}"
- "{{ ansible_ssh_pass }}"
# if succesfully login then quit
"\\~\\]\\$": exit
register: status
changed_when: "'authentication tokens updated successfully' in status.stdout"
按照大神的解答,进行尝试过后,发现行不通,提示如下:
You are required to change your password immediately (password aged)
Last login: Thu Aug 13 11:23:46 2020 from 1*.*.*7
WARNING: Your password has expired.
You must change your password now and login again!
Changing password for user sysadmin.
Changing password for sysadmin.
(current) UNIX password:
passwd: Authentication token manipulation error
认真理解了一番,发现,reponses里面少了一次键入密码的机会,我的环境是通过密码认证登录的 ,如果是使用秘钥登录,即ansible_ssh_common_args 应配置为秘钥方式,更正后如下:
- name: Set password if expired
delegate_to: 127.0.0.1
become: no
expect:
command: ssh {{ ansible_ssh_common_args }} {{ ansible_user }}@{{ inventory_hostname }}
timeout: 10
responses:
"(?i)password:":
# we couldn't keep the same old password
- "{{ ansible_ssh_pass_old }}"
- "{{ ansible_ssh_pass_old }}"
- "{{ ansible_ssh_pass }}"
- "{{ ansible_ssh_pass }}"
# if succesfully login then quit
"\\~\\]\\$": exit
register: status
changed_when: "'authentication tokens updated successfully' in status.stdout"
解释:
第一次键入 ansible_ssh_pass_old 为 command ssh 的登录密码。
第二次键入 ansible_ssh_pass_old 为 提示过期输入当前密码。
第三次键入 ansible_ssh_pass 为 新密码。
第四次键入 ansible_ssh_pass 为 确认新密码。
结果测试
ansible-playbook 功能测试结果:
STDOUT:
Warning: Permanently added 'm******o' (ECDSA) to the list of known hosts.
symin@m******o's password:
You are required to change your password immediately (password aged)
Last login: Fri Jul 31 10:46:38 2020 from 1*.*.*.*7
WARNING: Your password has expired.
You must change your password now and login again!
Changing password for user sysadmin.
Changing password for sysadmin.
(current) UNIX password:
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
Connection to m*****o closed.
大功告成。
SSH 提示密码过期,如何通过 ansible 批量更新线上服务器密码的更多相关文章
- Ansible 批量管理Windows Server服务器
Ansible批量管理Windows Server Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具, 它用Python写成,类似于saltstack和Puppe ...
- ansible批量管理服务 上
1 ansible简介 1.1 ansible批量管理服务概述 (1)是基于python语言开发的自动化软件工具(2)是基于SSH远程管理服务实现远程主机批量管理(3)并行管理,部署简单,应用也简单方 ...
- ansible 批量在远程主机上执行命令
ansible 和 saltstack 都是为了同时在多台主机上执行相同的命令, 但是 salt配置麻烦,ansible基本不用配置, ansible 通过ssh来连接并控制被控节点 1. 安装 第一 ...
- Ansible批量更新远程主机用户密码 (包括Ansible批量做ssh互信)
按照集团运维信息安全制度, 需要每个一段时间对线上服务器密码进行一次变更,通过shell脚本部署比较繁琐,所以决定采用ansible脚本对远程主机root密码进行批量重置,该脚本已经在稳定运行在正式环 ...
- mac下连接本地安装的mysql报错提示密码过期
前提: mac中之前安装了mysql,一段时间没使用,今天使用mysql客户端去连接,报错提示密码过期,原因是mysql5.7之后版本有密码过期这个功能. error: Your password h ...
- 如何修改oracle数据库用户密码过期策略
转至:https://www.cnblogs.com/zhangshuaihui/p/11451590.html 1. 查询数据库用户何时过期 登陆数据库PL/SQL工具,输入以下sql语句: s ...
- Python自动批量修改服务器密码
工作中,我们经常会定期更换服务器密码,如果手动去修改,不仅费时,而且容易出错.下面提供了一种思路,可以实现批量.自动修改服务器密码. 大致思路:首先,为每一台服务器设定一个唯一标识:其次,将每台服务器 ...
- Ansible 批量修改密码
客户要求每3个月修改一次主机密码.密码规则为客服提供的一串字符 xxxx + 主机后3位. 将需要登录主机添加到 Ansible. 将需要登录主机的公钥添加到 known_hosts ssh-keys ...
- Ansible playbook 批量修改服务器密码 先普通后root用户
fsckzy Ansible playbook 批量修改服务器密码 客户的需求:修改所有服务器密码,密码规则为Rfv5%+主机名后3位 背景:服务器有CentOS6.7,SuSE9.10.11,r ...
随机推荐
- Nginx TP5环境配置
Apache默认支持Pathinfo模式 Nginx不支持 需要手动配置 Apache默认支持Pathinfo模式 Nginx不支持 需要手动配置 server { #配置监听端口 list ...
- mysql中varchar类型和datetime类型字段进行比较
我是在mysql5.7版本进行比较 表a的字段order_no和表iwebshop_tmp的字段order_no一样 需要更新iwebshop_member_order表的datetime类型expi ...
- 网页兼容最新IE声明meta方法
第三种,总是使用最新版本文档模式. 以下是例子: <meta http-equiv="X-UA-Compatible" content="IE=edge" ...
- P3190-[HNOI2007]神奇游乐园【插头dp】
正题 题目链接:https://www.luogu.com.cn/problem/P3190 题目大意 \(n*m\)的网格上有权值,求一条权值和最大的不交回路. \(1\leq n\leq 100, ...
- "错误: 找不到或无法加载主类"解决办法
前言:记上次一个找了个把小时的问题(很烦这些配置) 原因: 从svn下checkOut的项目 在application的配置的输出class路径为main,而class文件路径是在项目名的根路径下,所 ...
- Python3入门系列之-----元组
元组 Python 的元组与列表类似,不同之处在于元组的元素不能修改 元组使用小括号,列表使用方括号 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可 实例 tup1 = (1,2,3,4, ...
- 4 个场景揭秘,如何低成本让容器化应用 Serverless 化?
作者 | changshuai FaaS 的门槛 Serverless 形态的云服务帮助开发者承担了大量复杂的扩缩容.运维.容量规划.云产品打通集成等责任,使得开发者可以专注业务逻辑.提高交付速度 ( ...
- HC(Histogram-based Contrast) 基于直方图对比度的显著性
HC(Histogram-based Contrast) 基于直方图对比度的显著性 来源于: 2011, Global contrast based salient region detection, ...
- 题解 GRE Words Revenge
题目传送门 题目大意 给出 \(m\) 次操作,分别为以下两种操作: 学习一个单词 给出一个段落,查询里面有多少个学过的单词.注意,如果学习过 \(\text{ab,bc}\) ,当前查询段落为 \( ...
- NOI2016区间bzoj4653(线段树,尺取法,区间离散化)
题目描述 在数轴上有 \(N\) 个闭区间 \([l_1,r_1],[l_2,r_2],...,[l_n,r_n]\) .现在要从中选出 \(M\) 个区间,使得这 \(M\) 个区间共同包含至少一个 ...