Ansible基础

一、简介

1.ansible简介

ansible是一款基于python开发的自动化运维工具,实现了批量系统配置、批量程序部署、批量运行命令等功能。

2.命令执行过程

1、加载自己的配置文件,默认/etc/ansible/ansible.cfg

2、查找对应的主机配置文件,找到要执行的主机或者组

3、加载自己对应的模块文件,如command

4、通过ansible将模块或命令生成对应的临时py文件,并将该文件传输至远程服务器

5、对应执行用户家目录的.ansible/tmp/XXX/XXX.PY文件

6、给文件+x执行7、执行并返回结果

8、删除临时py文件,sleep 0 退出

3.工作原理

在这里插入图片描述

大致工作原理就是ansible程序调用读取/etc/ansible/ansible.cfg配置文件获取主机列表清单/etc/ansible/hosts文件,获取所要处理的主机列表,然后查看剧本任务,在根据剧本中一系列任务生成一个临时的脚本文件,然后将该脚本文件发送给所管理的主机,脚本文件在远程主机上执行完成后返回结果,然后删除本地临时文件。

二、install-部署

1.安装epel源
yum install -y epel-release
2.安装ansible
yum install -y ansible
3.检测部署是否完成
rpm -ql ansible  ###列出所有文件
rpm -qc ansible ###查看配置文件
ansible --help  ###查看ansible帮助
ansible-doc -l  ###看所有模块
ansibe-doc -s yum ###看yum模块,了解其功能

三、ssh-key(可选)

1.服务器端生成公钥私钥
ssh-keygen
2.推送公钥至客户端
ssh-copy-id

四、ansible基础

1.定义主机清单
vim /etc/ansible/hosts
[web]
node2
node3
node4
node5
2.测试连通性
ansible web -m ping  

ansible web -m ping -o   ###简洁输出

ansible web -m ping -u root -k  -o    ###-u指定用户名,-k密码选项

五、主机清单

1.增加主机组
vim /etc/ansible/hosts
[web]
node2
node3
node4
node5
2.增加用户名、密码
vim /etc/ansible/hosts
[web]
node2  ansible_ssh_user='root' ansible_ssh_pass='666666'
node3  ansible_ssh_user='root' ansible_ssh_pass='666666'
node4  ansible_ssh_user='root' ansible_ssh_pass='666666'
node5  ansible_ssh_user='root' ansible_ssh_pass='666666'
3.增加端口
vim /etc/ansible/hosts
[web]
node2  ansible_ssh_user='root' ansible_ssh_pass='666666' ansible_ssh_port=2222    ###增加端口
node3  ansible_ssh_user='root' ansible_ssh_pass='666666'
node4  ansible_ssh_user='root' ansible_ssh_pass='666666'
node5  ansible_ssh_user='root' ansible_ssh_pass='666666'
4.变量
vim /etc/ansible/hosts
[web]
node2  
node3  
node4  
node5  

[web:vars]
ansible_ssh_user='root' 
ansible_ssh_pass='666666'
5.子分组
vim /etc/ansible/hosts
[web:children]
 apache
 nginx
 
[apache]
node2  
node3 

[nginx]
node4  
node5 

六、Ad-Hoc点对点模式

1.拷贝模块
ansible web -m copy -a 'src=/etc/hosts dest=/tmp/1.txt owner=root group=root mode=777'   ###拷贝文件
ansible web -m copy -a 'src=/etc/hosts dest=/tmp/1.txt owner=root group=root mode=777 backup=yes' ###拷贝文件并备份文件

删除目录和文件需要用到file模块

ansible web -m file -a 'path=/test state=absent'     ####表示删除web组的/test目录
2.用户模块

(1)创建用户

ansible web -m user -a 'name=test state=present'

(2)修改密码

生成加密密码

[root@ansible ~]# echo '123456' |openssl passwd -1 stdin
$1$bYfCvrIS$YnSldRWGYs3/DQTgFUH.9.
ansible web -m user -a 'name=test password="$1$bYfCvrIS$YnSldRWGYs3/DQTgFUH.9." append=yes' ###append表示追加

(3)修改shell

absible web -m user -a 'name=test shell=/sbin/nologin append=yes'

(4)删除用户

ansible web -m user -a 'name=test state=absent'
3.软件包管理
ansible web -m yum -a 'name=* state=latest'    ###代表升级所有软件包

以安装httpd为例子:

ansible web -m yum -a 'name=httpd state=latest'    ####出现下面的情况则已安装成功

