首页 > 其他分享 >ansible 修改密码

ansible 修改密码

时间:2024-04-14 11:12:09浏览次数:17  
标签:yunwei etc changed 192.168 修改 密码 ansible password

hosts

[centos-root]
192.168.174.129 ansible_ssh_port=22 
192.168.174.130 ansible_ssh_port=22  
192.168.174.131 ansible_ssh_port=22 

Ansible Vault 文件

创建 Ansible Vault 文件

# ansible-vault create passwords.yml
New Vault password:                    # 12345678
Confirm New Vault password:

编辑 Ansible Vault 文件

# ansible-vault edit passwords.yml
Vault password:

passwords.yml

192.168.174.129:
  old_yunwei_password: yunwei_129
  new_yunwei_password: yunwei*_129
192.168.174.130:
  old_yunwei_password: yunwei_130
  new_yunwei_password: yunwei*_130
192.168.174.131:
  old_yunwei_password: yunwei_131
  new_yunwei_password: yunwei*_131

playbook

change_password.yaml

- hosts: centos
  remote_user: yunwei
  gather_facts: no  # 禁用 Ansible 在执行任务之前从目标主机中收集信息
  become: yes
  become_method: sudo
  become_user: root
  vars_files:
    - passwords.yml
  vars:
    new_password: "{{ hosts_passwords[inventory_hostname].new_yunwei_password }}"
    ansible_ssh_pass: "{{ hosts_passwords[inventory_hostname].old_yunwei_password }}"
    ansible_become_pass: "{{ hosts_passwords[inventory_hostname].old_yunwei_password }}" 

  tasks:
    - name: chattr -i /etc/gshadow /etc/shadow /etc/group /etc/passwd /etc/ssh/sshd_config /etc/profile
      ansible.builtin.shell:
        cmd: |
          chattr -i /etc/gshadow /etc/shadow /etc/group /etc/passwd /etc/ssh/sshd_config /etc/profile
 
    - name: Change user password
      ansible.builtin.user:
        name: "{{ ansible_user }}"
        password: "{{ new_password | password_hash('sha512') }}"
      register: password_change_result

    - name: Print password change info
      debug:
        msg: "Password change info: {{ password_change_result }}"

    - name: chattr +i /etc/gshadow /etc/shadow /etc/group /etc/passwd /etc/ssh/sshd_config /etc/profile
      vars:
        ansible_ssh_pass: "{{ hosts_passwords[inventory_hostname].new_yunwei_password }}"
        ansible_become_pass: "{{ hosts_passwords[inventory_hostname].new_yunwei_password }}"    
      ansible.builtin.shell:
        cmd: |
          chattr +i /etc/gshadow /etc/shadow /etc/group /etc/passwd /etc/ssh/sshd_config /etc/profile

测试 playbook

# ansible-playbook -i hosts change_password.yaml --ask-vault-pass
Vault password: 

PLAY [centos] *****************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************
ok: [192.168.174.130]
ok: [192.168.174.131]
ok: [192.168.174.129]

TASK [chattr -i /etc/gshadow /etc/shadow /etc/group /etc/passwd /etc/ssh/sshd_config /etc/profile] ****************************************************************************
changed: [192.168.174.131]
changed: [192.168.174.129]
changed: [192.168.174.130]

TASK [Change user password] ***************************************************************************************************************************************************
changed: [192.168.174.130]
changed: [192.168.174.131]
changed: [192.168.174.129]

TASK [Print password change info] *********************************************************************************************************************************************
ok: [192.168.174.129] => {
    "msg": "Password change info: {u'comment': u'', u'shell': u'/bin/bash', u'group': 1002, u'name': u'yunwei', u'changed': True, 'failed': False, u'state': u'present', u'home': u'/home/yunwei', u'move_home': False, u'password': u'NOT_LOGGING_PASSWORD', u'append': False, u'uid': 1002}"
}
ok: [192.168.174.130] => {
    "msg": "Password change info: {u'comment': u'', u'shell': u'/bin/bash', u'group': 1002, u'name': u'yunwei', u'changed': True, 'failed': False, u'state': u'present', u'home': u'/home/yunwei', u'move_home': False, u'password': u'NOT_LOGGING_PASSWORD', u'append': False, u'uid': 1002}"
}
ok: [192.168.174.131] => {
    "msg": "Password change info: {u'comment': u'', u'shell': u'/bin/bash', u'group': 1000, u'name': u'yunwei', u'changed': True, 'failed': False, u'state': u'present', u'home': u'/home/yunwei', u'move_home': False, u'password': u'NOT_LOGGING_PASSWORD', u'append': False, u'uid': 1000}"
}

