zoukankan      html  css  js  c++  java
  • 9.ansible 循环功能和忽略错误

    在剧本中设置循环信息
    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
    

    实践配置:
    [root@m01 ansible_playbook]# cat test_循环功能.yaml

    - hosts: 172.16.1.7
      tasks:
        #- name: 创建多个用户
        #  user: name={{ item.name }} shell={{ item.shell }} create_home={{ item.create_home }}
        #  with_items:
        #    - { name: 'oldboy01', shell: '/sbin/nologin', create_home: 'no' }
        #    - { name: 'oldboy02', shell: '/sbin/nologin', create_home: 'yes' }
        #    - { name: 'oldboy03', shell: '/bin/bash', create_home: 'yes' }
        - name: 创建多个用户
          user: name={{ item }} shell=/sbin/nologin create_home=no
          with_items:
            - oldboy04
            - oldboy05
            - oldboy06
    

    扩展说明:单独加载指定主机清单文件

    ansible-playbook nfs_auto.yaml -i ./nfs_hosts
    	-i  指定加载主机清单
    

    在剧本中设置忽略错误
    默认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	
    
    实践配置:
    
        [root@m01 ansible_playbook]# cat test_忽略错误.yaml 
        - hosts: 172.16.1.7
          tasks:
            - name: create user
              shell: useradd oldboy01 -s /sbin/nologin -M
              ignore_errors: yes
            - name: create dir
              shell: mkdir /oldboy
              ignore_errors: yes
            - name: create file
              shell: touch /etc/oldboy.txt	
    
  • 相关阅读:
    Ensp模拟OSPF与ACL综合应用
    ENSP配置NAT
    Ensp配置RSTP
    EnspOSPF单区域配置
    Ensp配置静态路由和默认路由
    NotPron国外版
    MSF
    转换流
    梦之光芒1-14关通关秘籍
    php序列化与反序列化
  • 原文地址:https://www.cnblogs.com/yangtao416/p/14595934.html
Copyright © 2011-2022 走看看