ansible是什么
????ansible是紅帽旗下,使用python語(yǔ)言開(kāi)發(fā),基于ssh協(xié)議工作的一款運(yùn)維工具,與其相似的工具還有puppet、cfengine、chef、func、saltstack,fabric等。
????ansible融合了其他自動(dòng)化運(yùn)維工具的優(yōu)點(diǎn),實(shí)現(xiàn)了批量操作系統(tǒng)配置、批量程序的部署、批量運(yùn)行命令等功能。
ansible的架構(gòu)

架構(gòu)圖
- Ansible:核心引擎
- Modules:包括 Ansible 自帶的核心模塊(core modules)及自定義模塊 (custom modules);
???? 1. 核心模塊:ansible執(zhí)行的任何任務(wù)都不是有自身進(jìn)行完成,而是調(diào)用各種核心模塊進(jìn)行完成;管理主機(jī)之前,先調(diào)用croe modules進(jìn)中的模塊,然后指明Host Inventory中的主機(jī),完成指定的任務(wù)。
???? 2.自定義模塊:ansible支持使用任務(wù)語(yǔ)言進(jìn)行模塊的開(kāi)發(fā),用于補(bǔ)充核心模塊的功能不足。 - Ansible:核心引擎
- HostInventory :ansible中的主機(jī)清單
- Playbooks :YAML格式文件,提高ansible使用的復(fù)用性
- Plugins:完成模塊功能的補(bǔ)充,包括連接插件、郵件插件等
- Connection Plugins :連接管理主機(jī)的插件
Inventory:定義 Ansible 管理主機(jī)的清單
ansible的特性
- 高度模塊化:調(diào)用特定的模塊,完成特定的任務(wù)
- 基于python語(yǔ)言實(shí)現(xiàn)
- 部署簡(jiǎn)單:無(wú)需agent端,更輕量級(jí) 。
- 支持playbook
- 冪等性(多次運(yùn)行結(jié)果一樣)
- 支持非root用戶管理操作,支持sudo
ansible安裝
環(huán)境:
操作系統(tǒng)
centos7.6
IP地址
- ansible: 192.168.44.100
- node1: 192.169.44.101
關(guān)閉ansible node1 node2 中的selinux firewalld以防后續(xù)干擾使用,如需啟用,也可時(shí)候再行啟動(dòng)。
注:本次未使用到ansible的最新特性,僅使用yum的進(jìn)行安裝使用,如需要使用最新特性,請(qǐng)上官方網(wǎng)站上進(jìn)行下載
安裝
yum -y install ansible # 請(qǐng)?zhí)崆芭渲煤胑pel的yum源
[root@ansible ~]# ansible --version # 本次使用的ansible版本,截止到本次使用時(shí)ansible最新版本為2.8
ansible 2.7.10
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, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
ansible的簡(jiǎn)單使用
使用前需要配置ansible主機(jī)可以直接免密登錄被管理主機(jī)
配置主機(jī)清單
vim /etc/ansible/hosts # 本次使用的是yum安裝,默認(rèn)主機(jī)清單路徑
[node1]
192.168.44.101
- 可以直接使用命令行工具 ansible
ansible 主機(jī)地址or主機(jī)組名or all -m 模塊名稱-a 模塊名稱 例:[root@ansible ~]# ansible all -m ping 192.168.44.101 | SUCCESS => { "changed": false, "ping": "pong" } - ansible-doc
ansible中有眾多模塊,可使用-l命令列出,使用-s查看模塊的簡(jiǎn)要使用說(shuō)明
[root@ansible ~]# ansible-doc -l
a10_server Manage A10 Networks AX/SoftAX/Th...
a10_server_axapi3 Manage A10 Networks AX/SoftAX/Th...
a10_service_group Manage A10 Networks AX/SoftAX/Th...
a10_virtual_server Manage A10 Networks AX/SoftAX/Th...
aci_aaa_user Manage AAA users (aaa:User)
aci_aaa_user_certificate Manage AAA user certificates (aa...
aci_access_port_to_interface_policy_leaf_profile Manage Fabric interface policy l...
aci_aep Manage attachable Access Entity ...
aci_aep_to_domain Bind AEPs to Physical or Virtual...
aci_ap
···
- ansible-playbook
用于執(zhí)行ansible的playbook的命令行工具
playbook的核心元素- Hosts 用于執(zhí)行的主機(jī)
- tasks 主要任務(wù)
- variables 將需要修改的配置抽取為變量,簡(jiǎn)化配置
- templates 可以結(jié)合facts針對(duì)不同的應(yīng)用場(chǎng)景進(jìn)行自動(dòng)修改生成所需的配置文件
- handle 由特定條件出發(fā)的任務(wù)
- roles 用于提高playbook的復(fù)用性
使用playbook進(jìn)行l(wèi)nmp的安裝
v1本次使用yum進(jìn)行nmp的安裝,后續(xù)如果需要使用編譯方式安裝自行修改yaml文件即可
- hosts: node1
remote_user: root
tasks:
- name: install {{ packages }}
yum: name={{ packages }} state=installed
vars:
packages:
- nginx
- mariadb-server
- php-fpm
- name: create data dir
command: mkdir -p /data/html
- name: nginx template
template: src=/tmp/nginx.conf dest=/etc/nginx/nginx.conf
- name: php test page
copy : src=/tmp/phptest.php dest=/data/html/
notify: restart nginx
- name: start nginx,php-fpm,mariadb
systemd: name={{ item }} state=started
with_items:
- nginx
- php-fpm
- mariadb
handlers:
- name: restart nginx
systemd: name=nginx state=restarted
tmp下的兩個(gè)文件
phptest.php
<?php
phpinfo();
?>
nginx.conf中的server段修改內(nèi)容
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /data/html;
index index.php index.html index.htm;
include /etc/nginx/default.d/*.conf;
location / {
}
location ~ .*\.*.(php|php5)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
報(bào)錯(cuò)及解決
第一次運(yùn)行yaml文件時(shí)有一個(gè)警告
TASK [install {{ item }}] *********************************************************************************************************************************
[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of using a loop to supply multiple items and
specifying `name: "{{ item }}"`, please use `name: ['nginx', 'php-fpm', 'mariadb']` and remove the loop. This feature will be removed in version 2.11.
Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
是因?yàn)榈谝淮问褂昧搜h(huán)的方式進(jìn)行安裝,后續(xù)替換成了上面yaml中的形式后不在有警告,因?yàn)樵?.7中不建議使用這種方式。
task was:
- name: Install base packages
apt:
name: "{{ item }}"
state: present
update_cache: yes
with_items:
- htop
- zsh
- s3cmd
Very standard.
The new style with Ansible 2.7 should look like:
- name: Install base packages
apt:
name: "{{ packages }}"
state: present
update_cache: yes
vars:
packages:
- htop
- zsh
- s3cmd
v2為提高playbook的可用性,可使用role的方式將playbook進(jìn)行拆分,并在下次使用時(shí)可直接調(diào)用相關(guān)的yaml文件
Role的默認(rèn)存放位置在/etc/ansible/roles
可通過(guò)修改配置文件修改role的存放位置
以特定的角色名命名目錄,子目錄命名規(guī)則
- files 存放由copy或者script模塊等調(diào)用的文件
- templates template模塊查找所需模板文件目錄
- tasks 至少應(yīng)該包含一個(gè)名為main.yaml文件;其他文件需要在此文件中通過(guò)include 進(jìn)行包含
- handles 至少應(yīng)該包含一個(gè)名為main.yaml文件,其他文件需要通過(guò)include進(jìn)行包含
- vars 至少應(yīng)該包含一個(gè)名為main.yaml文件,其他文件需要通過(guò)include進(jìn)行包含,格式 name:value
- meta 至少應(yīng)該應(yīng)該包含一個(gè)名為main.yaml文件,其他文件通過(guò)include進(jìn)行包含,定義當(dāng)前角色的特殊設(shè)定及其依賴關(guān)系
- default 設(shè)定默認(rèn)變量時(shí)使用此目錄中的main.yaml文件
子目錄不一定需要都存在,需要哪一個(gè)創(chuàng)建哪一個(gè)
使用時(shí)寫一個(gè)playbook將角色應(yīng)用到主機(jī)上
- hosts: HOSTNAME
remote_user: REMOTE_USER
roles:
- ROLER1
- ROLER2
···
playbook
- hosts: node1
remote_user: root
roles:
- install_lnmp
rolers
[root@ansible roles]# tree
.
└── install_lnmp
├── files
│ └── phptest.php
├── handlers
│ └── main.yaml
├── tasks
│ └── main.yaml
├── templates
│ └── nginx.conf
└── vars
└── main.yaml
[root@ansible install_lnmp]# cat handlers/main.yaml
- name: restart nginx
systemd: name=nginx state=restarted
[root@ansible install_lnmp]# cat tasks/main.yaml
- name: install {{ packages }}
yum: name={{ packages }} state=installed
- name: nginx template
template: src=nginx.conf dest=/etc/nginx.conf
notify: restart nginx
- name: create data dir
shell: mkdir -p /data/html
- name: php_test page
copy: src=phptest.php dest=/data/html
- name: start service
systemd: name={{ item }} state=started
with_items:
- nginx
- mariadb
- php-fpm
[root@ansible install_lnmp]# cat vars/main.yaml
packages:
- nginx
- php-fpm
- mariadb-server