zoukankan      html  css  js  c++  java
  • Linux:综合架构批量管理服务(ansible)--- 剧本功能实践介绍2

    ansible剧本功能实践介绍

    00. 说明:

    1) 剧本扩展功能讲解完毕
    2) nfs服务一键化部署     使用剧本扩展功能
    3) 如何将rsync和nfs两个剧本进行整合
    4) 掌握剧本roles编写方法  标准化完善剧本的过程
    

    01. 回顾:

    1) ansible程序的主机清单配置
       五种配置方法:
       a 分组配置管理主机信息  ****
       b 支持符号信息进行匹配
         web01.oldboy.com  --> web[01:02].oldboy.com 
    	 web02.oldboy.com 
       c 主机信息加上端口配置
         172.16.1.31:52113
    	 web01:52113
       d 主机后面添加变量信息
         172.16.1.31 ansible_ssh_port=52113 ansible_ssh_user=root ansible_ssh_pass=123456
       e 采用嵌入信息方式配置
         1) [web01]
    	    xxxxx
    	    [web02]
    		xxxx 
    		[web:children]
    		web01 
    		web02
         2) [web01]
    	    172.16.1.7
    		[web:vars]
    		ansible_ssh_port=52113
            ansible_ssh_user=root
            oldboy=123
    2) 剧本扩展功能 
    

    01. 编写剧本的重要功能介绍

    a 在剧本中设置变量信息  OK 3种方式 常用方式--剧本中设置
    b 在剧本中设置注册信息  OK 执行剧本时,可以显示输出命令结果信息  debug
    b 在剧本中设置判断信息  OK                                       setup
    c 在剧本中设置循环信息  OK
    d 在剧本中设置错误忽略  OK
    d 在剧本中设置标签信息  OK
    e 在剧本中设置触发信息  OK
    详细的剧本扩展:
    https://docs.ansible.com/ansible/latest/user_guide/playbooks.html
    f 在剧本中进行剧本整合
    

    02. 在剧本中设置变量信息

    方式一:直接在剧本文件编写  
    vars:
      oldboy01: data01
      oldboy02: data02
    
    方式二:在命令行中进行指定
    ansible-playbook --extra-vars=oldboy01=data01
    
    方式三:在主机清单文件编写
    [oldboy]
    oldboy01=data01
    oldboy02=data02
    
    三种变量设置方式都配置了,三种方式的优先级???
    最优先: 命令行变量设置
    次优先: 剧本中变量设置
    最后:   主机清单变量设置
    
    如何全局设置变量: roles 剧本整合
    

    03. 在剧本中设置注册信息

    - hosts: oldboy
      tasks:
        - name: check server port
          shell: netstat -lntup  --- 端口信息
          register: get_server_port<--端口信息
    
        - name: display port info
          debug: msg={{ get_server_port.stdout_lines }}
    显示进程信息,表示服务已经正常启动
    PS: 设置变量不能有空格信息
    

    04. 在剧本中设置判断信息

    如何指定判断条件:
    (ansible_hostname == "nfs01")
    (ansible_hostname == "web01")
    setup模块中显示被管理主机系统的详细信息
    
    - hosts: oldboy
      remote_user: root
      tasks:
        - name: Check File
          file: path=/tmp/this_is_{{ ansible_hostname }}_file state=touch
          when: (ansible_hostname == "nfs") or (ansible_hostname == "backup")	
    
        - name: install httpd
    	    yum: name=httpd state=installed
    	    when: (系统情况 == "CentOS")
    	  
    	- name: install httpd2
          yum: name=httpd2 state=installed
          when: (系统情况 == "ubuntu") 
    
    获取内置变量方法:
    ansible oldboy -m setup -a "filter=ansible_hostname"
    常见主机信息:
    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个数(只显示总的个数)。
    
    获取子信息方法:
    ansible_eth0[ipv4]
    

    04. 在剧本中设置循环信息

    vim test04.yml
    - hosts: all
      remote_user: root
      tasks:
        - name: Add Users
          user: name={{ item.name }} groups={{ item.groups }} state=present
          with_items: 
    	    - { name: 'testuser1', groups: 'bin' }
    		- { name: 'testuser2', groups: 'root' }
    
    vim test05.yml
    - hosts: all
      remote_user: root
      tasks:
        - name: Installed Pkg
          yum: name={{ item }}  state=present
          with_items:
    	    - wget
    		- tree
    		- lrzsz	
    
    剧本执行出现错误排查思路/步骤:
    1) 找到剧本中出现问题关键点
    2) 将剧本中的操作转换成模块进行操作
    3) 将模块的功能操作转换成linux命令
       本地管理主机上执行命令测试
       远程被管理主机上执行命令测试
    
    - name: 01-install rsync
      yum:
        name: ['rsync', 'tree', 'wget']  --- saltstack
        state: installed
    
    - name: xxx 
      yum: name=xxx state=installed      --- ansible
    

    05. 在剧本中设置忽略错误

    默认playbook会检查命令和模块的返回状态,如遇到错误就中断playbook的执行
    可以加入ignore_errors: yes忽略错误
    vim test06.yml
    - hosts: all
      remote_user: root
      tasks:
        - name: Ignore False
          command: /bin/false
    	    ignore_errors: yes
        - name: touch new file
    	    file: path=/tmp/oldboy_ignore state=touch		
    

    06. 在剧本中设置标签功能

    - hosts: oldboy
      ignore_errors: yes
      remote_user: root
      tasks:
        - name: Check File
          file: path=/tmp/this_is_{{ ansible_hostname }}_file state=touch
          when: (ansible_hostname == "nfs01") or (ansible_hostname == "backup")
    	    tags: t1
    
        - name: bad thing
          command: ech 123
          #ignore_errors: yes
    	    tags: t2
    
        - name: install httpd
          yum: name=httpd state=installed
          when: (ansible_all_ipv4_addresses == ["172.16.1.7","10.0.0.7"])
    	    tags: t3
    
        - name: install httpd2
          yum: name=httpd2 state=installed
          when: (ansible_distribution == "ubuntu")
    	    tags: t4
    
    指定执行哪个标签任务: ansible-playbook --tags=t2 test05.yml 
    跳过指定标签任务:    ansible-playbook --skip-tags=t2 test05.yml 	
    


    07. 在剧本中设置触发功能

    - hosts: backup
      remote_user: root
      tasks:
        - name: 01 Install rsync
          yum: name=rsync state=present
        
        - name: 02 push config file
          copy: src=./file/{{ item.src }} dest=/etc/{{ item.dest }} mode={{ item.mode }} 
          with_items:
            - { src: "rsyncd.conf", dest: "rsyncd.conf", mode: "0644" }
            - { src: "rsync.password", dest: "rsync.password", mode: "0600" }
          notify: restart rsync server
    
      handlers:
        - name: restart rsync server
          service: name=rsyncd state=restarted   
    

    08. 将多个剧本进行整合

    方式一:include_tasks: f1.yml  
    - hosts: all
      remote_user: root
      tasks:
        - include_tasks: f1.yml
        - include_tasks: f2.yml
    
    方式二:include: f1.yml
    - include:f1.yml	
    - include:f2.yml
    
    方式三(推荐使用):- import_playbook:
    [root@m01 ansible-playbook]# cat main.yml 
    - import_playbook: base.yml     
    - import_playbook: rsync.yml    
    - import_playbook: nfs.yml      
    - import_playbook: oxxx.yml
    - import_playbook: rsync.yml
    - import_playbook: nfs.yml
    

    09 编写NFS服务剧本

    第一个历程: 创建几个目录

    cd /etc/ansible/ansible-playbook
    mkdir nfs-file
    mkdir nfs-file/{nfs-server,nfs-client}
    [root@m01 ansible-playbook]# tree nfs-file/
    nfs-file/
    ├── nfs-client
    └── nfs-server
    

    第二个历程: 编写剧本信息
    1.主机清单

    cat /etc/ansible/hosts
    [nfs:children]
    nfs_server
    nfs_client
    [nfs_server]
    172.16.1.31
    [nfs_client]
    172.16.1.7
    173.16.1.41
    #172.16.1.8
    #172.16.1.9
    

    2.剧本信息

    [root@m01 ansible-playbook]# cat nfs-server.yaml 
    - hosts: nfs
      gather_facts: no
    #  上述中gather_facts: no可以不提前收集系统信息,执行时速度能快一些
    #  此处定义的变量不会被使用,需要定义到各自的模块中或者写到/etc/ansible/hosts中
    #  vars:
    #    Data_dir: /data
      tasks:
        - name: 01-install rpc,nfs
          yum:
            name: ['nfs-utils','rpcbind']
            state: installed 
    
    - hosts: nfs_server
      gather_facts: no
      vars:
        Data_dir: /data
      tasks:
        - name: 01-copy conf file
          copy: src=/etc/ansible/ansible-playbook/nfs-file/nfs-server/exports dest=/etc/
          notify: restart nfs service
        - name: 02-create data dir
          file: path={{ Data_dir }} state=directory owner=nfsnobody group=nfsnobody
        - name: 03-boot rpcbind and nfs server
         # service: name=rpcbind state=started enabled=yes
         # service: name=nfs state=started enabled=yes
          service: name={{ item }} state=started enabled=yes
          with_items:
            - rpcbind
            - nfs
    
      handlers:
        - name: restart nfs service
          service: name=nfs state=restarted 
    
    - hosts: nfs_client
     vars:
       Data_dir: /data
      tasks:
        - name: 01-mount dir
          mount: src=172.16.1.31:{{ Data_dir }} path=/mnt state=mounted fstype=nfs
        - name: 02-check mount info
          shell: df -h|grep /data
          register: mount_info
        - name: 03-display mount info 
          debug: msg={{ mount_info.stdout_lines }}
    [root@m01 ansible-playbook]#
    

    第三个历程: 进行剧本测试

    关闭服务端和客户端服务,删除服务端共享目录,卸载客户端远程挂载项
    nfs01:systemctl stop rpcbind nfs
    nfs01:systemctl status rpcbind nfs
    nfs01:rm -rf /data
    web01:umount /mnt
    web01:df -h

    [root@m01 ansible-playbook]# echo "/data 172.16.1.0/24(rw,sync)" >/etc/ansible/ansible-playbook/nfs-file/nfs-server/exports
    [root@m01 ansible-playbook]# ansible-playbook nfs-server.yaml --syntax-check
    [root@m01 ansible-playbook]# ansible-playbook nfs-server.yaml -C
    [root@m01 ansible-playbook]# ansible-playbook nfs-server.yaml 
    

    10. ansible程序roles

    规范(其实就是将剧本文件进行规范的拆分)

    解决剧本编写完问题:

    1. 目录结构不够规范 OK
    2. 编写好的任务如何重复调用
    3. 服务端配置文件改动,客户端参数信息也自动变化
    4. 汇总剧本中没有显示主机角色信息
    5. 一个剧本内容信息过多,不容易进行阅读,如何进行拆分 OK

    第一个历程: 规范目录结构

    cd /etc/ansible/roles
    mkdir {rsync,nfs}   --- 创建相应角色目录
    mkdir {nfs,rsync}/{vars,tasks,templates,handlers,files}  --- 创建角色目录下面的子目录
    [root@m01 roles]# tree 
      .
      ├── nfs
      │   ├── files       --- 保存需要分发文件目录 
      │   ├── handlers	--- 保存触发器配置文件信息
      │   ├── tasks       --- 保存要执行的动作信息文件   ok
      │   ├── templates   --- 保存需要分发模板文件,模板文件中设置变量信息,代替copy使用
      │   └── vars        --- 保存变量信息文件
      └── rsync
          ├── files
          ├── handlers
          ├── tasks
          ├── templates
          └── vars
    

    第二个历程: 在roles目录中创建相关文件

    一般编写文件顺序:tasks --> vars --> files --> handlers
    以nfs为例,roles目录中每个子目录都有main.yaml,写完的结构如下:

    └── roles
        ├── nfs
        │   ├── files
        │   │   └── exports
        │   ├── handlers
        │   │   └── main.yaml
        │   ├── tasks
        │   │   └── main.yaml
        │   ├── templates
        │   └── vars
        │       └── main.yaml
    

    以nfs为例:cd /etc/ansible/roles/nfs/tasks

    1. 编写tasks目录中的main.yaml文件
    - name: 01-copy conf file
     copy: src=exports dest=/etc
     notify: restart nfs server
    - name: 02-create data dir
     file: path={{ Data_dir }} state=directory owner=nfsnobody group=nfsnobody   
    - name: 03-boot server
     service: name={{ item }} state=started enabled=yes
     with_items:
       - rpcbind
       - nfs
    

    若有多个主机时,需要拆分,如将roles/nfs,拆为/roles/nfs_server,roles/nfs_client
    上面的tasks中的内容多次被使用时,也可以进行拆分,用main.yaml整合使用,使用与大规模的架构部署
    vim main.yml

    - include_tasks: copy_info.yml
    - include_tasks: create_dir.yml
    - include_tasks: boot_server.yml
    

    vim copy_info.yml

    - name: 01-copy conf file
      copy: src=exports dest=/etc
      notify: restart nfs server
    

    vim create_dir.yml

    - name: 02-create data dir
      file: path={{ Data_dir }} state=directory owner=nfsnobody group=nfsnobody 
    

    vim boot_server.yml

    - name: 03-boot server
      service: name={{ item }} state=started enabled=yes
      with_items:
        - rpcbind
    	- nfs	
    
    1. 编写vars目录中的main.yml文件
      cd /etc/ansible/roles/nfs/vars
    [root@m01 vars]# vim main.yaml
    Data_dir: /data
    
    1. 编写files目录中的文件
      cd /etc/ansible/roles/nfs/files
    [root@m01 files]# echo "/data 172.16.1.0/24(rw,sync)" >exports
    [root@m01 files]# ll
    total 4
    -rw-r--r-- 1 root root 29 May 17 15:23 exports
    
    1. 编写handlers目录中的main.ayml文件
      cd /etc/ansible/roles/nfs/handlers
      vim main.yaml
    - name: restart nfs server
    service: name=nfs state=restarted
    

    第三个历程: 编写一个主剧本文件

    [root@m01 roles]# cd /etc/ansible/roles
    [root@m01 roles]# cat site.yaml

    - hosts: nfs_server
      roles:
        - nfs-server
    
    - hosts: rsync_server
      roles:
        - rsync
    

    扩展了解:

    1. ansible剧本扩展功能进行总结

    2. (选做) 总结ansible剧本roles

    3. 预习web服务 http协议原理 nginx(安装 配置)

    4. 一键化部署全网备份项目

    5. 一键化部署实时同步服务

  • 相关阅读:
    Python——str(字符串)内部功能介绍
    Python网络编程——设定并获取默认的套接字超时时间
    Python网络编程——主机字节序和网络字节序之间的相互转换
    Python网络编程——通过指定的端口和协议找到服务名
    python网络编程——将IPv4地址转换成不同的格式
    Python网络编程——获取远程设备的IP地址
    Python网络编程——设备名和IPv4地址
    Python+Flask+MysqL的web建设技术过程
    管理信息系统 第三部分 作业
    模型分离(选做)
  • 原文地址:https://www.cnblogs.com/moox/p/12682236.html
Copyright © 2011-2022 走看看