目錄
- 一、ansible的主機清單管理
- 1、基本配置
- 2、類似的主機名配置
- 3、定義變數
- 二、YAML
- 1、YAML介紹
- 2、格式
- 3、YAML支持的資料結構
- 三、playbook簡介
- 四、playbook的核心元素
- 五、playbook劇本示例
一、ansible的主機清單管理
1、基本配置
vim /etc/ansible/hosts
[webserver] # 設定組名
www1.xxxxx.com # 定義被監控的主機,可以是主機名也可以是IP地址
www2.xxxxx.com:22222 # 冒號后定義遠程連接埠,默認是ssh的22埠
2、類似的主機名配置
[webserver]
www[01:50].xxxxxxx.com ansible_ssh_user=root ansible_ssh_pass=123123
# 這句話表示01到50的主機,可以使用免互動的方式進行管理
[dbbservers]
db-[a:f].xxxxxxx.com # 支持匹配a b c d e f
3、定義變數
1)主機變數
[webserver]
www1.xxxxxxx.com http_port=80 maxRequestsChild=808
www2.xxxxxxx.com http_port=8080 maxRequestsChild=909
2)組變數
[servers:vars]
ntp_server=ntp.xxxxxx.com
nfs_server=nfs.xxxxxx.com
3)組嵌套
[apache]
http1.xxxxxxx.com
http2.xxxxxxx.com
[nginx]
ngx1.xxxxxxx.com
ngx2.xxxxxxx.com
[webservers:children]
apache
nginx
4)inventory變數引數
引數 說明
ansible_ssh_host 將要連接的遠程主機名,與你想要設定的主機的別名不同的話,可通過此變數設定
ansible_ssh_port ssh埠號,如果不是默認的埠號,通過此變數設定
ansible_ssh_user 默認的ssh用戶名
ansible_ssh_pass ssh密碼(這種方式并不安全,強烈建議使用--ask-pass或SSH密鑰)
ansible_ssh_private_key_file ssh使用的私鑰檔案,適用于有多個密鑰,而你不想用SSH代理的情況
ansible_ssh_common_args 此設定附加到sftp,scp和ssh默認命令列
ansible_sftp_extra_args 此設定附加到默認sftp命令列
ansible_scp_extra_args 此設定附加到默認scp命令列
ansible_ssh_extra_args 此設定附加到默認ssh命令列
ansible_ssh_pipelining 確定是否使用SSH管道,可以覆寫ansible.cfg中的設定
ansible_shell_type 目標系統的shell型別,默認情況下,命令的執行使用”sh“語法,可設定為”csh“或”fish“
ansible_python_interpreter 目標主機的python路徑,適用于的情況系統中有多個 Python, 或者命令路徑不是"/usr/bin/python
ansible_*_interpreter 這里的*可以是ruby或Perl或者其他語言的解釋器,作用和ansible_python_interpreter類似
ansible_shell_executable 將設定ansible控制器將在目標機器上使用的shell,覆寫ansible.cfg中的默認為/bin/sh
二、YAML
1、YAML介紹
- YAML另一種標記語言,是用來寫組態檔的語言,非常簡潔和強大
- YAML語法和其他語言類似,也可以表達散串列,標量等資料結構
- 結構通過空格來展示,序列里配置項通過”-“來代表,Map里鍵值用”:“來分隔;
2、格式
- 大小寫敏感
- 使用縮進表示層級關系
- 縮進時不允許使用制表符,只允許使用空格
- 縮進的空格數不重要,只要相同層級的元素左側對齊即可
3、YAML支持的資料結構
- 物件:鍵值對的集合,又稱映射(mapping)、哈希(hashes)、字典(dictionary)
例如:name:example Developer
鍵 值
- 陣列:一組按次序排列的值,又稱為序列(sequence)、串列(list)
例如: - football
- basketball
- 純量 單個的、不可再分的值
例如:number:33.33
sure:true
YAML例子
name:zhangsan
age:21
name:lisi
age:22
people:
- name:zhangsan
age:21
-name:lisi
age:22
三、playbook簡介
playbook組態檔使用了YAML語法,具有簡介明了、結構清晰等特點,playbook組態檔類似于shell腳本,是一個YAML格式的檔案,用于保存針對特定需求的任務串列,之前介紹的ansible命令雖然可以完成各種任務,但是當配置一些復雜的任務時,逐條輸入就顯得效率非常地下了,更有效的方案是在playbook組態檔中放置所有的任務代碼,利用ansible-playbook命令執行該檔案,可實作自動化運維,YAML檔案的擴展名通常為.yaml或者.yml
四、playbook的核心元素
- tasks:任務,即呼叫模塊完成某操作
- variables:變數
- templates:模板
- handlers:處理器,當某種條件滿足時,觸發執行的操作
- roles:角色
五、playbook劇本示例
- hosts: test01
vars:
http_port: 80
max_clients: 200
user: root
tasks:
- name: ensure apache is at the latest version
yum: name=httpd state=latest
- name: write the apache config file
template: src=/srv/httpd.j2 dest=/etc/httpd.conf
notify:
- restart apache
- name: ensure apache is running
service: name=httpd state=started
handlers:
- name: restart apache
service: name=httpd state=restarted
執行一個playbook
ansible-playbook [yaml檔案名]
例如:
ansible-playbook ping.yml
-k (-ask-pass)用來互動輸入ssh密碼
-K(-ask-become-pass)用來互動輸入sudo密碼;-u:指定用戶
補充命令
ansible-playbook nginx.yaml --syntax-check # 檢查yaml檔案的語法
ansible-playbook nginx.yaml --list-task # 檢查tasks任務
ansible-playbook nginx.yaml --list-hosts # 檢查生效的主機
ansible-playbook nginx.yaml --start-at-task='Copy Nginx.conf' # 從某一個task任務開始
1)hosts和users介紹
- hosts: test01 # 指定主機組,可以是一個或多個組
remote_user:root # 指定遠程主機執行的用戶名
2)為每個任務定義遠程執行用戶
- hosts: test
remote_user: root
tasks:
- name: test connection
ping:
remote_user: root
- 指定遠程主機sudo切換用戶
- hosts:test
remote_user: root
become: yes
become_user: mysql
4)tasks串列和action
- hosts: 192.168.241.20
remote_user: root
tasks:
- name: disable selinux
command: '/sbin/setenforce 0'
- name: make sure apache is running
service: name=httpd state=started
play中只要執行命令的回傳值不為0,就會報錯,tasks停止
- hosts: test
remote_user: root
tasks:
- name: disable selinux
command: '/sbin/setenforce 0'
ignore_errors: True # 忽略錯誤,強制回傳成功
- name: make sure apache is running
service: name=httpd state=started
另一個示例:
- hosts: test
remote_user: root
tasks:
- name: create nginx group
group: name=nginx system=yes gid=208
- name: create nginx user
user: name=nginx uid=208 group=nginx system=yes
- hosts: test01
remote_user: root
tasks:
- name: copy file to mysql
copy: src=/etc/inittab dest=/opt/inittab.bak
5)Handlers
- hosts: test
remote_user: root
tasks:
- name: install httpd package
yum: name=httpd state=latest
- name: install configuration file for httpd
copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
- name: start httpd service
service: enabled=true name=httpd state=started
handlers:
- name: restart httpd
service: name=httpd state=restarted
這里也可以引入變數
- hosts: test
remote_user: root
vars:
- package: httpd
- service: httpd
tasks:
- name: install httpd package
yum: name={{package}} state=latest
- name: install configuration file for httpd
copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
- name: start httpd service
service: enabled=true name={{service}} state=started
handlers:
- name: restart httpd
service: name={{service}} state=restarted
playbook使用變數的方法
1、通過ansible命令傳遞
例如:編譯如下yaml
- hosts: test01
remote_user: root
vars:
- user:
tasks:
- name: add new user
user: name={{user}}
執行命令:
ansible-playbook a.yml -e “user=testvar”
查看:
ansible test01 -a ‘tail /etc/passwd’
2、直接在yaml中定義變數—如上handlers示例
3、直接參考一些變數
如:參考ansible的固定變數
vim test.yml
- hosts: test01
remote_user: root
tasks:
- name: copy file
copy: content={{ansible_all_ipv4_addresses}} dest=/opt/1.txt
4、參考主機變數
vim /etc/ansible/hosts
在test01組的主機后面添加如下
[test01]
192.168.241.4 testvar="8080" # 定義testvar變數的值為8080
vim test.yml
- hosts: test01
remote_user: root
tasks:
- name: copy file
copy: content= {{ansible_all_ipv4_addresses}} {{testvar}} dest=/opt/1.txt
5、條件測驗
單條件判斷:
vim when.yml
- hosts: test01
remote_user: root
tasks:
- name: shutdown centos
command: /sbin/shutdown -r now # h:關機,r:重啟
when: ansible_distribution == "CentOS"
多條件判斷:
vim when.yml
- hosts: test01
remote_user: root
tasks:
- name: shutdown centos7 systems
command: /sbin/shutdown -r now
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "7"
組條件判斷:
vim when.yml
- hosts: test01
remote_user: root
tasks:
- name: shutdown CentOS 6 and Debian 7 systems
command: /sbin/shutdown -t now
when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6") or
(ansible_distribution == "Debian" and ansible_distribution_major_version == "7")
自定義變數進行條件測驗
vim when.yml
- hosts: all
vars:
exist: "True"
tasks:
- name: create file
command: touch /tmp/test.txt
when: exist | match("True")
- name: delete file
command: rm -rf /tmp/test.txt
when: exist | match("False")
6、迭代
- hosts: test
remote_user: root
tasks:
- name: add user
user: name={{item}}
with_items:
- tw
- gcc
- yy
也可以自己定義
- hosts: test01
remote_user: root
tasks:
- name: add user
user: name={{ item.name }} state=present groups={{ item.groups }}
with_items:
- { name: 'test1', groups: 'wheel'}
- { name: 'test2', groups: 'root'}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/274147.html
標籤:其他
