Ansible基本概述和常用模块command、shell、service、copy、yum(一)

Ansible基本概述

Ansible 是一个配置管理系统configuration management system你只需要可以使用ssh访问你的服务器或设备就行。

Asible 能做什么

Ansible可以完成一些批量任务,或者完成一些经常需要重复的工作。
比如:同时在100台服务器上安装nginx服务,并在安装后启动服务
比如:将某个文件一次性拷贝到100台服务器上
比如:每当有信服务器加入工作环境时,你都要为信服务器部署某个服务,也就是说你需要经常重复的完成相同的的工作
这些环境下我们都可以使用ansbile

Ansible软件特点

  1. ansible不需要单独安装客户端,ssh相当于ansible客户端
  2. ansible不需要启动任何服务,仅需安装对应工具即可。
  3. ansible依赖大量的python模块来实现批量管理
  4. ansible配置文件/etc/ansible/ansible.cfg

基础架构:

 

    1.连接插件(connector plugins):用于连接主机 用来连接被管理端
    2.核心模块(core modules):连接主机实现操作 依赖于具体的模块来做具体的事情
    3.自定义模块(custom modules):根据自己的需求编写具体的模块
    4.插件(plugins):完成模块功能的补充
    5.剧本(playbooks):ansible的配置文件,将多个任务定义在剧本中,由ansible执行
    6.主机清单(host inventory):定义ansible需要操作主机的范围
    !!!ansible是模块化的 所有操作都依赖于模块!!!

1.安装ansible

安装ansible前一定先部署好epel源

centos7:

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all 
yum makecache
yum install -y ansible

查看版本

[root@m01 ~]# ansible --version
ansible 2.6.20

2.ssh部署公钥认证

ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.10.10.12
ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.10.10.13
ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.10.10.14

3.配置主机清单

主机清单配置文件/etc/ansible/hosts

[zqf]
10.10.10.12
10.10.10.13
10.10.10.14

#添加三台主机至webserver[low改良版]
[webservers]
web[1:3].zqf.com

#添加三台主机至webserver[密码版]
[webservers]
web1.zqf.com ansible_ssh_pass='123456'
web2.zqf.com ansible_ssh_pass='123456'
web3.zqf.com ansible_ssh_pass='123456'

#添加三台主机至webserver[密码改良版]
web[1:3].zqf ansible_ssh_pass='123456'

#添加三台主机至webserver[密码拆分版]
[webservers]
web1.zqf.com
web2.zqf.com
web3.zqf.com
[webservers:vars]
ansible_ssh_pass='123456'

4.验证ansible

[root@m01 ~]# ansible zqf -m ping
10.10.10.14 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
10.10.10.13 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
10.10.10.12 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

看见SUCCESS代表成功

命令执行后颜色说明

翔黄色:对远程节点进行相应修改
帽子绿:对远程节点不进行相应修改,或者只是对远程节点信息进行查看
深红色: 操作执行命令有异常
浅紫色:表示对命令执行发出警告信息(可能存在的问题,给你一下建议)

1.安装ansible

一定先部署好epel源
yum install -y ansible
查看版本
[root@m01 ~]# ansible --version
ansible 2.6.20

2.ssh部署公钥认证

ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.10.10.12
ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.10.10.13
ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.10.10.14

3.配置主机清单

[bakup]
10.10.10.12
[nfs]
10.10.10.13
[web01]
10.10.10.14  #每台机子分别对应一个名字可以单独操作

[zqf:children]  #三台机子可以同时进行操作
backup
nfs
web01

4.常用模块

command模块和shell模块

#默认模块,执行命令
[root@localhost ~]# ansible zqf -a hostname

#如果需要管道操作,则使用shell
[root@localhost ~]# ansible zqf -m shell -a "df -h |grep /$" -f 3

-f =forks  /etc/ansible/ansible.cfg #结果返回的数量


注意:  command和shell的区别:
       command只能调用一条指令
       shell可以使用管道
       不支持别名

举例:统一创建用户zqf,并修改密码为123
[root@localhost ~]# ansible zqf -m shell -a "useradd zqf"
[root@localhost ~]# ansible zqf -m command -a "useradd zqf"

[root@localhost ~]# ansible zqf -m shell -a "echo 123 |passwd --stdin zqf"

yum模块

[root@localhost ~]# ansible web -m yum -a "name=httpd state=installed"
                name              ---指定要安装的软件包名称,如果有多个,用“,”隔开
                state             ---指定使用yum的方法
                installed,present ---安装软件包
                removed,absent    ---移除软件包
                latest            ---安装最新软件包

copy模块

推送文件模块
ansible zqf -m copy -a "src=/etc/hosts dest=/tmp/test.txt"

