美文网首页Linux经验分享
ansible剧本编写扩展功能

ansible剧本编写扩展功能

作者: Jzyue | 来源:发表于2019-07-06 12:07 被阅读0次

    1、剧本编写循环功能

    实现一个模块干多件事情;
    方法一:

    - hosts: 172.16.1.7
      tasks:
        - name: creat user01-10
          user: name={{ item }} shell=/sbin/nologin create_home=no
          with_items:
            - user01
            - user02
            - user03
    

    方法二:

        - hosts: 172.16.1.7
          tasks:
            - name: create user01-03
              user: name={{ item.name }}  shell={{ item.shell }} create_home={{ item.home }}
              with_items:
                - {name: 'user01', shell: '/sbin/nologin', home: 'yes'}
                - {name: 'user02', shell: '/bin/bash', home: 'no'}
                - {name: 'user03', shell: '/bin/bash', home: 'yes'}
    

    2、剧本编写判断功能

        - hosts: rsync
          tasks:
            - name: create password file
              copy: content='rsync_backup:oldboy123' dest=/etc/rsync.password mode=600
              when: (ansible_hostname == "backup")
            - name: create password file for rsync_client
              copy: content='oldboy123' dest=/etc/rsync.password mode=600
              when: (ansible_hostname != "backup")
    

    3、ansible常见主机信息

    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个数(只显示总的个数)。

    4、忽略错误

    因前面脚本出现错误,后续脚本也不会执行,所以要学会忽略错误,便于调试。

    - hosts: 172.16.1.7
      tasks:
       - name: create user
         shell: useradd oldboy100
         ignore_errors: yes
       - name: copy file
         copy: src=/etc/hosts  dest=/tmp
       - name: file attr
         file: path=/etc/hosts mode=600
    

    5、标签功能

    针对某个点进行执行操作,单独执行错误的操作。
    ansible-playbook -t test01 test_标签功能.yaml

    - hosts: 172.16.1.7
      tasks:
        - name: create user
          shell: useradd oldboy100
          ignore_errors: yes
          tags: test01
        - name: copy file
          copy: src=/etc/hosts  dest=/tmp
        - name: file attr
          file: path=/etc/hosts mode=600
    

    6、忽略采集功能

    用于管理多台主机的提升执行效率,忽略收集过程。

    - hosts: 172.16.1.7
      gather_facts: no      --- 省略主机信息收集过程/确保是否还需要使用判断功能
      tasks:
        - name: create user
          shell: useradd oldboy100
          ignore_errors: yes
          tags: test01
        - name: copy file
          copy: src=/etc/hosts  dest=/tmp
        - name: file attr
          file: path=/etc/hosts mode=600
    

    7、剧本触发功能

    - hosts: 172.16.1.41
          tasks:
            - name: copy file
              copy: src=rsyncd.conf  dest=/etc/
              notify: rsync_server
            - name: boot server
              service: name=rsyncd state=started
        
          handlers:
            - name: rsync_server
              service: name=rsyncd state=restarted
    

    8、将剧本进行整合

    方式一:include_tasks: f1.yml

    • hosts: all
      remote_user: root
      tasks:
      • include_tasks: f1.yml
      • include_tasks: f2.yml

    实践操作:

    • hosts: 172.16.1.41
      tasks:
      • include_tasks: rsync_auto.yaml
    • hosts: 172.16.1.31
      tasks:
      • include_tasks: nfs_auto.yaml

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

    相关文章

      网友评论

        本文标题:ansible剧本编写扩展功能

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