zoukankan      html  css  js  c++  java
  • ansible使用6-Conditionals

    when

    tasks:
      - name: "shutdown Debian flavored systems"
        command: /sbin/shutdown -t now
        when: ansible_os_family == "Debian"
    
    tasks:
      - command: /bin/false
        register: result
        ignore_errors: True
      - command: /bin/something
        when: result|failed
      - command: /bin/something_else
        when: result|success
      - command: /bin/still/something_else
        when: result|skipped
    
    # fact
    ansible hostname.example.com -m setup
    
    tasks:
      - shell: echo "only on Red Hat 6, derivatives, and later"
        when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int >= 6
    
    # 变量
    vars:
      epic: true
    
    tasks:
        - shell: echo "This certainly is epic!"
          when: epic
    
    tasks:
        - shell: echo "This certainly isn't epic!"
          when: not epic
    
    # jinja2模板
    tasks:
        - shell: echo "I've got '{{ foo }}' and am not afraid to use it!"
          when: foo is defined
    
        - fail: msg="Bailing out. this play requires 'bar'"
          when: bar is not defined
    
    # loop
    tasks:
        - command: echo {{ item }}
          with_items: [ 0, 2, 4, 6, 8, 10 ]
          when: item > 5
    

    Loading in Custom Facts

    tasks:
        - name: gather site specific fact data
          action: site_facts
        - command: /usr/bin/thingy
          when: my_custom_fact_just_retrieved_from_the_remote_system == '1234'
    

    Applying ‘when’ to roles and includes

    - include: tasks/sometasks.yml
      when: "'reticulating splines' in output"
    
    - hosts: webservers
      roles:
         - { role: debian_stock_config, when: ansible_os_family == 'Debian' }
    

    Conditional Imports

    ---
    - hosts: all
      remote_user: root
      vars_files:
        - "vars/common.yml"
        - [ "vars/{{ ansible_os_family }}.yml", "vars/os_defaults.yml" ]
      tasks:
      - name: make sure apache is running
        service: name={{ apache }} state=running
    
    ---
    # for vars/CentOS.yml
    apache: httpd
    somethingelse: 42
    
    # for facter
    ansible -m yum -a "pkg=facter ensure=installed"
    ansible -m yum -a "pkg=ruby-json ensure=installed"
    
    # for ohai
    ansible -m yum -a "pkg=ohai ensure=installed"
    

    Selecting Files And Templates Based On Variables

    - name: template a file
      template: src={{ item }} dest=/etc/myapp/foo.conf
      with_first_found:
        - files:
           - {{ ansible_distribution }}.conf
           - default.conf
          paths:
           - search_location_one/somedir/
           - /opt/other_location/somedir/
    

    Register Variables

    - name: test play
      hosts: all
      tasks:
          - shell: cat /etc/motd
            register: motd_contents
    
          - shell: echo "motd contains the word hi"
            when: motd_contents.stdout.find('hi') != -1
    
    - name: registered variable usage as a with_items list
      hosts: all
      tasks:
          - name: retrieve the list of home directories
            command: ls /home
            register: home_dirs
    
          - name: add home dirs to the backup spooler
            file: path=/mnt/bkspool/{{ item }} src=/home/{{ item }} state=link
            with_items: home_dirs.stdout_lines
            # same as with_items: home_dirs.stdout.split()
    
  • 相关阅读:
    神代码
    初读《代码大全》
    单词频度统计
    AFO
    bzoj4816: [Sdoi2017]数字表格
    bzoj4006: [JLOI2015]管道连接
    bzoj4774: 修路
    bzoj3209: 花神的数论题
    bzoj4521: [Cqoi2016]手机号码
    COGS2314. [HZOI 2015] Persistable Editor
  • 原文地址:https://www.cnblogs.com/liujitao79/p/4201196.html
Copyright © 2011-2022 走看看