用户编写 Playbook(主机剧本)交由Ansible依次去执行定义好play…
节点名称 | IP地址 |
---|---|
node1 | 192.168.111.10 |
node2 | 192.168.111.20 |
node3 | 192.168.111.20 |
node1为ansible服务器,node2、node3为被控端
配置本地yum源
[root@node1 ~]# yum install -y epel-release
[root@node1 ~]# yum repolist
安装Ansible软件包
[root@node1 ~]# yum install -y ansible
[root@node1 ~]# ansible --version 检测安装是否成功
ansible 2.9.23
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, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
命令集
/usr/bin/ansible Ansibe AD-Hoc 临时命令执行工具,常用于临时命令的执行
/usr/bin/ansible-doc Ansible 模块功能查看工具
/usr/bin/ansible-galaxy 下载/上传优秀代码或Roles模块 的官网平台,基于网络的
/usr/bin/ansible-playbook Ansible 定制自动化的任务集编排工具
/usr/bin/ansible-pull Ansible远程执行命令的工具,拉取配置而非推送配置(使用较少,海量机器时使用,对运维的架构能力要求较高)
/usr/bin/ansible-vault Ansible 文件加密工具
/usr/bin/ansible-console Ansible基于Linux Consoble界面可与用户交互的命令执行工具
/usr/bin/ansible-doc 命令
ansible-doc命令常用于获取模块的帮助信息的
[root@node1 ~]# ansible-doc -l 列出所有的模块
[root@node1 ~]# ansible-doc -s ping -s指定模块进行查询
- name: Try to connect to host, verify a usable python and return `pong' on succ
ping:
data: # Data to return for the `ping' return value. If
this parameter is
set to `crash',
the module will
cause an
exception.
/usr/bin/ansible 命令
[root@node1 ~]# ansible -h 查看帮助文档
ansible 常用参数详解
-i 指定hosts文件 如果不指定 默认也是指向hosts文件的
-m 指定使用什么模块来进行操作 默认使用的模块为command
-a 当指定模块后,-a后面进行需要进行的操作 如 ls 等等
ansible 的格式
ansible -i 指定hosts文件的路径 组名 -m 指定模块 -a 进行的一些操作
command为默认模块,作用是能够对远端远端主机进行一些基础命令的操作,比如:ls,pwd…
例:
[root@node1 ~]# ansible all -m command -a "ls /root" #列出root目录下的内容
192.168.111.20 | CHANGED | rc=0 >>
anaconda-ks.cfg
192.168.111.30 | CHANGED | rc=0 >>
anaconda-ks.cfg
和command差不多,只不过执行命令是通过/bin/sh来执行的
例:
[root@node1 ~]# ansible all -m shell -a "cat /etc/passwd | grep /bin/bash" 找可以登陆的用户
192.168.111.30 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
192.168.111.20 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
将本地脚本复制到被控端并执行
例:
编写脚本
[root@node1 ~]# vi run.sh
#!/bin/bash
echo "this is ansible' >> /root/1.txt
[root@node1 ~]# chmod a+x run.sh 赋予执行权限
[root@node1 ~]# ansible all -m script -a "/root/run.sh"
[root@node1 ~]# ansible all -m shell -a "cat /root/1.txt" 验证
192.168.111.20 | CHANGED | rc=0 >>
this is ansible
192.168.111.30 | CHANGED | rc=0 >>
this is ansible
将本地文件复制到远端服务器上
例:
[root@node1 ~]# ansible all -m copy -a "src=/root/run.sh dest=/root/"
[root@node1 ~]# ansible all -m shell -a "ls /root | grep run.sh " 验证
192.168.111.20 | CHANGED | rc=0 >>
run.sh
192.168.111.30 | CHANGED | rc=0 >>
run.sh
该模块主要用于下载软件包
例:
[root@node1 ~]# ansible all -m yum -a "name=httpd state=present"
state 有五种状态:latest、present 、installed(三个代表安装) removed absent (两个代表卸载)
主要对一些服务进行开启、关闭、重启
例:
[root@node1 ~]# ansible all -m service -a "name=httpd state=started enabled=yes"
[root@node1 ~]# ansible all -m shell -a "ss -tan | grep 80" 验证
192.168.111.20 | CHANGED | rc=0 >>
LISTEN 0 128 :::80 :::*
192.168.111.30 | CHANGED | rc=0 >>
LISTEN 0 128 :::80 :::*
主要用于添加用户删除用户等操作
例:
[root@node1 ~]# ansible all -m user -a "name=test state=present"
[root@node1 ~]# ansible all -m shell -a "cat /etc/passwd | grep test" 验证
192.168.111.20 | CHANGED | rc=0 >>
test:x:1000:1000::/home/test:/bin/bash
192.168.111.30 | CHANGED | rc=0 >>
test:x:1000:1000::/home/test:/bin/bash
主要用于创建、删除组这一些操作
例:
[root@node1 ~]# ansible all -m group -a "name=zs state=present"
[root@node1 ~]# ansible all -m shell -a "cat /etc/group | grep zs"
192.168.111.20 | CHANGED | rc=0 >>
zs:x:1001:
192.168.111.30 | CHANGED | rc=0 >>
zs:x:1001:
这个模块主要是用于下载url地址相关软件信息的
例:
[root@node1 ~]# ansible all -m get_url -a 'url=http://nginx.org/download/nginx-1.19.2.tar.gz dest=/root mode=777 force=yes'
[root@node1 ~]# ansible all -m shell -a "ls -l /root | grep *.tar.gz" 验证
192.168.111.20 | CHANGED | rc=0 >>
-rwxrwxrwx. 1 root root 1048727 Aug 11 16:55 nginx-1.19.2.tar.gz
192.168.111.30 | CHANGED | rc=0 >>
-rwxrwxrwx. 1 root root 1048727 Aug 11 16:55 nginx-1.19.2.tar.gz
通过调用facts(facts是变量)组件来收集主机信息,如vCPU个数、内存大小等,[Gathering Facts]这个任务是我们在运行一个palybook都会存在的,主要用于收集主机信息然后通过变量的方式存放起来的
Playbook剧本是由一个或者多个play组成的列表
[root@node1 ~]# vi httpd.yaml
- hosts: all #指定被控端
remote_user: root #指定远端执行的用户
tasks:
- name: install httpd #描述信息
yum: name=httpd state=present #使用yum模块来安装httpd服务
- name: start httpd service
service: name=httpd state=started enabled=yes #启动httpd服务
执行效果
[root@node1 ~]# ansible-playbook httpd.yaml
[root@node1 ~]# ansible all -m shell -a "ss -tan | grep 80" #查看是否开放了80端口
192.168.111.20 | CHANGED | rc=0 >>
TIME-WAIT 0 0 192.168.111.20:58238 39.155.141.16:80
TIME-WAIT 0 0 192.168.111.20:58230 39.155.141.16:80
LISTEN 0 128 :::80 :::*
192.168.111.30 | CHANGED | rc=0 >>
TIME-WAIT 0 0 192.168.111.30:50172 210.28.130.3:80
TIME-WAIT 0 0 192.168.111.30:50164 210.28.130.3:80
LISTEN 0 128 :::80 :::*
两者的结合可构建一个触发环境,可用于修改了配置文件需要重启服务等这些操作
准备一个httpd.conf模板修改端口号为8080
[root@node1 ~]# vi httpd.conf
Listen 8080
编写触发的Playbook
[root@node1 ~]# vi reboot.yaml
- hosts: all
remote_user: root
tasks:
- name: copy httpd.conf
copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
handlers:
- name: restart httpd #此处和notify一致
service: name=httpd state=restarted
执行效果
[root@node1 ~]# ansible-playbook reboot.yaml
[root@node1 ~]# ansible all -m shell -a "ss -tan | grep 8080"
192.168.111.20 | CHANGED | rc=0 >>
LISTEN 0 128 :::8080 :::*
192.168.111.30 | CHANGED | rc=0 >>
LISTEN 0 128 :::8080
存在tags标签的tasks,我们可以选择性的来执行
示例
[root@node1 ~]# vi tags.yaml
- hosts: all
remote_user: root
tasks:
- name: echo 1.txt
shell: echo "this is 1.txt" >> /root/1.txt
tags:
- test1
- name: echo 2.txt
shell: echo "this is 2.txt" >> /root/2.txt
tags:
- test2
执行标签为test2的
[root@node1 ~]# ansible-playbook tags.yaml --tags="test2"
[root@node1 ~]# ansible all -m shell -a "ls -l /root/"
192.168.111.20 | CHANGED | rc=0 >>
total 1040
-rw-r--r--. 1 root root 14 Aug 12 18:31 2.txt
192.168.111.30 | CHANGED | rc=0 >>
total 1040
-rw-r--r--. 1 root root 14 Aug 12 18:31 2.txt
通过关键字vars即可在playbook中定义变量
[root@node1 ~]# vi vars.yaml
- hosts: all
remote_user: root
vars:
- username: test1
- groupname: test2
tasks:
- name: create user
user: name={{ username }} state=present
- name: create group
group: name={{ groupname }} state=present
引用变量使用{{ 变量名 }}
[root@node1 ~]# vi yum.yaml
- hosts: all
remote_user: root
tasks:
- name: install package
yum: name={{ name }} state=present
[root@node1 ~]# ansible-playbook yum.yaml -e name=net-tools
/etc/ansible/roles 为存放各类角色的目录,如mysql角色、httpd角色、php角色等等,每一个角色下面都会存在如下目录:
project: 角色的名称,比如mysql、nginx等
files:存放同步到被控端上的配置文件以及源码包,供copy模块和script模块调用的目录
tasks:此目录应该包含一个main.yaml文件,用于定义角色一系列操作
vars:此目录应该包含一个main.yaml文件,用于定义此角色用到的变量
meta:此目录应该包含一个main.yaml文件,用于定义此角色的特殊设定及其依赖关系
default:此目录应该包含一个main.yaml文件,用于为当前角色定义默认变量
templates:用来存放jinjia2模板,template会自动在这个目录中寻找jinjia2模板文件
handlers: 此目录应该包含一个main.yaml文件,用于定义此角色触发的条件执行的动作
准备所需的目录
[root@node1 ~]# mkdir -pv /etc/ansible/roles/httpd/{templates,vars,tasks,handlers}
mkdir: created directory ‘/etc/ansible/roles/httpd’
mkdir: created directory ‘/etc/ansible/roles/httpd/templates’
mkdir: created directory ‘/etc/ansible/roles/httpd/vars’
mkdir: created directory ‘/etc/ansible/roles/httpd/tasks’
mkdir: created directory ‘/etc/ansible/roles/httpd/handlers’
编写httpd角色用到的变量
[root@node1 ~]# vi /etc/ansible/roles/httpd/vars/main.yaml
PORT: 8088
USERNAME: apache
GROUPNAME: apache
创建templates模板文件
模板文件的后缀名*.j2
[root@node1 templates]# mv httpd.conf httpd.conf.j2
[root@node1 templates]# vi httpd.conf.j2
Listen {{ PORT }}
User {{ USERNAME }}
Group {{ GROUPNAME }}
剧本的编写
[root@node1 ~]# vi /etc/ansible/roles/httpd/tasks/main.yaml
- name: create user
user: name=apache state=present
- name: create group
group: name=apache state=present
- name: install httpd
yum: name=httpd state=present
- name: copy httpd.conf
template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
- name: start httpd service
service: name=httpd state=started enabled=yes
配置触发操作
[root@node1 ~]# vi /etc/ansible/roles/httpd/handlers/main.yaml
- name: restart httpd
service: name=httpd state=restarted
将以上操作整合在一起
[root@node1 ~]# vi /etc/ansible/roles/all.yaml
- hosts: all
remote_user: root
roles:
- httpd
执行效果
[root@node1 ~]# ansible-playbook /etc/ansible/roles/all.yaml
PLAY [all] *********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.111.20]
ok: [192.168.111.30]
TASK [httpd : create user] *****************************************************
ok: [192.168.111.30]
ok: [192.168.111.20]
TASK [httpd : create group] ****************************************************
ok: [192.168.111.30]
ok: [192.168.111.20]
TASK [install httpd] ***********************************************************
ok: [192.168.111.30]
ok: [192.168.111.20]
TASK [copy httpd.conf] *********************************************************
ok: [192.168.111.20]
ok: [192.168.111.30]
TASK [start httpd service] *****************************************************
changed: [192.168.111.20]
changed: [192.168.111.30]
PLAY RECAP *********************************************************************
192.168.111.20 : ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.111.30 : ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0