TASK [chattr +i /etc/gshadow /etc/shadow /etc/group /etc/passwd /etc/ssh/sshd_config /etc/profile] ****************************************************************************
changed: [192.168.174.130]
changed: [192.168.174.129]
changed: [192.168.174.131]

PLAY RECAP ********************************************************************************************************************************************************************
192.168.174.129            : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.174.130            : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.174.131            : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

标签:yunwei,etc,changed,192.168,修改,密码,ansible,password
From: https://www.cnblogs.com/wangguishe/p/18130278

相关文章

  • 读所罗门的密码笔记19_治理模式
    1. 解决方案1.1. 全球人工智能的环境错综复杂,它严重依赖于价值观,且关系重大1.2. 即使是与大家同仇敌忾的问题做斗争,也往往无法在国际社会中取得最佳效果1.3. OPCW(禁止化学武器组织)已经帮助限制了化学武器的开发和部署,但没有协议是百分百奏效的1.4. 如果《核不扩散条约》......
  • 实验一-密码引擎-3-加密API研究
    任务要求密码引擎API的主要标准和规范包括:1微软的CryptoAPI2RAS公司的PKCS#11标准3中国商用密码标准:GMT0016-2012智能密码钥匙密码应用接口规范,GMT0018-2012密码设备应用接口规范等研究以上API接口,总结他们的异同,并以龙脉GM3000Key为例,写出调用不同接口的代码,提交博客......
  • 主页修改前存档
    1.html:<!DOCTYPEhtml><htmllang="zh-CN"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>AI服务平台</title><l......
  • 密码引擎-加密API研究
    任务详细密码引擎API的主要标准和规范包括:1微软的CryptoAPI2RAS公司的PKCS#11标准3中国商用密码标准:GMT0016-2012智能密码钥匙密码应用接口规范,GMT0018-2012密码设备应用接口规范等研究以上API接口,总结他们的异同,并以龙脉GM3000Key为例,写出调用不同接口的代码,提交......
  • 实验一-密码引擎-3-加密API研究
    一、微软的CryptoAPI参考网站:https://learn.microsoft.com/zh-cn/windows/win32/seccrypto/cryptoapi-system-architecturehttps://developer.mozilla.org/zh-CN/docs/Web/API/Web_Crypto_APIhttps://www.w3.org/TR/2017/REC-WebCryptoAPI-20170126/https://blog.csdn.net/l......
  • 2022年4月树莓派系统初始用户名和密码
    最新版的树莓派系统(2022年4月发布的),没有了初始化的用户名和密码,以前用户名是pi,密码是raspberry,但是这次需要进系统之后才能设置。那如果手头没有屏幕无法操作的话,可以在往tf卡里刷好系统之后,新建一个文件,来设置初始话账号密码。文件名:userconf.txt文件内写入:pi:$6$/4.VdYgDm7......
  • pip 修改国内源
    参考https://zhuanlan.zhihu.com/p/681420064注意本文将pip源永久切换为清华源,测试可用。其他源未测试,请自主测试。环境环境版本说明WindowsWindows10专业版22H219045.4170VSCode1.88.1Python3.12pip3.12git命令客户端2.43.0.windows.......
  • 读所罗门的密码笔记18_大宪章
    1. 大宪章1.1. 1215年会议开启了一个艰难的谈判过程,充满了紧张和对权力与道德权威的争夺1.1.1. 这部宪章会赋予各方一系列的权力,对国王的自由裁量权进行制衡1.2. 《大宪章》还需要300多年的时间和多次迭代,才能成为财产权、公平税收、司法程序和政府最高法律的参考文件1.3......
  • ssh免密码登陆避免首次需要输入yes
    看下面,默认连接需要输入yes服务器多了之后一台一台去输入yes肯定也不现实。太麻烦了[root@ansible~]#ssh-keygen[root@ansible~]#ssh10.0.0.31[root@ansible~]#ssh-copy-id10.0.0.31/usr/bin/ssh-copy-id:INFO:Sourceofkey(s)tobeinstalled:"/root/.ssh/id_......
  • 2019年蓝桥杯省赛-修改数组(并查集)
    0.题目时间限制:1.0s内存限制:256.0MB本题总分:20分【问题描述】给定一个长度为N的数组A=[A1,A2,···AN],数组中有可能有重复出现的整数。现在小明要按以下方法将其修改为没有重复整数的数组。小明会依次修改A2,A3,···,AN。当修改Ai时,小明会检查......