環境說明
測試環境說明,有3台機器,一台作為ansible控制節點,兩台作為被管節點。
| 角色 | IP | 組名 |
|---|---|---|
| ansible控制節點 | 192.168.18.130 | - |
| 被管節點 | 192.168.18.131 | webserver |
| 被管節點 | 192.168.18.132 | webserver |
控制節點系統環境說明:
[root@syushin ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
[root@syushin ~]# uname -a
Linux syushin 3.10.0-1062.el7.x86_64 #1 SMP Wed Aug 7 18:08:02 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
[root@syushin ~]# getenforce
Disabled
[root@syushin ~]# hostname -I
192.168.18.130
安裝Ansible
控制節點操作
# 配置epel源
[root@syushin ~]# wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
# 清空本地yum緩存
[root@syushin ~]# yum clean all
# 更新緩存
[root@syushin ~]# yum makecache
# 安裝ansible
[root@syushin ~]# yum install -y ansible
# 查看ansible版本
[root@syushin ~]# ansible --version
ansible 2.9.13
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, Aug 7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
Ansible介紹
ansible.cfg
Ansible的主要配置文件
[root@syushin ~]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg # Ansible主配置文件
├── hosts # 主機清單文件
└── roles # 存放角色的目錄
ansible主配置文件的內容大部分不需要修改,只列出一些主要的配置:
[defaults]
#inventory = /etc/ansible/hosts #定義主機清單配置文件
#library = /usr/share/my_modules/ # 庫文件存放目錄
#remote_tmp = ~/.ansible/tmp # 臨時py命令文件存放在遠程主機目錄
#local_tmp = ~/.ansible/tmp # 本地的臨時命令執行目錄
#forks = 5 # 默認並發數
#sudo_user = root # 默認sudo用戶
#ask_sudo_pass = True # 每次執行ansible命令是否詢問ssh密碼
#ask_pass = True
#remote_port = 22
#module_name = command # 默認模塊,可以修改為shell模塊
#host_key_checking = False # 檢查對應服務器的host_key,建議取消注釋(第一次使用ssh密鑰連接時需要手動輸入yes,將其設置為false,就不需要手動輸入了)
#log_path = /var/log/ansible.log # 日志文件,建議啟用
ansible常用命令工具
/usr/bin/ansible:主程序,臨時命令執行工具/usr/bin/ansible-playbook:定制自動化任務,編排劇本工具/usr/bin/ansible-doc:查看配置文檔,模塊功能查看工具/usr/bin/ansible-galaxy:上傳和下載優秀代碼或者Roles模塊的官網平台/usr/bin/ansible-pull:遠程執行命令工具/usr/bin/ansible-console:基於console界面與用戶交互的執行工具
日常主要用ansible的管理方式:
Ad-Hoc: 即用ansible命令,主要用於臨時命令,一次性任務的使用場景。ansible-playbook: 主要用於長期規划好的,大型項目的場景,需要有前期的規划過程。
ansible-doc示例:
$ ansible-doc -l # 列出所有模塊
$ ansible-doc ping # 查看指定模塊幫助用法
$ ansible-doc -s ping # 顯示指定模塊的playbook片段
ansible 命令格式:
$ ansible <host-pattern> [-m module_name] [-a args]
<host-pattern>:指定目標主機,可以與/etc/ansible/hosts的格式一樣-m:指定模塊名-a:指定模塊參數-k:輸入此選項使用密碼認證(不建議使用)-b:指定sudo到哪個用戶,如-b root-C:檢查,不執行--list-hosts:查看主機列表,可簡寫--list-v:顯示詳細的執行過程,要想更詳細的信息-vv、-vvv
Inventory
ansible的主要功用在於批量主機操作,為了便攜地使用其中部分主機,可以在inventory file中將其分組命名,默認的inventory file是/etc/ansible/hosts。inventory file可以有多個,且可以通過Dynamic Inventory來動態生成。
主機清單文件格式
- Inventory文件遵循INI文件風格、中括號中的字符表示組名,可以將同一個主機同時歸並到多個不同的組中。
- 如果目標主機使用了非默認的SSH端口,可以在主機名稱之后使用冒號加端口號標明
- 如果主機遵循相似的命名模式,還可以使用列表的方式標識主機
示例:
$ cat /etc/ansible/hosts
192.168.18.131
[dbserver]
db1.example.com
db2.example.com
[web1]
www.web[1:100].example.com
[web2]
web[a:f].example.com
[appserver]
10.0.0.[1:100]
Inventory內置了一些參數,這些參數在日常工作中經常用到,可以直接在inventory中使用它們。常見參數如下:
| 參數 | 說明 | 示例 |
|---|---|---|
| ansible_ssh_host | 定義hosts ssh地址 | ansible_ssh_host=192.169.18.131 |
| ansible_ssh_port | 定義hosts ssh端口 | ansible_ssh_port=30022 |
| ansible_ssh_user | 定義hosts ssh用戶 | ansible_ssh_user=admin |
| ansible_ssh_pass | 定義hosts 認證密碼 | ansible_ssh_pass='123456' |
3.4 host-pattern
一個pattern通常關聯到一系列組(主機的集合) 。host-pattern支持的寫法有很多種形式,如下:
通配符寫法
ansible "*" -m ping
ansible all -m ping
可以寫IP地址或者主機名或者組
ansible 192.168.18.131 -m ping
ansible 192.168.18.* -m ping
ansible "webserver" -m ping
或關系,主機可以存在多個組中,多組之間用冒號隔開表示或的關系
ansible "web1:web2" -m ping
邏輯與,可以求兩個組的交集,即同時在兩個組中的主機
ansible "web1:&web2" -m ping
邏輯非,在web1組中,但是不在web2組中的主機
ansible "web1:!web2" -m ping
也支持正則表達式寫法,比如下面命令會匹配主機web1.test.com 或者web2.test.com
ansible "~(web1|web2).*\.test\.com" -m ping
ansible小試牛刀
前面環境說明中,有控制節點130和兩個被管節點131與132,現在配置環境進行測試
分發秘鑰
ansible是自動化運維工具,是基於SSH的。為了避免Ansible下發指令輸入目標主機密碼,可以用過證書簽名達到SSH無密登錄。在主節點進行配置,配置步驟如下:
[root@syushin ~]# ssh-keygen -t rsa # 生成秘鑰
[root@syushin ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.18.131 # 將秘鑰分發給被管主機
[root@syushin ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.18.132 # 將秘鑰分發給被管主機
# 驗證是否可以無密登錄
[root@syushin ~]# ssh root@192.168.18.131
Last login: Tue Oct 13 16:08:59 2020 from 192.168.18.130
[root@localhost ~]# exit
登出
Connection to 192.168.18.131 closed.
[root@syushin ~]# ssh root@192.168.18.132
Last login: Tue Oct 13 16:08:59 2020 from 192.168.18.130
[root@localhost ~]#
定義主機清單
[root@syushin ~]# cat /etc/ansible/hosts # 編輯inventory文件,內容如下:
[web1]
192.168.18.131
[web2]
192.168.18.132
測試
[root@syushin ~]# ansible web1 -m ping
192.168.18.131 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@syushin ~]# ansible web2 -m ping
192.168.18.132 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@syushin ~]# ansible web2 -m shell -a "pwd"
192.168.18.132 | CHANGED | rc=0 >>
/root
