剧本编写汇总

利用剧本完成服务一键化部署:
    rsync    服务部署
    nfs        服务部署
    sersync 服务部署 
    全网备份项目
   

一、rsync服务剧本编写:


    准备工作:
    01. 熟悉软件部署流程
    02. 熟悉ansible软件模块使用
    03. 熟悉ansible剧本编写规范
    ansible:
    ad-hoc    临时实现批量管理功能(模块)   --- 命令
    playbook  永久实现批量管理功能(剧本)   --- 脚本    

    剧本编写常见错误:
    01. 剧本语法规范是否符合(空格 冒号 短横线)
    02. 剧本中模块使用是否正确
    03. 剧本中一个name标识下面只能写一个模块任务信息
    04. 剧本中尽量不要大量使用shell模块
    

    [root@m01 ansible-playbook]# cat rsync_server.yaml 
- hosts: rsync_server
  tasks:
    - name: 01-install rsync  
      yum: name=rsync state=installed
    - name: 02-push conf file    
      copy: src=/etc/ansible/server_file/rsync_server/rsyncd.conf dest=/etc/
    - name: 03-create user
      user: name=rsync create_home=no shell=/sbin/nologin
      #shell: useradd rsync -M -s /sbin/nologin 
    - name: 04-create backup dir
      file: path=/backup state=directory owner=rsync group=rsync
    - name: 05-create password file
      copy: content=rsync_backup:oldboy123 dest=/etc/rsync.password mode=600
    - name: 06-start rsync server
      service: name=rsyncd state=started enabled=yes

- hosts: rsync_clients
  tasks:
    - name: 01-install rsync
      yum: name=rsync state=installed
    - name: 02-create password file
      copy: content=oldboy123 dest=/etc/rsync.password mode=600
    - name: 03-create test file
      file: dest=/tmp/test.txt  state=touch
    - name: 04-check test
      shell: rsync -avz /tmp/test.txt rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password

 

二、 如何配置主机清单


    第一种方式: 分组配置主机信息
    [web]
    172.16.1.7
    172.16.1.8
    172.16.1.9
    
    [data]
    172.16.1.31
    172.16.1.41
    操作过程
    [root@m01 ansible-playbook]# ansible data -a "hostname"
    172.16.1.31 | CHANGED | rc=0 >>
    nfs01
    
    172.16.1.41 | CHANGED | rc=0 >>
    backup
    
    [root@m01 ansible-playbook]# ansible web -a "hostname"
    172.16.1.7 | CHANGED | rc=0 >>
    web01
    
    第二种方式: 主机名符号匹配配置
    [web]
    172.16.1.[7:9]
    [web]
    web[01:03]
    
    第三种方式: 跟上非标准远程端口
    [web]
    web01:52113
    172.16.1.7:52113
    
    第四种方式: 主机使用特殊的变量
    [web]
    172.16.1.7 ansible_ssh_port=52113 ansible_ssh_user=root ansible_ssh_pass=123456
    [web]
    web01 ansible_ssh_host=172.16.1.7 ansible_ssh_port=52113 ansible_ssh_user=root ansible_ssh_pass=123456

    第五种方式: 主机组名嵌入配置
    [rsync:children]    --- 嵌入子组信息
    rsync_server
    rsync_client
    
    [rsync_server]
    172.16.1.41
    
    [rsync_client]
    172.16.1.31
    172.16.1.7
    
    [root@m01 /etc/ansible]# ansible rsync -a "hostname"
    172.16.1.7 | CHANGED | rc=0 >>
    web01.com
    172.16.1.41 | CHANGED | rc=0 >>
    backup
    172.16.1.31 | CHANGED | rc=0 >>
    nfs01

    
    [web:vars]         --- 嵌入式变量信息
    ansible_ssh_host=172.16.1.7
    ansible_ssh_port=52113
    ansible_ssh_user=root
    ansible_ssh_pass=123456
    [web]
    web01

    主机清单的配置方法:
    https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html

 

三、在剧本中设置变量信息


    方式一:直接在剧本文件编写  
    vars:
      oldboy01: data01
      oldboy02: data02
 
    方式二:在命令行中进行指定

    ansible-playbook --extra-vars=oldboy01=data01

    方式三:在主机清单文件编写
    [oldboy]
    oldboy01=data01
    oldboy02=data02

    三种变量设置方式都配置了,三种方式的优先级???
    最优先: 命令行变量设置
    次优先: 剧本中变量设置
    最后:   主机清单变量设置

    如何全局设置变量: roles 剧本整合
    


    
四、在剧本中设置注册信息


    - 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: 设置变量不能有空格信息


          
五、 在剧本中设置判断信息


    如何指定判断条件:
    (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]

 

六、在剧本中设置循环信息


    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    

 

七、在剧本中设置忽略错误


    默认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    

    

八、 在剧本中设置标签功能


    - 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      

   

九、 在剧本中设置触发功能


    - 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   

 

十、将多个剧本进行整合

 

    方式一: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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值