運維工具介紹
OS Provisioning: PXE, Cobbler(repository, distritution,profile)
PXE: dhcp, tftp, (http, ftp)
dnsmasq: dhcp, dns
OS Config:
Puppet, saltstack, func
Deployment:
fabricpython
運維工具分類: agent: puppet,func agentless: ansible,fabric ssh service ansible 的核心組件: ansible core host inventory core modules custom modules playbook (yaml) 容許咱們將多個任務寫在一個文件中,這個文件就叫作playbook,是yaml格式,使用jinjia2定義模版語言) connect plugin (使用的是python中的pramiko插件,log插件) ansible的特性: 基於python語言實現,由Pramiko 來實現建立ssh鏈接,PyYAML和jinjia2三個類模塊; 部署簡單, agentless 默認使用SSH協議; (1)基於密鑰認證; (2)在Inventory中定義指定帳戶和密碼。 主從模式; master: ansible, ssh client 主節點指ansible節點,同時是ssh的客戶端節點 slave: ansible的客戶端主機 同時是ssh server節點。 支持自定義模塊:支持各類編程語言 支持Playbook 基於「模塊」管理完成各類 「任務」 ansible的安裝: 在epel源有提供,只是在系統中沒有自帶 使用yum info ansible 查看軟件的一個簡介 初步使用ansible: 1. 在Inventory文件中定義要管理的鏈接信息 2. 使用ansible的命令行客戶端,經過讀取Inventory文件定位每個能夠被管理的主機,從而指揮他們來工做。 3. 遠程主機管理以前最好能作基於ssh密鑰認證方式,這樣就不須要每一次輸入密碼。也不用在Inventory文件中寫明文密碼。 命令使用格式: ansible -m "model_name" -h 'hostname' -a "array" rpm -ql ansible 查看安裝了哪些文件 配置文件:有兩個文件須要咱們留意的 /etc/ansible/ansible.conf 假若有100個主機,一次操做這麼多對ansible主機會有很大壓力,因此能夠配置一批處理多少個配置文件 /etc/ansible/hosts 它其實就是inventory,這個文件能夠將被管理主機定義進來,每個被管理主機的ip,用戶,端口,密碼等等 ansible不是服務,因此沒有須要運行的服務,可是額外有一些命令程序須要注意 /usr/bin/ansible /usr/bin/ansible-doc 查看得出ansible已經支持哪些自帶的模塊 /usr/bin/ansible-galaxy /usr/bin/ansible-playbook /usr/bin/ansible-pull /usr/bin/ansible-vault ansible命令使用事例: 首先編輯inventory文件 /etc/ansible/hosts文件 # cat /etc/ansible/ ansible.cfg hosts hosts_bak roles/ # cat /etc/ansible/hosts [websrvs] 10.211.55.7 10.211.55.5 [dbsrvs] 10.211.55.8 如何查看模塊幫助文檔 ansible-doc -l 列出全部模塊 ansible-doc -s module_name 查看某一個模塊的使用說明 嘗試運行一個任務 ansible 命令應用基礎 man ansible查看基本用法 ansible <host-pattern> [-f forks] [-m module_name] [-a args] <host-pattern 對哪些主機生效 -f forks 一批控制多少主機執行任務。其實就是啓動的併發線程數 -m 模塊名 -a args 模塊特有的參數
常見模塊:
command 模塊 當命令行不指定模塊的時候,默認模塊就是command模塊,用於在遠程主機執行命令。
這個模塊有一個缺點,就是運行命令不能使用變量或者參數 ,若是想要使用變量 或者參數那麼要使用ansible的另一個shell模塊mysql
基於ip地址運行 [root@centos-linux .ssh]# ansible 10.211.55.8 -m command -a 'date' 10.211.55.8 | SUCCESS | rc=0 >> Mon Apr 2 14:32:04 HKT 2018 基於inventory裏配置的主機組運行 # ansible websrvs -m command -a 'date' 10.211.55.7 | SUCCESS | rc=0 >> Mon Apr 2 14:31:08 HKT 2018 10.211.55.5 | SUCCESS | rc=0 >> Mon Apr 2 14:31:09 HKT 2018 使用all,inventory裏有一個默認的組把全部主機加入到這個組中 [root@centos-linux .ssh]# ansible all -m command -a 'date' 10.211.55.7 | SUCCESS | rc=0 >> Mon Apr 2 14:33:34 HKT 2018 10.211.55.8 | SUCCESS | rc=0 >> Mon Apr 2 14:33:34 HKT 2018 10.211.55.5 | SUCCESS | rc=0 >> Mon Apr 2 14:33:35 HKT 2018 cron 模塊 ansible-doc -s cron 屬性: present . 安裝 absent 移除 如: */10 * * * * /bin/echo 'hello, world' ansible all -m cron -a 'minute="*/10" job="/bin/echo hello" name="test job" state=present' ansible all -m cron -a 'minute="*/10" job="/bin/echo hello" name="test job" state=absent' user模塊 最關鍵的屬性 name 指定用戶的名稱 # ansible-doc -s user [root@centos-linux .ssh]# ansible websrvs -m user -a 'name="user1"' 10.211.55.5 | SUCCESS => { "changed": true, "comment": "", "createhome": true, "group": 1001, "home": "/home/user1", "name": "user1", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1001 } 10.211.55.7 | SUCCESS => { "changed": true, "comment": "", "createhome": true, "group": 1001, "home": "/home/user1", "name": "user1", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1001 } 刪除 ansible websrvs -m user -a 'name="user1" state=absent' group模塊,實現組管理的 # ansible websrvs -m group -a "name=mysql gid=306 system=yes" 10.211.55.5 | SUCCESS => { "changed": true, "gid": 306, "name": "mysql", "state": "present", "system": true } 10.211.55.7 | SUCCESS => { "changed": true, "gid": 306, "name": "mysql", "state": "present", "system": true } 而後在使用user模塊添加用戶的時候指定組 # ansible websrvs -m user -a "name=mysql uid=306 system=yes group=mysql" 10.211.55.7 | SUCCESS => { "changed": true, "comment": "", "createhome": true, "group": 306, "home": "/home/mysql", "name": "mysql", "shell": "/bin/bash", "state": "present", "system": true, "uid": 306 } 10.211.55.5 | SUCCESS => { "changed": true, "comment": "", "createhome": true, "group": 306, "home": "/home/mysql", "name": "mysql", "shell": "/bin/bash", "state": "present", "system": true, "uid": 306 } copy模塊 ,從本機 複製文件到遠程主機上 ansible-doc -s copy # ansible all -m copy -a "src=/etc/fstab dest=/tmp/fstab.ansible owner=root mode=640" src 指定本地文件路徑,能夠使用相對路徑和絕對路徑 dest 遠程服務器文件拷貝的目標目錄 ,只能使用絕對路徑 owner 拷貝後屬主是root mode 權限 640 備註:src屬性不是必須的,能夠使用content屬性直接指定文件內容 # ansible all -m copy -a 'content="Hello Ansible\nHello my brother\n" dest=/tmp/test.ansible' 不只能夠拷貝文件,也能夠是目錄,直接寫目錄就能夠了 file模塊,這是一個管理文件屬性的模塊 能夠使用file模塊建立連接文件 # ansible websrvs -m file -a 'owner=mysql group=mysql mode=644 path=/tmp/fstab.ansible' 建立一個符號連接 # ansible websrvs -m file -a ' path=/tmp/fstab.link src=/tmp/fstab.ansible state=link' path 指定你要改的文件,也能夠用dest屬性,還能夠用name屬性 state = link 設置狀態爲連接文件 src 指定源文件 ping 模塊 測試遠程主機的連通性 # ansible all -m ping 10.211.55.7 | SUCCESS => { "changed": false, "ping": "pong" } 10.211.55.8 | SUCCESS => { "changed": false, "ping": "pong" } 10.211.55.5 | SUCCESS => { "changed": false, "ping": "pong" } service模塊 ,很是重要的模塊 ,控制服務的指定狀態 # ansible websrvs -m service -a 'enabled=true name=httpd state=started' enable 是否開機自動啓動,取值爲true或者false; name: 服務名稱 state: 狀態 取值友started, stopped shell 模塊,能夠使用變量 好比咱們在三臺機器上已經建立了user1用戶,如今咱們使用shell模塊給用戶建立密碼 假如咱們使用command命令執行 # ansible all -m command -a 'echo mypasswd|passwd -stdin user1' 10.211.55.8 | SUCCESS | rc=0 >> mypasswd|passwd -stdin user1 10.211.55.7 | SUCCESS | rc=0 >> mypasswd|passwd -stdin user1 10.211.55.5 | SUCCESS | rc=0 >> mypasswd|passwd -stdin user1 命令執行後,沒有報錯,這時咱們看下密碼文件,user1到底有沒有被設置密碼 cat /etc/shadow user1:!!:17623:0:99999:7::: 發現並無爲user1設置密碼,這就是由於使用command 不能執行較複雜的命令,這時咱們使用shell模塊 # ansible all -m shell -a 'echo mypasswd|passwd --stdin user1' 10.211.55.7 | SUCCESS | rc=0 >> Changing password for user user1. passwd: all authentication tokens updated successfully. 10.211.55.8 | SUCCESS | rc=0 >> Changing password for user user1. passwd: all authentication tokens updated successfully. 10.211.55.5 | SUCCESS | rc=0 >> Changing password for user user1. passwd: all authentication tokens updated successfully. 這時在看下/etc/shadow文件以下: user1:$6$YFEI7Fqn$cwMdso5xwPv5gTKkHSOavP4LraxQNv78DDP4r.tKXiewhRp9Ps7tQPofouYnQs2JjEIm0B2nKY4tR1.QRdash.:17623:0:99999:7::: script 模塊 若是想在遠程主機上執行一個 ansible服務器上存在的腳本,就須要使用此模塊 指定一個本地腳本,它會自動將這個腳本複製到每個遠程主機上並運行下。 例如本地有test.sh腳本 #!?bin/bash echo "hello ansible form script" > /tmp/script.ansible useradd user2 而後經過ansible script腳本 # ansible all -m script -a '/tmp/test.sh' 若是發現執行失敗,由於老版本ansible script 執行本地的腳本,腳本必須在執行命令的當前目錄下如: # ansible all -m script -a 'test.sh' yum 模塊,安裝程序包,卸載程序包 # ansible all -m yum -a 'name=zsh' name 指明要安裝的程序包,能夠帶上版本號 state= persent, latest 表示安裝,absent表示卸載 setup 模塊,收集遠程主機的facts 能查看每一個被管理節點在接收並運行管理命令以前,會將本身主機的相關信息,如操做系統版本,IP地址等報告給遠程的ansible主機 # ansible all -m setup 直接查看全部遠程主機的facts信息 好比nginx的配置文件,要設置work_prosess 參數設置,通常是指爲服務器cpu核心數-1的數量,這就須要遠程主機發過來的facts信息,進行設置。要使用到模版文件爲不一樣配置的服務器生成不一樣的nginx配置文件。