#在推送覆盖远程端文件前,对远端已有文件进行备份,按照时间信息备份
ansible zqf -m copy -a "src=/etc/hosts dest=/tmp/test.txt backup=yes"

#直接向远端文件内写入数据信息,并且会覆盖远端文件内原有数据信息
ansible zqf -m copy -a "content='zqf' dest=/tmp/zqf"

src        ---推送数据的源文件信息
dest        ---推送数据的目标路径
backup        ---对推送传输过去的文件,进行备份
content        ---直接批量在被管理文件中添加内容
group        ---将本地文件推送到远端,指定文件属组信息
owner        ---将本地文件推送到远端,指定文件属主信息
mode        ---将本地文件推送到远端,指定文件权限信息
案例1:批量推送hosts文件,并备份
cat /etc/hosts
10.10.10.12  backup
10.10.10.13  nfs
10.10.10.14  web01
10.10.10.11  manager
[root@localhost ~]# ansible zqf -m copy -a "src=/etc/hosts dest=/etc/hosts backup=yes"
案例2:添加rsync认证文件和rsync客户端密码文件
1.添加rsync服务端认证文件
[root@manager ~]# ansible zqf -m  copy -a "content='rsync_backup:1' dest=/etc/rsync.password owner=root group=root mode=600"
10.10.10.12 | SUCCESS => {
    "changed": false, 
    "checksum": "c6e45d8d2843493d4eb37947d3a9f8df32079196", 
    "dest": "/etc/rsync.password", 
    "gid": 0, 
    "group": "root", 
    "mode": "0600", 
    "owner": "root", 
    "path": "/etc/rsync.password", 
    "size": 14, 
    "state": "file", 
    "uid": 0
}
10.10.10.13 | SUCCESS => {
    "changed": true, 
    "checksum": "c6e45d8d2843493d4eb37947d3a9f8df32079196", 
    "dest": "/etc/rsync.password", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "70a0afc63f084920453ac5ac3e2d733f", 
    "mode": "0600", 
    "owner": "root", 
    "size": 14, 
    "src": "/root/.ansible/tmp/ansible-tmp-1594235357.51-186004016617014/source", 
    "state": "file", 
    "uid": 0
}
10.10.10.14 | SUCCESS => {
    "changed": true, 
    "checksum": "c6e45d8d2843493d4eb37947d3a9f8df32079196", 
    "dest": "/etc/rsync.password", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "70a0afc63f084920453ac5ac3e2d733f", 
    "mode": "0600", 
    "owner": "root", 
    "size": 14, 
    "src": "/root/.ansible/tmp/ansible-tmp-1594235357.51-141248823621879/source", 
    "state": "file", 
    "uid": 0
}
2.验证

[root@manager ~]# ansible zqf -m shell -a "ls -l /etc/rsync.password"
10.10.10.14 | SUCCESS | rc=0 >>
-rw------- 1 root root 14 7月   9 03:09 /etc/rsync.password

10.10.10.12 | SUCCESS | rc=0 >>
-rw------- 1 root root 14 7月   9 03:09 /etc/rsync.password

10.10.10.13 | SUCCESS | rc=0 >>
-rw------- 1 root root 14 7月   9 03:09 /etc/rsync.password

service模块

[root@manager ~]# ansible zqf -m service -a "name=crond state=stopped enabled=yes"
    10.10.10.14 | SUCCESS => {
        "changed": true, 
        "enabled": true, 
        "name": "crond", 
        "state": "stopped"
    }
    10.10.10.12 | SUCCESS => {
        "changed": true, 
        "enabled": true, 
        "name": "crond", 
        "state": "stopped"
    }
    10.10.10.13 | SUCCESS => {
        "changed": true, 
        "enabled": true, 
        "name": "crond", 
        "state": "stopped"
    }
案列:启动web01的httpd服务,并添加一个首页文件,通过浏览器可以访问
[root@manager ~]# ansible web01 -m service -a "name=httpd state=started enabled=yes"
10.10.10.14 | SUCCESS => {
    "changed": true, 
    "enabled": true, 
    "name": "httpd", 
    "state": "started"
}

[root@manager ~]# ansible web01 -m copy -a "content='welcom to zqf web' dest=/var/www/html/index.html"    #添加首页文件
10.10.10.14 | SUCCESS => {
    "changed": true, 
    "checksum": "daf95be3760dc173a445d35da6df6b0319b52460", 
    "dest": "/var/www/html/index.html", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "8d36483616a16780cc86161321047c21", 
    "mode": "0644", 
    "owner": "root", 
    "size": 17, 
    "src": "/root/.ansible/tmp/ansible-tmp-1594238837.12-118217891705084/source", 
    "state": "file", 
    "uid": 0
}
posted @ 2020-07-17 15:57  梦想如花般盛开  阅读(415)  评论(0)    收藏  举报