ansible的簡單使用

安裝Ansible

1.環境準備

主機名 wan ip lan ip 角色
m01 10.0.0.61 172.16.1.61 ansible控制端
web01 10.0.0.7 172.16.1.7 ansible被控端
web02 10.0.0.8 172.16.1.8 ansible被控端

2.安裝ansible

#卸載saltmaster
[root@m01 ~]# yum remove -y salt-master salt-minion

#安裝ansible
[root@m01 ~]# yum install -y ansible

#查看ansible版本和相關信息
[root@m01 ~]# ansible --version
  config file = /etc/ansible/ansible.cfg	#主配置文件
  python version = 2.7.5
  
# 查看默認主機清單位置及相關
[root@m01 ~]# vim /etc/ansible/ansible.cfg

[root@m01 ~]# rpm -q ansible
ansible-2.9.9-1.el7.noarch
[root@m01 ~]# rpm -ql ansible|grep -v lib
/etc/ansible/ansible.cfg	#配置文件
/etc/ansible/hosts		#ansible默認主機清單文件
/etc/ansible/roles
/usr/bin/ansible	#在全局環境變量裏面,能夠TAB和使用相對路徑執行
/usr/bin/ansible-2
/usr/share/ansible

-- version	#ansible版本信息,檢查當前使用的是哪一個配置文件
-i 			#指定主機清單的路徑(默認指定/etc/ansible/hosts)

-m			#使用的模塊名稱,默認使用command模塊(不支持特殊符號,可省略),還有shell模塊(支持特殊符號,不過也不太好用)
-a			#使用的模塊參數,模塊的具體動做

-k			#提示輸入ssh密碼,而不是基於ssh的密匙認證
-C			#模擬執行測試,可是不會真的執行(編譯)
-T			#執行命令超時時間

ansible配置文件優先級

1.$ANSIBLE_CONFIG	#ansible相關的環境變量
2../ansible.cfg		#當前工做目錄下的
3.~/ansible.cfg		#當前用戶家目錄下的
4./etc/ansible/ansible.cfg	#主配置文件

#文件名必須是ansible.cfg,可使用-i切換配置文件

ansible配置文件及優化

[root@m01 ~]#  vim /etc/ansible/ansible.cfg		
[defaults]
#inventory      = /etc/ansible/hosts		#主機清單
#library        = /usr/share/my_modules/		#庫文件存放目錄
#module_utils   = /usr/share/my_module_utils/	
#remote_tmp     = ~/.ansible/tmp	#臨時py文件存放在遠程主機目錄
#local_tmp      = ~/.ansible/tmp
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks          = 5			#默認併發數
#poll_interval  = 15
#sudo_user      = root		#默認sudo用戶
#ask_sudo_pass = True		#每次執行是否詢問sudo的ssh密碼
#ask_pass      = True		#每次執行是否詢問ssh密碼
#transport      = smart
#remote_port    = 22		#遠程主機端口
#module_lang    = C
#module_set_locale = False	#是否跳過檢查的主機指紋(與第一次鏈接時的yes/no有關)

#log_path = /var/log/ansible.log	#ansible日誌

#普通用戶提權操做
[privilege_escalation]
#become=True
#become_method=sudo
#become_user=root
#become_ask_pass=False

#跳過檢查的主機指紋,
不優化就不能直接使用ansible遠程鏈接,因此服務端必定要作
(不作的話就會顯示know_hosts報錯,不打開的話也能夠,不過要先使用ssh挨個鏈接一遍,才能使用ansible來管理客戶端)
優化後能夠直接使用ansible遠程鏈接
[root@m01 ~]#  vim /etc/ansible/ansible.cfg		#改成
host_key_checking = False

ansible inventory(主機清單)

/etc/ansible/hosts 是ansible默認主機資產清單文件,用於定義被管理主機的認證信息,例如ssh登陸用戶名,密碼以及key信息python

inventory文件中填寫須要被管理的主機與主機組信息,還能夠自定義inventory主機清單的位置,使用 -i 指定文件位置便可web

ansible的鏈接可使用本地的緩存redis

使用場景一

[root@m01 ~]# vim /etc/ansible/hosts 
#[]標籤名任意,可是最好不要用特殊符號和大寫字母,中文
#端口是22的時候能夠省略
[web_group]
172.16.1.7 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'
172.16.1.8 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'
172.16.1.9 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'

[db_group]
172.16.1.51 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'
172.16.1.52 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'
172.16.1.53 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'
172.16.1.54 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'

[nfs_group]
172.16.1.31 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'

[redis_group]
172.16.1.81 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'

[lb_group]
172.16.1.5 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'
172.16.1.6 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'

[backup_group]
172.16.1.41 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'

[zabbix_group]
172.16.1.71 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'

[m01_group]
172.16.1.61 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'

[mtj_group]
172.16.1.202 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'

使用場景二

支持正則shell

#主機名(至關於註釋)不等於主機名,ansible命令指定的主機名是在hosts文件中的主機名
[root@m01 ~]# vim /etc/ansible/hosts
[web_group]
web0[1:2:3] ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'