在这里插入图片描述

到节点机器上去检查
在这里插入图片描述

说明已经安装成功。

4.服务模块

用ansible去启动节点上的httpd

ansible web -m service -a 'name=httpd state=started'    ###启动

ansible web -m service -a 'name=httpd state=started enabled=yes'   ####开机自启动

ansible web -m service -a 'name=httpd state=stoped'  ###停止

ansible web -m service -a 'name=httpd state=restarted'  ####重启

ansible web -m service -a 'name-httpd state=started enabled=no' ####禁止开机启动

5.文件模块

创建文件

ansible web -m file -a 'path=/tmp/666.txt mode=777 state=touch'

删除文件

ansible web -m file -a 'path=/tmp/666.txt state=absent'

创建文件夹

ansible web -m file -a 'path=/tmp/test mode=777 state=directory'

删除文件夹

ansible web -m file -a 'path=/tmp/test state=absent'
6.收集模块

查询所有信息

ansible web -m setup

filter可以过滤出想要的信息,比如下面的过滤出地址

ansible web -m setup -a 'filter=ansible_all_ipv4_addresses'
7.fetch模块

fetch模块的作用是从远程主机获取文件到本地

ansible web -m fetch -a 'src=/tmp/1.txt dest=root'
8.cron模块

ansible创建定时任务

ansible web -m cron -a 'name="sync time from ntpserver" minute="*/10" job="/usr/sbin/ntpdate 172.17.0.1 &>/dev/null"'

ansible删除定时任务

ansible web -m cron -a 'name="sync time from ntpserver" minute="*/10" job="/usr/sbin/ntpdate 172.17.0.1 &>/dev/null" state=absent'
9.group模块

gid 设置组的id号

name= ###管理组的名称

state ###指定组状态,默认为创建,设置值为absent为删除

system ###设置值为yes,表示创建系统组

下面为一个创建bbb组的例子:

ansible web -m group -a 'name=bbb'

下面为删除bbb组的例子:

ansible web -m group -a 'name=bbb state=absent'
10.script模块

script模块的作用主要是指定节点运行服务端的脚本

下面为一个时间写入到/tmp/time.txt的一个脚本:

服务端创建一个time.sh的脚本如下
vim /root/time.sh
date >> /tmp/time.txt

ansible web -m script -a '/root/time.sh'
11.unarchive模块

unarchive模块主要是解压缩,默认情况下,此模块会将本地压缩包拷贝到远程机器上解压,当设置了remote_src=yes选项时表示解压远程主机上的压缩包

src:必选项,要解压的报名

dest:必选项,解压到哪个目录下

remote_src:

​ yes 表示解压远程主机上的压缩包

​ no 将管理机上的包传到远程主机上解压

以下是将time.tar.gz压缩包解压到远程主机的tmp目录下

ansible web -m unarchive -a 'src=/root/time.tar.gz dest=/tmp'

以下是解压node2节点上的压缩包

ansible node2 -m unarchive -a 'src=/tmp/time.tar.gz dest=/tmp remote_src=yes'
12.shell模块

shell模块直接调用shell指令批量在节点上执行

以下是删除节点上的/tmp/vmware-root* 文件夹的一个例子

ansible web -m shell -a 'rm -rf /tmp/vmware-root*'

七、YAML-非标记语言

1.语法

列表

country:
  - China             ###注意前面空两个,然后“-”,然后空格,然后Apple,然后空格
  - American
  - Canada

字典

man:
  name: zhangsan    ####注意name:的前后都有空格
  job: Developer
2.示例(安装Apache)
1.在ansible服务器上安装httpd,目的是获取httpd.conf的配置文件,更改以后好推送到远程主机
[root@ansible tmp]# yum install -y httpd
[root@ansible tmp]# mkdir apache
[root@ansible tmp]# cd apache/
[root@ansible apache]# cp -rf /etc/h
host.conf  hostname   hosts      httpd/     
[root@ansible apache]# cp -rf /etc/httpd/conf/httpd.conf .               ####拷贝apache配置文件到当前目录下
[root@ansible apache]# ls
httpd.conf

[root@ansible apache]# grep 80 httpd.conf               
#Listen 12.34.56.78:80
Listen 80
#ServerName www.example.com:80
[root@ansible apache]# sed -i s/'Listen 80'/'Listen 8080'/g httpd.conf        ###修改80端口为8080端口,用作推送
[root@ansible apache]# grep 80 httpd.conf 
#Listen 12.34.56.78:80
Listen 8080
#ServerName www.example.com:80


