Ansible
1、 特性:
模塊化:調用特定的模塊,完成特定任務;
基于Python語言實現部署簡單:agentless;
支持自定義模塊;
支持playbook;編排任務;
ansible自身并不實現任何管理任務,它的所有管理任務,統統都使用模塊完成;
2、 安裝
]# yum -y install ansible
安裝ansible
]# rpm -ql ansible | less
安裝了大量的模塊,其中:
/etc/ansible/ansible.cfg 主配置文件,配置ansible程序的工作特性
/etc/ansible/hosts 主機清單
/etc/ansible/roles 存放角色的目錄
/usr/bin/ansible 主程序
/usr/bin/ansible-doc 查看配置文檔
/usr/bin/ansible-galaxy
/usr/bin/ansible-playbook 輔助工具:劇本
模塊:
獲取模塊列表:ansible-doc -l
獲取指定模塊的使用幫助:ansible-doc -s MOD_NAME
查看模塊選項用法
先在管理主機上配置ssh-keygen生成秘鑰對
[root@centos6clean ansible]# ssh-keygen -t rsa -P ''
[root@centos6clean ~]# ssh-copy-id -i .ssh/id_rsa.pub root@10.1.72.10
[root@centos6clean ~]# ssh-copy-id -i .ssh/id_rsa.pub root@10.1.72.20
簡單應用:
a)、Vim /etc/ansible/hosts
定義一個keepalived組,里面包含2臺主機
20 [keepalived]
21 10.1.72.10
22 10.1.72.20
[root@centos6clean ~]# ansible keepalived -m ping
b)、使用command模塊
[root@centos6clean ~]# ansible keepalived -m command -a "ls /tmp"
C)、使用shell模塊
[root@centos6clean ~]# ansible all -m shell -a "cat /etc/fstab | grep 'ext4'"
注意:command和shell模塊的核心參數直接為命令本身;而其它模塊的參數通常為“key=value”格式
d)、使用copy模塊
[root@centos6clean ~]# ansible all -m copy -a "src=/etc/issue dest=/tmp/issue.bak mode=777"
[root@centos6clean ~]# ansible all -m copy -a "content=hello\nworld dest=/tmp/hello"
可以直接將文件內容寫進文件
[root@centos6clean ~]# ansible all -m copy -a "content=hello\nworld owner=root dest=/tmp/hello"也可以直接指定屬主
e)、使用cron模塊
[root@centos6clean ~]# ansible all -m cron -a "minute=*/10 job='/sbin/ntpdate 10.1.0.1 &> /dev/null' name=ntp state=present"
Name:表示計劃任務條目,必須指定
State:表示狀態present(創建)是默認值|absent(刪除)
f)、使用fetch模塊:表示從遠程主機取文件
[root@centos6clean ~]# ansible all -m fetch -a "src=/etc/issue dest=/tmp/issue.bak"
g)、file:設定文件特性;(修改屬主、組等屬性)
(1)創建鏈接文件
[root@centos6clean ~]# ansible 10.1.72.10 -m file -a "path=/tmp/issue src=/etc/issue state=link"
Path:表示要創建的鏈接文件
Src:鏈接哪個文件
(2)修改屬性
[root@centos6clean ~]# ansible 10.1.72.10 -m file -a "path=/tmp/hello owner=user1 mode=222"
(3)創建文件夾
[root@centos6clean ~]# ansible 10.1.72.10 -m file -a "path=/tmp/tmp state=directory"
h)、使用hostname模塊
要使用變量方式,寫個循環實現批量更改遠程主機主機名
i)、使用yum模塊
[root@centos6clean ~]# ansible all -m yum -a "name=tree state=present"
name= 指定程序包名稱,可以帶版本號,默認最新版本;
state=
present,latest(最新版) 安裝程序;
absent 卸載程序;
j)、使用service模塊,:管理遠程主機的服務
[root@centos6clean ~]# ansible all -m service -a "name=httpd state=started"
[root@centos6clean ~]# ansible all -m service -a "name=httpd state=stopped"
name= 指明管理的服務
state=
started 啟動服務;
stopped 停止服務;
restarted 重啟服務;
enabled= 開機自動啟動;1或0;
k)、使用user模塊
name= 指定要管理的用戶;
state= 為present | absent;
system= 是否創建系統賬號;
uid= 指定UID;
shell= 默認shell類型;
group= 基本組;
groups= 額外(附加)組;
comment= 注釋信息;
home= 用戶的家目錄;
remove 當state=absent時,刪除用戶時同時刪除家目錄;
[root@centos6clean ~]# ansible all -m user -a "name=newuser shell=/sbin/nologin uid=555"
[root@centos6clean ~]# ansible all -m user -a "name=newuser state=absent"//刪除用戶
l)、使用group模塊
name=管理的組
state=新建|刪除
system=系統組
gid=
[root@centos6clean ~]# ansible all -m group -a "name=newgroup system=true"
3、YAML:是數據序列化格式設計的可讀性與腳本語言與互動
數據結構:
key:value
– item1
– item2
– item3
{name:jerry, age:21}
4、PlayBook:
核心元素:
Tasks:任務,由模塊定義的操作的列表;
Variables:變量,可調用ansible的變量或自定義的變量
Templates:模板,即使用了模板語法的文本文件;
Handlers:由特定條件觸發的Tasks;
Roles:角色,就是由以上元素組成;把主機列表分出來,用到哪個主機時,就放在哪個主機上執行;
主要作用:就是能夠把多個相關聯的任務,通過讀取YAML格式的配置文件一次編完;要把任務、變量、模板、處理器放在一個YAML格式文件中進行指定,然后任務就可一次批量執行;
Vim /etc/ansibles/first.yaml
– hosts: all
remote_user: root
tasks:
– name: create user
user: name=user111 system=true //SYSTEM=true 表示系統用戶
– name: create group
group: name=group111 system=true state=present
注意:此處嚴格要求語法
[root@centos6clean ansible]# ansible-playbook –check first.yaml//預設置
[root@centos6clean ansible]# ansible-playbook –check –list-hosts –list-tasks first.yaml
–list-hosts:顯示要執行的主機
–list-tasks:顯示要執行的任務
[root@centos6clean ansible]# ansible-playbook –check –syntax-check first.yaml
–syntax-check:測試語法
[root@centos6clean ansible]# ansible-playbook first.yaml //執行
GATHERING FACTS 第一個任務,是默認的,在每一個目標主機上運行之前,需要知道目標主機的狀態,例如主機名、ip地址等,這些都是內建變量,叫主機的facts變量,是ansible可調用的變量之一;這個過程就是收集變量的過程,也可手動收集;
TASK [create user] 在playbook中定義的第一個任務
TASK [create group] 在playbook中定義的第二個任務
PLAY RECAP 返回的報告
[root@centos6clean ansible]# ansible all -m setup //手動收集信息
5、handlers:由特定條件觸發的Tasks
使用場景:當遠程主機已經啟動nginx服務,并監聽在80端口,這時,我想改變監聽的端口
[root@centos6clean ansible]# vim nginx.yaml
– hosts: all
remote_user: root
tasks:
– name: install nginx
yum: name=nginx
– name: start nginx
service: name=nginx state=started
– name: set conf
copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf
notify: reload nginx
handlers:
– name: reload nginx
shell: nginx -s reload
如果僅修改了配置文件,卻還要從第一步,執行安裝程序包,這樣是沒必要的,所以,可使用tag,給任務加標簽,不指定標簽時,執行所有任務,加標簽時,只執行標簽所在的任務;修改如下:tags
vim nginx.yaml
– hosts: all
remote_user: root
tasks:
– name: install nginx
yum: name=nginx
– name: start nginx
service: name=nginx state=started
– name: set conf
copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf
tags: instconf
notify: reload nginx
handlers:
– name: reload nginx
shell: nginx -s reload
[root@centos6clean ansible]# ansible-playbook -t instconf –list-tasks nginx.yaml
顯示標簽
[root@centos6clean ansible]# ansible-playbook –check -t instconf nginx.yaml
跳過執行
可以定義一個標簽,多次使用
vim nginx.yaml
– hosts: all
remote_user: root
tasks:
– name: install nginx
yum: name=nginx
tags: instconf
– name: start nginx
service: name=nginx state=started
– name: set conf
copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf
tags: instconf
notify: reload nginx
handlers:
– name: reload nginx
shell: nginx -s reload
[root@centos6clean ansible]# ansible-playbook -t instconf nginx.yaml
也可以定義多個標簽一同調用
vim nginx.yaml
– hosts: all
remote_user: root
tasks:
– name: install nginx
yum: name=nginx
tags: instnginx
– name: start nginx
service: name=nginx state=started
– name: set conf
copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf
tags: instconf
notify: reload nginx
handlers:
– name: reload nginx
shell: nginx -s reload
[root@centos6clean ansible]# ansible-playbook -t instconf,instnginx nginx.yaml
Playbook小結:
Hosts: 指明任務的目標主機
Remote_user: 在遠程主機上執行任務的用戶
tasks: 任務列表
如果指明了三個任務,在三臺主機上運行,執行次序是,把第一個任務在第三臺主機運行,沒問題則在三臺主機上再運行第二個任務,如果在運行其中某一主機出現故障,后面的任務會終止;所以,任務列表,是自上而下,每個任務依次進行的;
6、variables:變量
類型:
內建:
(1) facts:任何facts變量都由正在通信的目標主機發回的信息,ansible自動獲取變量,可直接調用;在setup模塊中查看變量;
自定義:
(1) 命令行傳遞;
-e VAR=VALUE
vim install.yaml
– hosts: all
remote_user: root
tasks:
– name: install
yum: name={{ pkgname }} state=present
[root@centos6clean ansible]# ansible-playbook -e pkgname=tree install.yaml
(2) 在hosts Inventory中為每個主機定義專用變量值;
(a) 向不同的主機傳遞不同的變量 ;
IP/HOSTNAME variable_name=value
在/etc/ansible/hosts文件中寫入參數
[keepalived]
10.1.72.10 hostn=cent10
10.1.72.20 hostn=cent20
[root@centos6clean ansible]# vim host.yaml
– hosts: keepalived
remote_user: root
tasks:
– name: hostname
hostname: name={{ hostn }}
[root@centos6clean ansible]# ansible-playbook host.yaml
(b) 向組內的所有主機傳遞相同的變量 ;
[groupname:vars]
variable_name=value
[keepalived]
10.1.72.10
10.1.72.20
[keepalived:vars]
pkgname=tree
[root@centos6clean ansible]# vim host.yaml
– hosts: keepalived
remote_user: root
tasks:
– name: hostname
hostname: name={{ hostn }}
[root@centos6clean ansible]# ansible-playbook host.yaml
(3) 在playbook中定義
vars:
– var_name: value
– var_name: value
– hosts: all
remote_user: root
vars:
– pkgname: tree
tasks:
– name: install
yum: name={{ pkgname }} state=present
[root@centos6clean ansible]# ansible-playbook install.yaml
(4) Inventory還可以使用參數:
用于定義ansible遠程連接目標主機時使用的屬性,而非傳遞給playbook的變量;
ansible_ssh_host
ansible_ssh_port
ansible_ssh_user
ansible_ssh_pass
ansible_sudo_pass
…
此方法很少使用
(5) 在角色調用時傳遞
roles:
– { role: ROLE_NAME, var: value, …}
Templates:模板
文本文件,內部嵌套有模板語言腳本(使用模板語言編寫)
補充一點:setup模塊式用來收集主機的一些信息的
執行模板文件中的腳本,并生成結果數據流,需要使用template模塊;
template:
-a ”“
src=
dest=
mode=
onwer=
group=
注意:此模板不能在命令行使用,而只能用于playbook;
vim nginx.yaml
– hosts: keepalived
remote_user: root
tasks:
– name: install nginx
yum: name=nginx state=present
– name: start service
service: name=nginx state=started
– name: set conf
template: src=/root/nginx.conf.j2 dest=/etc/nginx/nginx.conf
tags: setconf
notify: reload server
handlers:
– name: reload server
shell: nginx -s reload
[root@centos6clean ansible]# vim /root/nginx.conf.j2
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes {{ ansible_processor_vcpus }};
條件測試:when語句:在tasks中使用,Jinja2的語法格式
vim nginx.yaml
– hosts: all
remote_user: root
tasks:
– name: install nginx
yum: name=nginx state=present
– name: start service6
shell: service nginx start
when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "6"
– name: start service
shell: systemctl start nginx
when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"
1、基于列表的方式
vim install.yaml
– hosts: all
remote_user: root
tasks:
– name: install
yum: name={{ item }} state=present
with_items:
– tree
– php
– vsftpd
2、基于字典的方式
vim createuser.yaml
– hosts: 10.1.72.10
remote_user: root
tasks:
– name: create group
group: name={{ item }} state=present
with_items:
– group1
– group2
– group3
– name: create user
user: name={{ item.user }} group={{ item.group }} state=present
with_items:
– {user: "user1",group: "group1"}
– {user: "user2",group: "group2"}
– {user: "user3",group: "group3"}
===================給centos6|7安裝amp并啟動服務============================
vim amp.yaml
– hosts: all
remote_user: root
tasks:
– name: install amp
yum: name={{ item }} state=present
with_items:
– php
– php-mysql
– mysql
when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "6"
– name: start server
shell: service mysqld start
shell: service httpd start
when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "6"
– name: install amp
yum: name={{ item }} state=present
with_items:
– php
– php-mysql
– mariadb
when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"
– name: start server
shell: systemctl start mariadb
shell: systemctl start httpd
when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"
角色:roles
以特定的層級目錄結構進行組織的tasks、variables、handlers、templates、files等;
role_name/
files/:存儲由copy或script等模塊調用的文件;
tasks/:此目錄中至少應該有一個名為main.yml的文件,用于定義各task;其它的文件需要由main.yml進行“包含”調用;
handlers/:此目錄中至少應該有一個名為main.yml的文件,用于定義各handler;其它的文件需要由main.yml進行“包含”調用;
vars/:此目錄中至少應該有一個名為main.yml的文件,用于定義各variable;其它的文件需要由main.yml進行“包含”調用;
templates/:存儲由template模塊調用的模板文本;
meta/:此目錄中至少應該有一個名為main.yml的文件,定義當前角色的特殊設定及其依賴關系;其它的文件需要由main.yml進行“包含”調用;
default/:此目錄中至少應該有一個名為main.yml的文件,用于設定默認變量;
在playbook中調用角色的方法:
– hosts: HOSTS
remote_user: USERNAME
roles:
– ROLE1
– ROLE2
– { role: ROLE3, VARIABLE: VALUE, …}
– { role: ROLE4, when: CONDITION }
第一步:創建固定目錄結構
]# mkdir /etc/ansible/roles/nginx/{files,tasks,templates,handlers,vars,default,meta} -pv
]# tree /etc/ansible/roles/nginx/
第二步:提供各目錄下的配置文件
[root@centos6clean nginx]# vim tasks/main.yml
– name: install nginx
yum: name=nginx state=present
– name: copy conf
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: reload nginx
tags: setconf
– name: start nginx
service: name=nginx state=started
[root@centos6clean nginx]# vim handles/main.yml
– name: reload nginx
shell: nginx -s reload
[root@centos6clean nginx]# cp /root/nginx.conf.j2 templates/
第三步:編寫yml文件
[root@centos6clean nginx]#vim nginx.yml
hosts: keepalived
remote_user: root
roles:
– nginx
實戰作業:
(1) 主/備模型的keepalived+nginx;
(2) httpd+php+php-mysql;
(3) mysql-server或mariadb-server;
擁有testdb庫,并允許testuser對其擁有所有權限;
準備環境:
[root@centos6cleanroles]#mkdir{keepalived,mariadb,php,phpmysql,httpd}/{files,tasks,vars,templates,meta,default,handlers} -pv
[root@centos6clean ansible]# vim hosts
[keepalived]
10.1.72.10 STATE=MASTER LEVEL=100 ip_addr=10.1.72.10
10.1.72.20 STATE=BACKUP LEVEL=90 ip_addr=10.1.72.20
#STATE是keepalived的狀態
#LEVEL是優先級
#ip_addr是nginx反代
[nginx]
10.1.72.40
10.1.72.30
[mariadb]
10.1.72.60
[root@centos6clean ansible]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.1.72.30 …//拷貝ssh
Keepalived角色
[root@centos6clean ansible]# vim roles/keepalived/tasks/main.yml
– name: install keeplived
yum: name=keepalived state=present
– name: set conf
template: src=keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf
– name: start keepalived
service: name=keepalived state=started
[root@centos6cleanansible]#cp/etc/keepalived/keepalived.conf ./roles/keepalived/templates/keepalived.conf.j2
[root@centos6clean ansible]# vim roles/keepalived/templates/keepalived.conf.j2
global_defs {
notification_email {
root@localhost
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script nginx {
script "killall -0 nginx && exit 0 || exit 1"
interval 3
weight -50
}
vrrp_instance VI_1 {
state {{ STATE }}
interface eth0
virtual_router_id 51
priority {{ LEVEL }}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
{{ ip_addr }}
}
track_script {
nginx
}
[root@centos6clean ansible]# vim roles/keepalived/vars/main.yml
ip_addr: "10.1.72.211/16"
Nginx角色
[root@centos6clean ansible]# vim roles/nginx/tasks/main.yml
– name: install nginx
yum: name=nginx state=present
– name: copy conf
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: reload nginx
tags: setconf
– name: start nginx
service: name=nginx state=started
[root@centos6clean ansible]# vim roles/nginx/handles/main.yml
– name: reload nginx
shell: nginx -s reload
[root@centos6clean ansible]# vim roles/nginx/templates/nginx.conf.j2
user nginx;
worker_processes {{ ansible_processor_vcpus }};
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr – $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
upstream webser {
server 10.1.72.30 weight=2;
server 10.1.72.40 weight=1
}
server {
listen 80;
root /var/www/html;
server_name www.72.com;
location / {
proxy_pass http://webser;
}
}
}
http角色
[root@centos6clean ansible]# vim roles/httpd/tasks/main.yml
– name: install amp
yum: name={{ item }} state=present
with_items:
– httpd
– php
– php-mysql
– name: start amp
service: name=httpd state=started
– name: copy
copy: src=index.php dest=/var/www/html/index.php
vim roles/httpd/files/index.php
<?php
$conn=mysql_connect("10.1.72.60",'testuser','123');
if ($conn){
echo "sussful";
}else{
echo "fales";
}
?>
mariadb角色
[root@centos6clean ansible]# vim roles/mariadb/tasks/main.yml
– name: install mariadb
yum: name=mariadb-server state=present
– name: start mariadb
service: name=mariadb state=started
– name: create db
script: sql.sh
[root@centos6clean ansible]# vim ./roles/mariadb/files/sql.sh
#!/bin/bash
#
mysql_host="127.0.0.1"
mysql_user="testuser"
mysql_pass="123"
mysql_db="testdb"
mysql -e "create database $mysql_db;"
mysql -e "grant all on $mysql_db.* to '$mysql_user'@'%' identified by '$mysql_pass';"
劇本文件:
[root@centos6clean ansible]# vim ans.yml
– hosts: all
remote_user: root
roles:
– { role: nginx, when: "ansible_default_ipv4.address == '10.1.72.10'"}
– { role: nginx, when: "ansible_default_ipv4.address == '10.1.72.20'"}
– { role: keepalived, when: "ansible_default_ipv4.address == '10.1.72.10'"}
– { role: keepalived, when: "ansible_default_ipv4.address == '10.1.72.20'"}
– { role: httpd, when: "ansible_default_ipv4.address == '10.1.72.30'"}
– { role: httpd, when: "ansible_default_ipv4.address == '10.1.72.40'"}
– { role: mariadb, when: "ansible_default_ipv4.address == '10.1.72.60'"}
[root@centos6clean roles]# tree
.├── httpd
│ ├── default
│ ├── files
│ │ └── index.php
│ ├── handlers
│ ├── meta
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ └── vars
├── keepalived
│ ├── default
│ ├── files
│ ├── handlers
│ ├── meta
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ │ └── keepalived.conf.j2
│ └── vars
│ └── main.yml
├── mariadb
│ ├── default
│ ├── files
│ │ └── sql.sh
│ ├── handlers
│ ├── meta
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ └── vars
└── nginx
├── default
├── files
├── handlers
│ └── main.yml
├── meta
├── tasks
│ └── main.yml
└── templates
└── nginx.conf.j2
原創文章,作者:landanhero,如若轉載,請注明出處:http://www.www58058.com/57923