ansible 模块和playbook

ansible 模块和playbook

欢迎来到 来到大浪涛天的博客

ansible 模块和playbook

1. 应用场景

自动化运维工具:shell脚本,ansible,saltstack
服务器部署流程:购买云主机-->软件部署-->配置部署-->启动服务-->测试-->加入集群

2. ansible特点:

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

3. ansible的基础架构:

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

ansible原理图
ansible原理图

4. 安装部署步骤

  1. 环境部署,确认好,管理端和受控端,如:
test03 10.211.55.3  ansible 管理端
test04 10.211.55.4  ansible 受控端
test05 10.211.55.5  ansible 受控端
test06 10.211.55.6  ansible 受控端
test07 10.211.55.7  ansible 受控端
test08 10.211.55.8  ansible 受控端
test09 10.211.55.9  ansible 受控端
test10 10.211.55.10 ansible 受控端
  1. 部署ssh公钥认证,利用expect工具实现批量分发公钥
  2. 配置ansible主机清单
配置host清单列表目录为:/etc/ansible/hosts
[server]
10.211.55.4
10.211.55.5
10.211.55.6
10.211.55.7
10.211.55.8
10.211.55.9
10.211.55.10
  1. 利用ping模块验证ansible是否工作正常
[root@test03 getip]# ansible server -m ping
10.211.55.5 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
10.211.55.4 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
... ...

5. ansible语法格式

ansible(命令) server(主机组模块名) -m (指定模块参数) command (模块名称) -a (指定利用模块执行的动作参数) hostname (命令)

[root@test03 getip]# ansible server -m command -a "uptime"  
10.211.55.5 | CHANGED | rc=0 >>
 18:17:48 up  2:57,  1 user,  load average: 0.00, 0.01, 0.05

10.211.55.4 | CHANGED | rc=0 >>
 18:16:37 up  2:55,  1 user,  load average: 0.00, 0.01, 0.04

10.211.55.8 | CHANGED | rc=0 >>
 03:15:22 up  3:46,  1 user,  load average: 0.00, 0.01, 0.04

10.211.55.7 | CHANGED | rc=0 >>
 18:16:29 up  2:55,  1 user,  load average: 0.14, 0.05, 0.06

10.211.55.6 | CHANGED | rc=0 >>
 18:16:02 up  2:55,  1 user,  load average: 0.08, 0.03, 0.05

10.211.55.9 | CHANGED | rc=0 >>
 03:17:32 up  3:44,  1 user,  load average: 0.00, 0.01, 0.05

10.211.55.10 | CHANGED | rc=0 >>
 05:59:54 up 10:17,  1 user,  load average: 0.00, 0.01, 0.05

6. ansible常用模块

在ansible中执行一条简单的命令,并且不需要保存,对于复杂的命令则使用playbook
ansible 提示颜色信息的说明:
翔黄色:对远程的节点进行相应的修改
帽子绿:对远程节点不进行相应修改,只是对远程的节点信息进行查看
深红色:操作执行命令有异常
浅紫色:表示对命令执行发出警告信息(可能存在的问题,提供建议)

  1. command模块,默认模块,可以不写。
[root@test03 getip]# ansible server -a 'hostname'
10.211.55.4 | CHANGED | rc=0 >>
test04
  1. 如果需要使用管道等符号需要用shell模块,如:
[root@test03 getip]# ansible server -m shell -a 'df -h |grep /$'
10.211.55.4 | CHANGED | rc=0 >>
/dev/sda3        20G  4.1G   16G   21% /
  1. script脚本模块,则使用 ansible server -m script -a "a.sh"
[root@test03 ~]# echo "hostname" >>test.sh
[root@test03 ~]# echo "uptime" >>test.sh        
[root@test03 ~]# ansible server -m script -a "/root/test.sh"
10.211.55.4 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 10.211.55.4 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 10.211.55.4 closed."
    ], 
    "stdout": "test04\r\n 00:22:10 up  1:04,  1 user,  load average: 0.00, 0.01, 0.01\r\n", 
    "stdout_lines": [
        "test04", 
        " 00:22:10 up  1:04,  1 user,  load average: 0.00, 0.01, 0.01"
    ]
}
  1. yum模块
  • 4.1 name:指定要安装的软件包名称
  • 4.2 state:指定使用yum的方法
    • 4.2.1:installed,present --------安装软件包
    • 4.2.2:removed,absent --------移除软件包
    • 4.2.3:latest --------安装最新软件包