#須要作本地域名解析
[root@m01 ~]# vim /etc/hosts
172.16.1.7 web01
172.16.1.8 web02
172.16.1.9 web03
#主機名(至關於註釋)不等於主機名
[root@m01 ~]# vim /etc/ansible/hosts
[web_group]
web01 ansible_ssh_host='172.16.1.7' ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'
web02 ansible_ssh_host='172.16.1.8' ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'
web03 ansible_ssh_host='172.16.1.9' ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'

使用場景三(變量)

使用變量指定密碼vim

[root@m01 ~]# vim /etc/ansible/hosts
[web_group]
web01 ansible_ssh_host=172.16.1.7
web02 ansible_ssh_host=172.16.1.8
web03 ansible_ssh_host=172.16.1.9

[db_group]
db01 ansible_ssh_host=172.16.1.5
db02 ansible_ssh_host=172.16.1.6

#指定web_group組的主機密碼,等於把密碼加入到主機組
[web_group:vars]
ansible_ssh_pass='1'

[db_group:vars]
ansible_ssh_pass='1'

使用場景四(變量)

#自定義標籤組或者(合併組),新組名爲install_rsync和install_nfs
[root@m01 ~]# vim /etc/ansible/hosts
[install_rsync:children]
web_group
nfs_group

[install_nfs:children]
web_group
nfs_group

使用場景五(祕鑰)

經過密匙認證客戶端緩存

1.建立密鑰對
[root@m01 ~]# ssh-keygen
2.推送公鑰
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.5
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.6
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.8
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.9
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.31
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.41
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.51
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.52
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.53
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.54
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.61

3.編輯主機清單
[root@m01 ~]# vim /etc/ansible/hosts
[web_group]
web01 ansible_ssh_host=172.16.1.7 asible_ssh_user=root ansible_ssh_port=22
web02 ansible_ssh_host=172.16.1.8 asible_ssh_user=root ansible_ssh_port=22
web03 ansible_ssh_host=172.16.1.9 asible_ssh_user=root ansible_ssh_port=22

模塊是Ansible執行的最小單位,能夠是由Python編寫,也能夠是Shell編寫,也能夠是由其餘語言編寫。

#檢查服務端和客戶端是否能夠連通(是真正能夠鏈接的,受控端狀態異常的話報錯)
只能指定標籤名,不能指定主機名
[root@m01 ~]# ansible 'web_group' -m ping
172.16.1.8 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"	#緩存和使用命令
    }, 
    "changed": false, 
    "ping": "pong"		#ping pong 至關於模塊之間的暗號
}
172.16.1.7 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
172.16.1.9 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

#查看能夠遠程鏈接的主機(不是真正能夠鏈接的,只是顯示本地記錄的)
[root@m01 ~]# ansible 'all' --list-host
  hosts (11):
    172.16.1.5
    172.16.1.6
    172.16.1.31
    172.16.1.7
    172.16.1.8
    172.16.1.9
    172.16.1.51
    172.16.1.52
    172.16.1.53
    172.16.1.54
    172.16.1.81
[root@m01 ~]# ansible 'web_group' --list-host
[root@m01 ~]# ansible 'web01' --list-host
[root@m01 ~]# ansible '10.0.0.7' --list-host
[root@m01 ~]# ansible 'all' --list-host
[root@m01 ~]# ansible 'lnmp' --list-host
[root@m01 ~]# ansible '*' --list-host	#顯示的是默認主機清單中的主機
  hosts (11):
    172.16.1.5
    172.16.1.6
    172.16.1.31
    172.16.1.7
    172.16.1.8
    172.16.1.9
    172.16.1.51
    172.16.1.52
    172.16.1.53
    172.16.1.54
    172.16.1.81

# -i指定自定義主機清單,自定義操做(通常用於臨時批量操做)
# --list-host只顯示主機清單中的第一列
[root@m01 ~]# ansible '*' -i /root/hosts --list-host
  hosts (5):
    web01
    web02
    web03
    db01
    db02

ansible ad-hoc (--help)

#查看模塊語法
[root@m01 ~]# ansible-doc command
EXAMPLES:

#查看支持的模塊個數
[root@localhost ~]# ansible-doc -l |wc -l

ansible-hoc結構返回的顏色

綠色:表示被管理端主機沒有被控制端修改(執行成功)bash

黃色:表示被管理端主機發生變動(執行成功)併發

紅色:表示出現了故障,注意看提示ssh

粉色:警告(說明命令的執行有可能行,有可能不行)測試

ansible使用yml語法(playbook)

#卸載httpd
[root@m01 ~]# yum remove -y httpd

#編輯yml語法(當心2468空格,不能直接TAB)
[root@m01 ~]# vim httpd.yml
- hosts: web_group
  tasks:
    - name: Install Httpd Server	#隨便寫,只是個標記
      yum:
        name: httpd
        state: present
        
#檢查yml語法        
[root@m01 ~]# ansible-playbook --syntax-check httpd.yml
playbook: httpd.yml

#控制端執行
1.工做目錄清單優先級 > 家目錄下清單優先級 > 默認主機清單優先級(?)
2. -C 指定要執行的yml語法文件
[root@m01 ~]# ansible-playbook -C httpd.yml 
PLAY [web_group] ******************************************************************
TASK [Gathering Facts] ******************************************************************
ok: [172.16.1.8]
ok: [172.16.1.7]
ok: [172.16.1.9]
相關文章
相關標籤/搜索