美文网首页
ansible剧本编写

ansible剧本编写

作者: ManBu_x | 来源:发表于2019-08-23 21:04 被阅读0次

    需求:如何利用ansible模块,进行rsync服务部署

    考察:需要对模块灵活应用

    思路:

    第一个里程: 确认好管理主机信息

    管理服务端主机: 172.16.1.41
    管理客户端主机: 172.16.1.31 172.16.1.7

    第二个里程: 配置知己清单信息

    vim /etc/amsible/hosts
    [rsync_server]
    172.16.1.41
    [rsync_client]
    172.16.1.31
    172.16.1.41

    第三个里程: 进行rsync服务部署

    服务端:
    1.安装软件

    ansible rsync_server -m yum -a "name=rsync state=installed"

    2. 编写配置文件

    ansible rsync_server -m copy -a "src=/ansible_file/rsync/rsyncd.conf dest=/etc/"

    3. 创建虚拟用户

    ansible rsync_server -m user -a "name=rsync create_home=no shell=/sbin/nologin"

    4. 创建密码文件并授权

    ansible rsync_server -m copy -a "content='rsync_backup:oldboy123' dest=/etc/rsync.password mode=600"

    5. 创建备份目录并授权

    ansible rsync_server -m file -a "path=/backup/ state=directory owner=rsync group=rsync"

    6. 启动服务程序

    ansible rsync_server -m service -a "name=rsyncd state=started enabled=yes"

    客户端:
    1.创建密码文件

    ansible rsync_client -m copy -a "content='oldboy123' dest=/etc/rsync.password mode=600"

    2. 进行传输测试

    ansible rsync_client -m shell -a "'rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password'"

    掌握剧本编写: 将多个模块操作命令进行整合

    模块 == 命令
    剧本 == shell脚本

    作用说明:

    1. 可以实现服务自动部署
    2. 可以提高运维工作效率
    3. 可以减少运维工作问题
    4. 可以节省公司运维成本

    剧本编写组成:

    1. 剧本角色信息: hosts --主机信息
    2. 剧本任务信息: tasks --任务信息

    *剧本编写规范: 遵循yaml语法规范 == python代码语法规范

    1. 缩进规范: 两个空格表示一个缩进关系

    标题一(一级)
    空空标题二(二级)
    空空空空标题三(三级)
    PS: 在缩进时只能用空格表示缩进,不能用tab代替缩进

    2. 字典书写规范:

    oldboy: 123456(key: value)
    PS: 冒号结尾时不需要有空格/冒号信息出现在注释说明中不需要有空格

    3. 列表规范: 横线后面要有空格

    name:
    - rsunc
    - nfs
    - rpcbind

    测试剧本编写.png

    剧本测试执行方法

    1. 进行检查测试:
    ansible-playbook --syntax-check ./test01.yml

    2. 剧本运行方式:
    模拟执行:
    ansible-playbook -C test01.yml

    补充: 取消cowsay小牛图案出现
    vim /etc/ansible/ansible.cfg ---编写修改ansible文件
    217gg
    # set to 1 if you don't want cowsay support or export ANSIBLE_NOCOWS=1
    nocows = 1

    真实执行: ansible-playbook ./test01.yml

    测验: 编写剧本完成nfs服务一键自动部署

    第一个历程: 定义主机清单信息
    [nfs_server]
    172.16.1.31
    [nfs_client]
    172.16.1.41
    172.16.1.7

    第二个历程: 编写剧本信息
    服务端:
    1) 安装软件程序
    2) 编写配置文件
    3) 创建存储目录
    4) 启动服务程序

    客户端:
    1) 安装软件程序
    2) 进行挂载操作

    剧本信息:
    方法一: 变量格式编写剧本

       - hosts: nfs_server
         tasks:
           - name: 01:install software
             yum: name=nfs-utils state=installed
             yum: name=rpcbind   state=installed
           - name: 02:push conf_file to server
             copy: src=./nfs/exports dest=/etc/
           - name: 03:create data dir
             file: path=/data  state=directory owner=nfsnobody group=nfsnobody
           - name: 04:boot server
             service: name=rpcbind state=started enabled=yes
             service: name=nfs state=started enabled=yes
      
      - hosts: nfs_client
        tasks:
          - name: 01:install software
            yum: name=nfs-utils state=installed
          - name: 02:mount data dir
            shell: mount -t nfs 172.16.1.31:/data /mnt
    

    剧本信息: 方法二 字典格式编写剧本

       - hosts: nfs_server
         tasks:
           - name: 01:install software
             yum:
               name:
                 - nfs-utils 
                 - rpcbind
               state: installed
           - name: 02:push conf_file to server
             copy:
               src: ./nfs/exports
               dest: /etc/
           - name: 03:create data dir
             file:
               path: /data
               state: directory
               owner: nfsnobody
               group: nfsnobody
           - name: 04:boot server rpc
             service:
               name: rpcbind
               state: started
               enabled: yes
           - name: 05:boot server nfs
             service:
               name: nfs
               state: started
               enabled: yes
    
       - hosts: nfs_client
         tasks:
           - name: 01:install software
             yum:
               name: nfs-utils
               state: installed
           - name: 02:mount data dir
             shell: mount -t nfs 172.16.1.31:/data /mnt 
    

    剧本编写扩展功能

    a 在剧本中设置变量信息
    设置变量方法一: 在剧本中设置变量
    data_dir: /oldboy
    设置变量方法二: 在命令行设置
    ansible-playbook -e data_dir=/boy test05.yml
    设置变量方法三: 在主机清单设置
    vim /etc/ansible/hosts 172.16.1.41 data_dir=/old

    问题: 三种变量的优先级是 命令行-剧本-主机清单

    image.png

    b 在剧本中设置注册信息
    debug: msg={{ oldboy.stdout_lines }}

    image.png

    c 在剧本中设置判断信息
    常见主机信息:

      ansible_all_ipv4_addresses:               仅显示ipv4的信息。
      ansible_devices:                          仅显示磁盘设备信息。
      ansible_distribution:                 显示是什么系统,例:centos,suse等。
      ansible_distribution_major_version:       显示是系统主版本。
      ansible_distribution_version:         仅显示系统版本。
      ansible_machine:                          显示系统类型,例:32位,还是64位。
      ansible_eth0:                         仅显示eth0的信息。
      ansible_hostname:                     仅显示主机名。
      ansible_kernel:                           仅显示内核版本。
      ansible_lvm:                              显示lvm相关信息。
      ansible_memtotal_mb:                      显示系统总内存。
      ansible_memfree_mb:                       显示可用系统内存。
      ansible_memory_mb:                        详细显示内存情况。
      ansible_swaptotal_mb:                 显示总的swap内存。
      ansible_swapfree_mb:                      显示swap内存的可用内存。
      ansible_mounts:                           显示系统磁盘挂载情况。
      ansible_processor:                        显示cpu个数(具体显示每个cpu的型号)。
      ansible_processor_vcpus:                  显示cpu个数(只显示总的个数)。   
    
    
     - hosts: all
             tasks:
               - name: install software
                 yum: name=nfs-utils state=installed
               - name: create data dir
                 file: path=/data/ state=directory
                 when: (ansible_hostname == "nfs01")      
    

    判断语法结构信息:
    01. 匹配单个信息:
    when: (ansible_hostname == "nfs01")

    02. 匹配多个信息 ???
    when: (ansible_hostname == "nfs01") or (ansible_hostname == "backup")
    when: (ansible_hostname == "nfs01") and (ansible_eth0 == "10.0.0.31")

    03. 对匹配信息进行取反
    when: (ansible_hostname != "nfs01")

    d 在剧本中设置循环信息
    e 在剧本中设置错误信息
    f 在剧本中设置标签信息
    g 在剧本中设置触发信息

    {{oldboy.stdout_lines}} 标准输出注册功能

    相关文章

      网友评论

          本文标题:ansible剧本编写

          本文链接:https://www.haomeiwen.com/subject/nzctectx.html