按照集團運維信息安全制度, 須要每一個一段時間對線上服務器密碼進行一次變動,經過shell腳本部署比較繁瑣,因此決定採用ansible腳本對遠程主機root密碼進行批量重置,該腳本已經在穩定運行在正式環境下。具體方法以下:html
1) 在服務端安裝ansiblenode
[root@ansible-server ~]# yum install -y ansible
2) 配置ansible到遠程主機的ssh無密碼信任關係 (authoried_keys
模塊)shell
批量實現多臺服務器之間ssh無密碼登陸的相互信任關係, 能夠參考以前的文章: https://www.cnblogs.com/kevingrace/p/9063745.html 這裏採用Ansible 實現批量創建互信, 方法以下: 首先要生成ansible服務端本機ssh的key [root@ansible-server ~]# ssh-keygen -t rsa //一路回車 [root@ansible-server ~]# ls /root/.ssh/ id_rsa id_rsa.pub ==================================================== 須要注意ssh創建互信的命令格式: # ssh-copy-id -i ~/.ssh/id_rsa.pub username@[ip,hostname] ==================================================== 在客戶機比較多的狀況下,使用 ssh-copy-id命令的方法顯然是有些費時,使用ansible-playbook 推送 ymal進行批量建立ssh互信關係就顯得省事多了, 這裏就使用到了ansible的authoried_keys 模塊: 首先要配置ansible清單 (遠程主機的密碼這裏爲"123456") [root@ansible-server ~]# vim /etc/ansible/hosts ................ ................ [ssh-host] 172.16.60.204 172.16.60.205 172.16.60.206 172.16.60.207 [ssh-host:vars] ansible_ssh_pass="123456" ========================================================== 發送公鑰到目標機器命令格式以下: # ansible ssh-host -m copy -a "src=/root/.ssh/id_rsa.pub dest=/root/.ssh/authorized_keys mode=600" ========================================================== 編寫playbook文件 [root@ansible-server ~]# vim /opt/ssh_key.yaml --- - hosts: ssh-host user: root tasks: - name: ssh-copy authorized_key: user=root key="{{ lookup('file', '/root/.ssh/id_rsa.pub') }}" 注意上面yaml腳本中的"ssh-key-host"是在/etc/ansible/hosts清單文件裏配置的遠程客戶機列表 這裏作的是基於遠程主機root用戶的ssh互信 執行批量互信 [root@ansible-server ~]# ansible-playbook /opt/ssh_key.yaml PLAY [ssh-host] ************************************************************************************************************************ TASK [Gathering Facts] ***************************************************************************************************************** ok: [172.16.60.204] ok: [172.16.60.205] ok: [172.16.60.206] ok: [172.16.60.207] TASK [ssh-copy] ************************************************************************************************************************ changed: [172.16.60.205] changed: [172.16.60.204] changed: [172.16.60.206] changed: [172.16.60.207] PLAY RECAP ***************************************************************************************************************************** 172.16.60.204 : ok=2 changed=1 unreachable=0 failed=0 172.16.60.205 : ok=2 changed=1 unreachable=0 failed=0 172.16.60.206 : ok=2 changed=1 unreachable=0 failed=0 172.16.60.207 : ok=2 changed=1 unreachable=0 failed=0 最後驗證下ssh互信 [root@ansible-server ~]# ansible -i /etc/ansible/hosts ssh-host -m shell -a "whoami" 172.16.60.204 | SUCCESS | rc=0 >> root 172.16.60.205 | SUCCESS | rc=0 >> root 172.16.60.207 | SUCCESS | rc=0 >> root 172.16.60.206 | SUCCESS | rc=0 >> root 至此, ansible批量建立到遠程客戶機的ssh信任關係已經實現了!
3) Ansible批量更新遠程主機用戶密碼方法vim
方法一: 使用Ansible的user模塊批量修改遠程客戶機的用戶密碼安全
因爲在使用ansible修改用戶密碼的時候不能使用明文的方式,須要先加密,因此就須要使用一個方法對輸入的明文的密碼進行加密. 廢話很少說了. 下面直接記錄下操做方法: [root@ansible-server ~]# vim /opt/root_passwd.yaml --- - hosts: ssh-host gather_facts: false tasks: - name: change user passwd user: name={{ item.name }} password={{ item.chpass | password_hash('sha512') }} update_password=always with_items: - { name: 'root', chpass: 'kevin@123' } - { name: 'app', chpass: 'bjop123' } 注意上面在yaml文件中修改了遠程客戶機的root用戶密碼, app用戶密碼. 若是還想要修改其餘用戶密碼, 則繼續按照上面規則添加便可! 執行ansible-play [root@ansible-server ~]# ansible-playbook /opt/root_passwd.yaml PLAY [ssh-host] ************************************************************************************************************************ TASK [change user passwd] ************************************************************************************************************** changed: [172.16.60.204] => (item={u'chpass': u'kevin@123', u'name': u'root'}) changed: [172.16.60.205] => (item={u'chpass': u'kevin@123', u'name': u'root'}) changed: [172.16.60.204] => (item={u'chpass': u'bjop123', u'name': u'app'}) changed: [172.16.60.205] => (item={u'chpass': u'bjop123', u'name': u'app'}) changed: [172.16.60.206] => (item={u'chpass': u'kevin@123', u'name': u'root'}) changed: [172.16.60.206] => (item={u'chpass': u'bjop123', u'name': u'app'}) changed: [172.16.60.207] => (item={u'chpass': u'kevin@123', u'name': u'root'}) changed: [172.16.60.207] => (item={u'chpass': u'bjop123', u'name': u'app'}) PLAY RECAP ***************************************************************************************************************************** 172.16.60.204 : ok=1 changed=1 unreachable=0 failed=0 172.16.60.205 : ok=1 changed=1 unreachable=0 failed=0 172.16.60.206 : ok=1 changed=1 unreachable=0 failed=0 172.16.60.207 : ok=1 changed=1 unreachable=0 failed=0
方法二: 修改遠程主機的單個用戶密碼使用此方法比較方便bash
編寫playbook文件 [root@ansible-server ~]# vim /opt/root_passwd2.yaml --- - hosts: ssh-host gather_facts: false tasks: - name: Change password user: name={{ name1 }} password={{ chpass | password_hash('sha512') }} update_password=always 執行ansible-playbook, 使用-e參數傳遞用戶名和密碼給劇本,其中root爲用戶名,admin#123就是修改後的root密碼 [root@ansible-server ~]# ansible-playbook /opt/root_passwd2.yaml -e "name1=root chpass=admin#123" PLAY [ssh-host] ************************************************************************************************************************ TASK [Change password] ***************************************************************************************************************** changed: [172.16.60.204] changed: [172.16.60.205] changed: [172.16.60.206] changed: [172.16.60.207] PLAY RECAP ***************************************************************************************************************************** 172.16.60.204 : ok=1 changed=1 unreachable=0 failed=0 172.16.60.205 : ok=1 changed=1 unreachable=0 failed=0 172.16.60.206 : ok=1 changed=1 unreachable=0 failed=0 172.16.60.207 : ok=1 changed=1 unreachable=0 failed=0
方法三: 使用以下Ansible腳本, 適用於修改清單中部分遠程主機的用戶密碼服務器
編寫ansible-playbook腳本 (須要注意下面腳本中"ens192"是客戶機ip所在的網卡設備名稱, 這個要根據本身實際環境去配置, 好比eth0, eth1等) [root@ansible-server ~]# cat /opt/root_passwd4.yaml - hosts: test-host remote_user: root tasks: - name: change password for root shell: echo '{{ item.password }}' |passwd --stdin root when: ansible_ens192.ipv4.address == '{{ item.ip }}' with_items: - { ip: "172.16.60.220", password: 'haha@123' } - { ip: "172.16.60.221", password: 'kevin@123' } - { ip: "172.16.60.222", password: 'bobo@123' } 執行ansible-playbook: [root@ansible-server ansible]# ansible-playbook /opt/root_passwd3.yaml PLAY [ssh-host] ************************************************************************************************************************ TASK [Gathering Facts] ***************************************************************************************************************** ok: [172.16.60.204] ok: [172.16.60.205] ok: [172.16.60.206] ok: [172.16.60.207] TASK [change password for root] ******************************************************************************************************** [WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: ansible_eth0.ipv4.address == '{{ item.ip }}' [WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: ansible_eth0.ipv4.address == '{{ item.ip }}' skipping: [172.16.60.205] => (item={u'ip': u'172.16.60.204', u'password': u'haha@123'}) [WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: ansible_eth0.ipv4.address == '{{ item.ip }}' skipping: [172.16.60.206] => (item={u'ip': u'172.16.60.204', u'password': u'haha@123'}) skipping: [172.16.60.206] => (item={u'ip': u'172.16.60.205', u'password': u'kevin@123'}) [WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: ansible_eth0.ipv4.address == '{{ item.ip }}' skipping: [172.16.60.207] => (item={u'ip': u'172.16.60.204', u'password': u'haha@123'}) skipping: [172.16.60.207] => (item={u'ip': u'172.16.60.205', u'password': u'kevin@123'}) skipping: [172.16.60.207] => (item={u'ip': u'172.16.60.206', u'password': u'bobo@123'}) changed: [172.16.60.205] => (item={u'ip': u'172.16.60.205', u'password': u'kevin@123'}) skipping: [172.16.60.205] => (item={u'ip': u'172.16.60.206', u'password': u'bobo@123'}) changed: [172.16.60.204] => (item={u'ip': u'172.16.60.204', u'password': u'haha@123'}) skipping: [172.16.60.204] => (item={u'ip': u'172.16.60.205', u'password': u'kevin@123'}) skipping: [172.16.60.204] => (item={u'ip': u'172.16.60.206', u'password': u'bobo@123'}) changed: [172.16.60.206] => (item={u'ip': u'172.16.60.206', u'password': u'bobo@123'}) PLAY RECAP ***************************************************************************************************************************** 172.16.60.204 : ok=2 changed=1 unreachable=0 failed=0 172.16.60.205 : ok=2 changed=1 unreachable=0 failed=0 172.16.60.206 : ok=2 changed=1 unreachable=0 failed=0 172.16.60.207 : ok=1 changed=0 unreachable=0 failed=0
若是ansible服務端沒有和遠程主機作ssh信任關係, 則能夠在hosts清單配置裏直接指明用戶名和密碼. 若是使用普通用戶, 而且容許sudo, 則須要提早在客戶機裏的/etc/sudoers文件裏配置好該普通用戶的sudo配置, 即容許該普通用戶有sudo權限. [root@ansible-server ~]# vim /etc/ansible/hosts ................ [test-host] 172.16.60.220 ansible_ssh_user=root ansible_ssh_pass=123456 ansible_ssh_port=22 172.16.60.221 ansible_ssh_user=root ansible_ssh_pass=bo@123 ansible_ssh_port=22 172.16.60.222 ansible_ssh_user=app ansible_ssh_pass=bj@123 ansible_ssh_port=22 ansible_sudo_pass=bj@123 即172.16.60.220客戶機上要提早配置, 容許app用戶具備sudo權限. 執行: [root@ansible-server ~]# ansible test-host -m shell -a "hostname" 172.16.60.222 | SUCCESS | rc=0 >> k8s-node02 172.16.60.220 | SUCCESS | rc=0 >> k8s-master01 172.16.60.221 | SUCCESS | rc=0 >> k8s-node01 [root@ansible-server ~]# ansible -i /etc/ansible/hosts test-host -m shell -a "hostname" 172.16.60.222 | SUCCESS | rc=0 >> k8s-node02 172.16.60.220 | SUCCESS | rc=0 >> k8s-master01 172.16.60.221 | SUCCESS | rc=0 >> k8s-node01