ansible是基於模塊工做的,自己沒有批量部署的能力。真正具備批量部署的是ansible所運行的模塊,ansible只是提供一種框架。主要包括:python
(1)、鏈接插件connection plugins:負責和被監控端實現通訊;shell
(2)、host inventory:指定操做的主機,是一個配置文件裏面定義監控的主機;vim
(3)、各類模塊核心模塊、command模塊、自定義模塊;bash
(4)、藉助於插件完成記錄日誌郵件等功能;框架
(5)、playbook:劇本執行多個任務時,非必需可讓節點一次性運行多個任務。--摘自360百科運維
簡而言之ansible有以下的特色:ssh
(一)批量管理工具工具
(二)模塊oop
(三)python
(四)無終端,是基於ssh實現管理的
(五)也支持主從模式
(六)也支持playbook
Ansible的安裝
能夠直接使用yum進行安裝,前提是你已經配置了epel源
第一步:yum安裝ansible
[root@ken ~]# yum install ansible -y
第二步:查看ansible的版本信息
能夠看到個人安裝版本是2.6.2的
[root@ken ~]# ansible --version ansible 2.6.2 config file = /etc/ansible/ansible.cfg configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/site-packages/ansible executable location = /usr/bin/ansible python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
第三步:查看ansible配置文件
咱們接下來各個節點的管理主要是配置/etc/ansible/hosts文件
[root@ken ~]# rpm -qc ansible /etc/ansible/ansible.cfg /etc/ansible/hosts
Ansible使用基本配置
第一步:把須要管理的節點的IP地址加入到/etc/ansible/hosts文件中
在文件末行添加以下三行信息,第一行是定義了一個主機組,下面兩行只要把須要管理的IP地址便可填寫進去便可
[root@ken ~]# vim /etc/ansible/hosts ... # Here's another example of host ranges, this time there are no # leading 0s: ## db-[99:101]-node.example.com ##############在末行添加以下信息#################
[ken] 10.220.5.138 10.220.5.139
第二步:發送祕鑰至被操控節點
ansible是基於sshd服務,因此若是咱們須要管理其餘節點的話,須要給各個節點發送祕鑰
在主節點生成祕鑰,發送至各個被監控節點
使用以下腳本便可進行批量安裝
#!/bin/bash . /etc/init.d/functions
#author:技術流ken
#date:2018.11.16
#desc:this script for ssh key #下載expect yum install expect -y &>/dev/null if [ $? -eq 0 ];then echo -n "download expect" success echo "" else echo -n "download expect" failure echo "" exit 8 fi #刪除保存的祕鑰信息 if [ -f id_rsa -o -f id_rsa.pub -o known_hosts ];then rm -rf /root/.ssh/id* rm -rf /root/.ssh/known* fi #自動生成祕鑰對 /usr/bin/expect<<eof spawn ssh-keygen expect { "(/root/.ssh/id_rsa)" {send \r;exp_continue} "passphrase" {send \r;exp_continue} "again" {send \r} } expect eof exit eof #在各個節點分發祕鑰 for i in 37 38 39 do ken=10.220.5.1$i /usr/bin/expect<<eof spawn ssh-copy-id $ken expect { "yes/no" {send yes\r;exp_continue} "password" {send o\r} } expect eof exit eof done
Ansible使用基本格式
ansible使用格式
能夠輸入一個ansible回車便可看到使用格式
[root@ken ~]# ansible
Usage: ansible <host-pattern> [options]
ansible經常使用使用選型
你能夠輸入ansible回車看到不少的選型,這裏選取幾個比較經常使用的選項進行說明
-m:指定模塊名稱 -a:指定模塊的具體參數 -s:以sudo的方式運行操做 -i:指定被管理節點的主機列表 -f:一批鏈接幾個主機進行操做(默認是5個主機)
Ansible模塊使用幫助
正如咱們前文介紹的,ansible是基於模塊來工做的,因此要想使用ansible,必須對ansible的模塊有個清晰的認識。
查看模塊
可使用以下命令進行查看所支持的模塊
[root@ken ~]# ansible-doc -l
獲取模塊使用幫助
使用-s指定獲取shell模塊的使用幫助
[root@ken ~]# ansible-doc -s shell - name: Execute commands in nodes. shell: chdir: # cd into this directory before running the command creates: # a filename, when it already exists, this step will *not* be run. executable: # change the shell used to execute the command. Should be an absolute path to the executable. free_form: # (required) The shell module takes a free form command to run, as a string. There's not an actual option named "free form". See the examples! removes: # a filename, when it does not exist, this step will *not* be run. stdin: # Set the stdin of the command directly to the specified value. warn: # if command warnings are on in ansible.cfg, do not warn about this particular line if set to no/false.
Ansible管理節點的三種方法
還記得剛纔在hosts文件添加的三行內容嗎?
[ken] 10.220.5.138 10.220.5.139
在使用ansible的時候你能夠指定主機組名,也能夠指定一個IP,也能夠用all來表示所有,由於你的配置文件裏面可能不止一個主機組名,想要實現批量管理,就要用到了all.
因此這裏有三種使用方法
(一)指定主機組名
經過以下的命令就能夠獲取到了整個主機組節點的信息
[root@ken ~]# ansible ken -m command -a "hostname" 10.220.5.138 | SUCCESS | rc=0 >> ken 10.220.5.139 | SUCCESS | rc=0 >> ken
(二)指定一個特定IP
指定ip 10.220.5.138獲取特定節點的信息
[root@ken ~]# ansible 10.220.5.138 -m command -a "ip a" 10.220.5.138 | SUCCESS | rc=0 >> 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000 link/ether 00:0c:29:a9:90:16 brd ff:ff:ff:ff:ff:ff inet 10.220.5.138/24 brd 10.220.5.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fea9:9016/64 scope link valid_lft forever preferred_lft forever
(三)使用all
由於在配置文件裏面我只定義了一個主機組,因此這裏呈現的效果和使用ken是同樣的,你們能夠嘗試定義多個主機組,再使用all.
[root@ken ~]# ansible all -m command -a "ls /tmp" 10.220.5.138 | SUCCESS | rc=0 >> ansible_TpWP26 hsperfdata_root hsperfdata_zabbix systemd-private-495d844cb6f24a5fa04192c973de9274-chronyd.service-SVap94 systemd-private-495d844cb6f24a5fa04192c973de9274-httpd.service-Grw0SF systemd-private-79452c683402427e944cc4959183f774-httpd.service-DENLXJ systemd-private-79452c683402427e944cc4959183f774-ntpd.service-cH4QGP systemd-private-f0243ed42bf34679b61e0687522914f6-chronyd.service-DADZWt systemd-private-f0243ed42bf34679b61e0687522914f6-httpd.service-lCPw92 vmware-root 10.220.5.139 | SUCCESS | rc=0 >> ansible_bxGz8A systemd-private-2e376cd91398450f85a81bc060207ef8-chronyd.service-TxdhUO systemd-private-2e376cd91398450f85a81bc060207ef8-httpd.service-k8IZOZ systemd-private-5c9f32d6cff64520b10075e086d943ab-chronyd.service-iAH3c0 systemd-private-5c9f32d6cff64520b10075e086d943ab-httpd.service-dsAqeg systemd-private-65ded84926e64a90b0a201a805f752ca-chronyd.service-eSj3iR systemd-private-6706ba5361284cd4a0c91f3c8b68c606-chronyd.service-sLgAei systemd-private-6706ba5361284cd4a0c91f3c8b68c606-httpd.service-op5Yg7 vmware-root yum_save_tx.2018-11-15.16-02.KHC9kd.yumtx