天天看點

1 ansible安裝和常用子產品

一 概況:

不需要安裝用戶端,通過sshd去通信

基于子產品工作,子產品可以由任何語言開發

不僅支援指令行使用子產品,也支援編寫yaml格式的playbook

支援sudo

有提供UI(浏覽器圖形化)www.ansible.com/tower  10台主機以内免費

開源UI  https://github.com/alaxli/ansible_ui 文檔 http://download.csdn.net/detail/liyang23456/7741185

二 安裝:隻需要安裝服務端,用戶端不需要安裝任何東西

兩台機器 172.7.15.106  172.7.15.111

隻需要在106上安裝ansible即可

yum install -y epel-release
yum install -y ansible      

安裝完成之後,編輯/etc/ansible/hosts 去添加你要管理的主機

[test]
192.168.6.220
192.168.10.227      

三 設定秘鑰,用來免密碼管理

#################用哪個使用者生成的密鑰,就必須放到哪個使用者的家目錄裡面

106上生成密鑰對

ssh-keygen -t rsa  直接回車即可,不用設定密鑰密碼
把公鑰(id_rsa.pub)内容放到對方機器(111)的/root/.ssh/authorized_keys裡面
方法:scp .ssh/id_rsa.pub  172.7.15.111:/root/.ssh/authorized_keys
本機也要操作 
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
關閉selinux
setenforce 0      

四 常用子產品

4.1ping子產品

[yx@localhost ~]$ ansible test -m ping
192.168.6.220 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.10.227 | SUCCESS => {
    "changed": false, 
    "ping": "pong"      

4.2 常用遠端指令子產品(command、script、shell  copy  cron)

## command 作為 ansible 的預設子產品,可以運作遠端權限範圍所有的 shell 指令,不支援管道符

[yx@localhost ~]$ ansible test -m command -a 'free -h'
192.168.6.220 | CHANGED | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           1.8G        629M        201M        8.9M        1.0G        804M
Swap:          1.6G        194M        1.4G

192.168.10.227 | CHANGED | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           7.5G        692M        6.4G         74M        488M        6.4G
Swap:          4.0G          0B        4.0G

 
 ## script 的功能是在遠端主機執行主要端存儲的 shell 腳本檔案,前提是ansible服務端已經存在的檔案或腳本
 ansible test -m script -a "/tmp/test.sh"
192.168.6.220 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.6.220 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.6.220 closed."
    ], 
    "stdout": "", 
    "stdout_lines": []
}
192.168.10.227 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.10.227 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.10.227 closed."
    ], 
    "stdout": "", 
    "stdout_lines": []
}


## shell 的功能是執行遠端主機上的 shell 腳本檔案,支援管道符。前提是遠端主機必須存在的
# 用shell子產品的時候,本機上面的腳本是不會執行的,要想執行本機上面的必須用script
yx@localhost ~]$ ansible test -m shell -a "/tmp/test.sh"
192.168.10.227 | FAILED | rc=126 >>
/bin/sh: /tmp/test.sh: 權限不夠non-zero return code

192.168.6.220 | CHANGED | rc=0 >>

    
 #copy子產品用于實作主要端向目标機器複制檔案
#src是源目錄,dest是目标目錄,mode是權限
ansible 192.168.6.220 -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755" 

# cron 子產品用于配置遠端主機 crontab 
# name是注釋的名字,minute是分鐘,job是crontab的内容,其他的時間表示:分鐘 minute 小時 hour 日期 day 月份 month

[yx@localhost ~]$ ansible test -m cron -a "name='check' minute='1' job='ntpdate asia.pool.ntp.org'"
192.168.10.227 | CHANGED => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "check"
    ]
}
192.168.6.220 | CHANGED => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "check"
    ]
}


#檢視crontab 
[yx@localhost ~]$ ansible test -m command -a "crontab -l"
192.168.6.220 | CHANGED | rc=0 >>
#Ansible: check
1 * * * * ntpdate asia.pool.ntp.org

192.168.10.227 | CHANGED | rc=0 >>
#Ansible: check
1 * * * * ntpdate asia.pool.ntp.org
 
 ###批量删除crontab  若要删除該cron 隻需要加一個字段 state=absent
 
 [yx@localhost ~]$ ansible test -m cron -a "name='check' minute='1' job='ntpdate asia.pool.ntp.org' state=absent"
192.168.10.227 | CHANGED => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}
192.168.6.220 | CHANGED => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}      

4.3 get_url, yum,service,user ,setup

# get_url 子產品可以實作從遠端主機下載下傳指定 URL 到本地,支援 sha256sum 檔案教驗。
ansible test -m get_url -a "url=http://www.baidu.com dest=/tmp/index.html mode=0440 force=yes"

##yum用于安裝軟體,這個必須要root使用者 
ansible test -m yum -a "name=httpd state=installed"  # 安裝
ansible testhost -m yum -a "name=httpd state=removed"  ##解除安裝http軟體包 

##service 子產品可以用來管理遠端主機的系統服務。 也必須要有root權限才可以
ansible 192.168.6.220 -m service -a "name=firewalld state=stopped enabled=yes"  #停止
ansible 192.168.6.220 -m service -a "name=firewalld state=restarted"   #重新開機
ansible 192.168.6.220 -m service -a "name=firewalld state=reloaded" # 重新加載

##user 子產品用來進行遠端主機使用者的管理。 
ansible Client -m user -a "name=wang comment='user wang'"     # 建立使用者 wang
ansible Client -m user -a "name=wang state=absent remove=yes" # 删除使用者 wang

########檢視主機的軟硬體資訊的
ansible 192.168.6.220 -m setup      

繼續閱讀