2.编写剧本
vim apache.yaml
[root@ansible apache]# cat apache.yaml 
- hosts: web
  tasks:
  - name: install apache packages
    yum: name=httpd state=present
  - name: copy apache conf
    copy: src=/tmp/apache/httpd.conf dest=/etc/httpd/conf/httpd.conf
  - name: start httpd
    service: name=httpd state=started enabled=yes 
  
3.检查剧本语法和内容
ansible-playbook apache.yaml --syntax-check    ####检查语法
ansible-playbook apache.yaml --lists-tasks    ###列出任务
ansible-playbook apache.yaml --lists-hosts   ###列出主机
absible-playbook apache.yaml    ###执行剧本

4.运行剧本(结果如下)
ansible-playbook apache.yaml
[root@ansible apache]# ansible-playbook apache.yaml

PLAY [web] ************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************
ok: [node3]
ok: [node2]
ok: [node4]
ok: [node5]

TASK [install apache packages] ****************************************************************************************
changed: [node2]
changed: [node3]
changed: [node4]
changed: [node5]

TASK [copy apache conf] ***********************************************************************************************
changed: [node5]
changed: [node2]
changed: [node3]
changed: [node4]

TASK [start httpd] ****************************************************************************************************
changed: [node3]
changed: [node2]
changed: [node5]
changed: [node4]

PLAY RECAP ************************************************************************************************************
node2                      : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node3                      : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node4                      : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node5                      : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

5.到远程主机看httpd服务是否已经开启,并用浏览器登录验证是否正常

在这里插入图片描述

6.当配置文件有变更,需要重启服务的时候,需要用到handlers,这个时候yaml剧本,可以改写为下面的例子
[root@ansible apache]# cat apache.yaml 
- hosts: web
  tasks:
  - name: install apache packages
    yum: name=httpd state=present
  - name: copy apache conf
    copy: src=/tmp/apache/httpd.conf dest=/etc/httpd/conf/httpd.conf
    notify: restart httpd
  - name: start httpd
    service: name=httpd state=started enabled=yes 
  handlers:
  - name: restart httpd
    service: name=httpd state=restarted

八、Role-角色扮演

1.简介

roles是在ansible中,playbook的目录组织结构。

将代码或文件进行模块化,成为roles的文件目录组织结构。

易读,代码可重用,层次清晰。

2.目标

通过role远程部署nginx并配置

3.目录结构

目录结构如下图所示:
在这里插入图片描述
nginx:角色名

files:普通文件

handlers:触发器程序

tasks:主任务

templates:金甲模版(有变量的文件)

vars:自定义变量

准备目录结构

mkdir -p roles/nginx/{files,handlers,tasks,templates,vars}
touch roles/site.yaml
touch roles/nginx/files/index.html
echo 123456789 >> roles/nginx/files/index.html
touch roles/nginx/{handlers,tasks,vars}/main.yaml

[root@ansible ~]# tree roles/
roles/
├── nginx
│   ├── files
│   │   └── index.html
│   ├── handlers
│   │   └── main.yaml
│   ├── tasks
│   │   └── main.yaml
│   ├── templates
│   │   └── nginx.conf.j2
│   └── vars
│       └── main.yaml
└── site.yaml

4.编写任务
[root@ansible tasks]# cat main.yaml 
--- 
- name: install epel-release package
  yum: name=epel-release state=latest

- name: install nginx
  yum: name=nginx state=latest

- name: copy index.html
  copy: src=/root/roles/nginx/files/index.html dest=/usr/share/nginx/html/index.html

- name: copy nginx conf
  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
  notify: restart nginx

- name: start nginx
  service: name=nginx state=started enabled=yes

5.准备配置文件
yum install nginx -y && cp -rp /etc/nginx/nginx.conf /root/roles/nginx/templates/nginx.conf.j2
6.编写变量
vim /root/roles/nginx/templates/nginx.conf.j2
worker_processor {{ ansible_processor_cores }}    ######调用已知内部变量

[root@ansible vars]# cat main.yaml    ####自定义变量
worker_connections: 10234
7.编写处理程序
[root@ansible handlers]# cat main.yaml 
---
- name: restart nginx
  service: name=nginx state=restarted

8.编写剧本
[root@ansible roles]# cat site.yaml 
- hosts: web
  roles:
  - nginx

9.实施
ansible-playbook site.yaml --syntax-check      ####检查语法错误
ansible-playbook site.yaml     ####执行剧本
  • 20
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值