利用ansible修改機羣密碼

利用ansible修改多機密碼,並保存到文件中,其中用到了生成密碼的python腳本,也能夠本身寫。
python

---
#######注意須要取ip地址的網卡接口是否正確,不一樣的公司有所不一樣###
- hosts: "{{ hosts }}"
  vars:
    change_user: sunbo
    passwd_file_path: /tmp/password.txt
  tasks:
    - name: End symbol
      shell: echo "{{ ansible_eth0.ipv4.address }}"|awk -F. '{print $3"-"$4}'
      register: symbol
      when: ansible_os_family == "RedHat" and ansible_distribution_major_version|int < 7
    - name: End symbol
      shell: echo "{{ ansible_eno16777736.ipv4.address }}"|awk -F. '{print $3"-"$4}'
      register: symbol


    - name: generate password
      local_action: command /usr/bin/python /etc/ansible/playbooks/passwd/passwd.py
      register: password
    - debug: msg="{{ inventory_hostname }}" 
    - debug: var="password.stdout"
      when: ansible_os_family == "RedHat" and ansible_distribution_major_version|int >= 7


    - name: Encryption string
      local_action: command /usr/bin/python -c "import crypt; print crypt.crypt('''{{ password.stdout }}{{ symbol.stdout }}''')"
      register: passwd
    - name: change root password
      user: name={{ change_user }} password={{ passwd.stdout }} update_password=always

    - name: save the password
      local_action: lineinfile  dest="{{ passwd_file_path }}"  line="{{ inventory_hostname }}      {{ password.stdout }}{{ symbol.stdout }}" create=yes state=present

python密碼生成腳本git

#/usr/bin/python
#coding=utf8

#import tab
from random import choice
import string,re
passwd_seed = string.digits + string.ascii_lowercase + string.ascii_letters +string.punc
tuation

"""function to generate a passwd"""
def get_passwd(passwd_length=30): ####默認值
    passwd = []
    while len(passwd) < passwd_length:
        passwd.append(choice(passwd_seed))
        password=''.join(passwd)
    password=password.replace("'","\\\'")
    password=password.replace('"','\\\"')
    return password
if __name__ == "__main__":
    #content_length = "\033[5;42;3m'length of passwd:'\033[0m" ########密碼的長度
    #content_count = "\033[5;44;3m'count of passwd:'\033[0m"   #######生成密碼的個數
    #length = int(raw_input('passwd length:'))
   # count = int(raw_input())
    #if length == '':
    #        print get_passwd()
    print get_passwd()
    #else:
    #        print get_passwd(length
相關文章
相關標籤/搜索