ansible自動化運維資料庫

gycixput發表於2018-03-22
   透過ansible簡單對資料庫主機檢查和資料庫表空間的檢查,前提資料庫主機有統一的使用者名稱和密碼.

 運維環境: rhel 7.2, ansible 1.9
 資料庫環境:Oracle 11.2.0.4, Suse 11sp4,統一使用者是Oracle和usercs
ansible配置檔案/etc/ansible/ansible.cfg,否則在執行ansible時需要制定host位置
--配置host組
[root@DBAMONITOR usercs]# more /ansible/hosts 
[Suse]
192.168.15.21
192.168.15.22
192.168.15.46
192.168.15.47


--簡單測試
[root@DBAMONITOR .ssh]# ansible Suse -m ping -u usercs -k
SSH password: 
192.168.15.22 | success >> {
    "changed": false, 
    "ping": "pong"
}


192.168.15.46 | success >> {
    "changed": false, 
    "ping": "pong"
}


192.168.15.47 | success >> {
    "changed": false, 
    "ping": "pong"
}


192.168.15.21 | success >> {
    "changed": false, 
    "ping": "pong"
}


--檢視空間
[root@DBAMONITOR .ssh]# ansible Suse -m command -a "df -hT"  -u usercs -k
password:
[root@DBAMONITOR .ssh]# ansible Suse -m command -a "ls -l /tmp" -u usercs -k
password:
--複製檔案:
[root@DBAMONITOR .ssh]# ansible Suse -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg" -u usercs -k
password:
{"src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=usercs group=usercs mode=0644" }


--遠端執行指令碼
ansible Suse -m shell -a "/tmp/rocketzhang_test.sh"
password:
--遠端建立目錄
ansible Suse -m command -a "mkdir /home/oracle/scripts" -u oracle -k
password:
--配置指令碼
source ~/.profile
sqlplus -S "/as sysdba"<<EOF


set lin 200 pages 100
 select current_scn,protection_mode,database_role,force_logging,open_mode,switchover_status from gv\$database;


set pagesize 9999 
set linesize 132 
select
a.tablespace_name,
a.Total_mb,
f.Free_mb,
round(a.total_MB-f.free_mb,2) Used_mb,
round((f.free_MB/a.total_MB)*100) "%_Free"
from
(select tablespace_name, sum(bytes/(1024*1024)) total_MB from dba_data_files group by tablespace_name) a,
(select tablespace_name, round(sum(bytes/(1024*1024))) free_MB from dba_free_space group by tablespace_name) f
WHERE a.tablespace_name = f.tablespace_name(+)
order by "%_Free"
/
exit
EOF


--複製檔案
ansible Suse -m copy -a "src=/ansible/check_db.sh  dest=/home/oracle/scripts" -u oracle -k
password:
--執行指令碼
ansible Suse -m command -a "chmod +x /home/oracle/scripts/check_db.sh" -u oracle -k 
password:
ansible Suse -m shell -a "/home/oracle/scripts/check_db.sh" -u oracle -k
password:

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24585765/viewspace-2152097/,如需轉載,請註明出處,否則將追究法律責任。

相關文章