[root@test03 ~]# ansible server -m yum -a "name=httpd state=latest" 
10.211.55.4 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "changes": {
        "installed": [
            "httpd"
[root@test03 ~]# ansible server  -a "httpd -v"     
10.211.55.4 | CHANGED | rc=0 >>
Server version: Apache/2.4.6 (CentOS)
Server built:   Aug  8 2019 11:41:18
  1. copy文件拷贝模块
  • 5.1 copy模块的参数详解
    • 5.1.1 src ---------- 推送数据的源文件信息
    • 5.1.2 dest ---------- 推送数据的目标文件信息
    • 5.1.3 backup ------- 对推送过去的文件覆盖前进行备份
    • 5.1.4 content ------- 直接在批量被管理端文件中添加内容
    • 5.1.5 group ------- 将本地文件推送到远端,指定文件的属组信息
    • 5.1.6 owner ------- 将本地文件推送到远端,指定文件的属主信息
    • 5.1.7 mode ------- 将本地文件推送到远端,指定文件的权限信息
  • 5.2 推送文件模块
    [root@test03 ~]# ansible server -m copy -a "src=/etc/hosts dest=/tmp/hosts"
  • 5.3 在推送覆盖远端文件前,对已有的远端文件进行备份,按照时间信息备份
    [root@test03 ~]# ansible server -m copy -a "src=/etc/hosts dest=/etc/hosts backup=yes"
  • 5.4 直接向远端文件写入数据信息,并且会覆盖远端文件内的原有的数据信息
    [root@test03 ~]# ansible server -m copy -a "content=test dest=/tmp/hosts"
  • 5.5 向rsnc客户端推送客户端密钥并设置权限
[root@test03 ~]# ansible server -m copy -a "content='rsyncback:admin123' dest=/etc/rsync.password owner=root group=root mode=600"
[root@test03 ~]# ansible server -a "ls -l /etc/rsync.password"
10.211.55.4 | CHANGED | rc=0 >>
-rw------- 1 root root 18 2月  23 02:13 /etc/rsync.password
[root@test03 ~]# ansible server -a "cat /etc/rsync.password"  
10.211.55.4 | CHANGED | rc=0 >>
rsyncback:admin123
  1. service模块
  • 6.1 模块定义
    • 6.1.1 name:定义要启动服务的名称
    • 6.1.2 state:指定服务状态是停止或是运行,停止和运行指令要写成过去时
      • 6.1.2.1:started 启动
      • 6.1.2.2:stopped 停止
      • 6.1.2.3:restarted 重启
      • 6.1.2.4:reloaded 重载
    • 6.1.3 enabled:是否让服务开启自启动
  • 6.2 让httpd服务开启,并设置为开机启动
    [root@test03 ~]# ansible server -m service -a "name=httpd state=started enabled=yes"
  1. ansible 模块的帮助查看信息
  • 7.1 ansible-doc -l:列出所有的可用模块
  • 7.2 ansible-doc -s 模块名称:列出所选模块的参数
  1. group模块
  • 8.1 gid=(设置组id) state=absent(删除),present(创建)name=(需要创建的组名)
  • 8.2 设置组名为www的组如下:
[root@test03 ~]# ansible server -m group -a "name=www gid=666 " 
 [root@test03 ~]# ansible server -m group -a "name=www gid=666 state=absent"
 [root@test03 ~]# ansible server -m group -a "name=www  state=present"  
  1. user模块
  • 9.1 name 用户名 uid,group (uid号和gid号),state(absent删除,present创建),shell(登陆shell),create_home(创建用户时是否创建家目录),password 用户密码,不能使用明文,必须openssl加密后的密码,注意-a后面用单引号,password的值后面用双引号
    [root@test03 ~]# ansible server -m user -a 'name=zjwh3 uid=2100 group=3000 state=present password="$1$wBImMlrP$160baB2kn34qHkuQPio7J."'
  • 9.2 openssl为密码进行md5加密
    [root@test03 ~]# echo "admin" |openssl passwd -1 -stdin
  1. file模块
  • 10.1 file的各个模块详解
    • path ------- 目标文件路径,copy模块的dest,其他模块的name
    • src ------- 源文件路径
    • owner ------ 属主
    • group ----- 属组
    • mode ------ 权限
    • state ------ absent 删除 directory 创建目录 file 修改文件属性(默认) touch 创建文件 link hard 链接
    • recurse ---- recurse=yes 递归
  • 10.2 创建/data和/backup目录,权限设置为属主root,属组root,权限为666,递归
[root@test03 ~]# ansible server -m file -a "path=/bbb owner=root group=root mode=666 recurse=yes state=directory"
[root@test03 ~]# ansible server -m file -a "path=/backup owner=root group=root mode=666 recurse=yes state=directory"
  • 10.3 创建/etc/rsync.password文件,权限为600
[root@test03 ~]# ansible server -m file -a "path=/etc/rsync.password owner=root group=root state=touch mode=600"
[root@test03 ~]# ansible server -a "ls -l /etc/rsync.password"
10.211.55.4 | CHANGED | rc=0 >>
-rw------- 1 root root 18 2月  24 11:18 /etc/rsync.password
  • 10.4 注意file模块仅适合创建目录,修改所属和权限,创建软链接,除开这些操作,其他的文件管理都通过copy模块来实现,例如下面,对/etc/hosts文件做一个软连接到/tmp/hosts
[root@test03 ~]# ansible server -m file -a "path=/tmp/host src=/etc/hosts state=link "
  1. mount挂载属性
  • 11.1 mount模块的各参数详解如下:
    * path ---------挂载点
    * src ---------需要挂载的设备
    * fstype ------- 挂载设备的文件系统,iso9660,ext4,xfs,nfs,cifs samba共享文件系统,ntfs windows磁盘文件系统
    * opts ------- 挂载属性,noatime,noexec,nosuid
    * state ------- 挂载动作,present(开机自动挂载),mounted(挂载,并开机自动挂载),umounted(卸载不会清理fstab文件),absent(卸载,并清理fstab文件)
  • 11.2 通过nfs实现网络挂载服务
    • 安装nfs
      [root@test03 ~]# ansible server -m yum -a "name=rpcbind,nfs-utils state=installed "
    • 启动服务
    ansible server -m service -a "name=rpcbind state=started"
    ansible server -m service -a "name=nfs state=started"
    • 修改配置文件
      ansible server -m copy -a "content='/mnt 10.211.55.0/24(rw,sync,all_squash)' dest=/etc/exports "
    • 创建目录,用户,并修改所属
      [root@test03 ~]# ansible server -m file -a "name=/data1 state=directory mode=600"
    • 重载配置文件
     ansible server -m service -a "name=rpcbind state=restarted"
     ansible server -m service -a "name=nfs state=restarted"
    • 挂载文件系统到固定目录
    ansible server -m mount -a "path=/media src=10.211.55.3:/media state=mounted fstype=nfs "
  1. script模块,直接在路径上标明脚本目录即可如
[root@test03 ~]# ansible server -m script -a "/scripts/1.sh"
  1. cron模块
  • name:描述,必须要写,确定某个条目
  • job:任务(命令)
  • state:执行状态,present(创建默认),absent(删除)
  • minute:默认为* ,0-59,/2
  • hour:默认为* ,0-23,/2
  • day:默认为* ,0-31,/2
  • month:默认为* ,0-12,/2
  • weekday:默认为* ,0-6(对应周日-周六),/2
    例如:每分钟执行一次执行ntpdate.sh脚本进行时间同步,如果要设置10秒执行一次,则如果脚本名为a.sh,第二个脚本则写上sleep 10 && sh a.sh,然后两个脚本同时加到crontab里面,* * * * * /路径/a.sh ; /路径/b.sh.
[root@test03 scripts]# ansible server -m file -a "path=/scripts mode=777 recurse=yes"
 [root@test03 scripts]# ansible server -m copy -a "src=/scripts/ntpdate.sh dest=/scripts/ntpdate.sh"
 [root@test03 scripts]# ansible server -m file -a "path=/scripts/ntpdate.sh mode=777"
 [root@test03 scripts]# ansible server -m cron -a "name="ntpdate" job='/bin/bash /scripts/ntpdate.sh &>/dev/null' minute=*/1"
 [root@test03 scripts]# ansible server -a "crontab -l"
10.211.55.4 | CHANGED | rc=0 >>
#Ansible: ntpdate
*/1 * * * * /bin/bash /scripts/ntpdate.sh &>/dev/null

7. ansible playbook

ansible playbook是由一个或者多个模块组成,使用多个不同的模块完成同一件事情,ansible playbook 通过yaml语法识别描述的状态文件

7.1 yaml 三板斧

  • 缩进
    • yaml使用一个固定的缩进风格表示层级结构,每个缩进使用2个空格,不能使用tab,但是 可以再vim的配置文件设置一个tab由两个空格组成:set tabstop=2
  • 冒号
    • 以冒号结尾 的除外,其他所有冒号后面必须有空格
  • 短横线
    • 表示列表项,使用一个短横杆加上一个空格
    • 多个项使用同样的缩进代表同一列表

7.2 playbook的核心元素

hosts:主机清单
tasks:任务
vars:变量
handles:特定条件触发任务
template:包含了模版语法的文本文件

7.3 playbook常用参数

-C													模拟运行
--list-hosts                                        列出剧本主机清单
--list-tags                                         列出剧本标记
--list-tasks                                        列出剧本任务
--syntax-check                                      检测语法

7.4 yaml语法格式

注意一定要对齐,不对齐肯定会报错的,每个层级必须和每个层级都对齐

  • host是管理的主机
  • task是指定义的任务
  • -name是指名称
  • notify应该和handlers的名字一样
- hosts:  server
  tasks: 

    - name: Install Httpd
      yum:  name=httpd  state=installed
    - name: Configure Httpd.conf
      copy: src=/etc/ansible/ansible_playbook/conf/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
      
posted @ 2020-04-25 23:55  OuYangTao  阅读(123)  评论(0编辑  收藏  举报