Ansible 批量修改远程主机密码且本地记录

---
- hosts: '{{ host }}'
  user: root
  tasks:

  - name: 修改root密码
    register: rootpw_result
    shell: |
      root_pw=$(date +"%s%N"| sha256sum | base64 | head -c 24)
      echo "$root_pw" | passwd --stdin root 2>&1 >/dev/null
      echo "{{ ansible_ssh_host }} : $root_pw"
  #- debug: var=rootpw_result.stdout_lines
  #- debug: msg={{ rootpw_result.stdout_lines }}
  #密码存到本地文件中
  #- local_action: copy content="{{ rootpw_result.stdout_lines }}\n" dest=~/GIT/serverlist/{{ root_passwd }}
  - local_action: lineinfile state=present line={{ rootpw_result.stdout_lines }} dest=~/GIT/serverlist/{{ root_passwd }}

使用方法

ansible-playbook -i ~/GIT/serverlist/server_list_17225.txt change_root_passwd.yml -e "host=rackspace_172_25_test root_passwd=20220523_password.txt"
lookback
  • 本文由 发表于 2022年5月23日04:22:02
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: