zoukankan      html  css  js  c++  java
  • playbook lookups

    Ansible playbook lookups

    1、lookups file

    读取文件内容

    [ansible@hk-elk-redis1 lookups]$ cat lookups.yaml 
    ---
      - hosts: all
        gather_facts: False
        vars:
          contents: "{{ lookup('file','/etc/sysconfig/network') }}"
        tasks:
          - name: debug lookups
            debug: msg="The contens is {% for i in contents.split("
    ") %} {{ i }} {% endfor %}}"
    [ansible@hk-elk-redis1 lookups]$ ansible-playbook lookups.yaml  -l 10.20.11.201
    
    PLAY [all] *******************************************************************************************************************************************************************************************************************************************************************
    
    TASK [debug lookups] *********************************************************************************************************************************************************************************************************************************************************
    ok: [10.20.11.201] => {
        "msg": "The contens is  # Created by anaconda }"
    }
    
    PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
    10.20.11.201               : ok=1    changed=0    unreachable=0    failed=0
    

     2、lookups password

    传入内容进行加密

    [ansible@hk-elk-redis1 lookups]$ cat lookup_pass.yaml 
    ---
      - hosts: all
        gather_facts: False
        vars:
          contents: "{{ lookup('password','ansible_book') }}"
        tasks:
          - name: debug lookups
            debug: msg="The contents is {{ contents }}"
          
    [ansible@hk-elk-redis1 lookups]$ ansible-playbook lookup_pass.yaml -l 10.20.11.201
    
    PLAY [all] *******************************************************************************************************************************************************************************************************************************************************************
    
    TASK [debug lookups] *********************************************************************************************************************************************************************************************************************************************************
    ok: [10.20.11.201] => {
        "msg": "The contents is :ny4_i5BAyMrOhmm8-tT"
    }
    
    PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
    10.20.11.201               : ok=1    changed=0    unreachable=0    failed=0
    

    3、lookups pipe

    pipe lookups的实现原理很简单,就是在机器上面调用subprocess.Popen执行命令,然后将命令传递给变量

    [ansible@hk-elk-redis1 lookups]$ cat lookup_pipe.yaml 
    ---
      - hosts: all
        gather_facts: False
        vars:
          contents: "{{ lookup('pipe','date +%Y-%m-%d') }}"
        tasks:
          - name: debug lookups
            debug: msg="The contents is {% for i in contents.split("
    ") %} {{ i }} {% endfor %}"
    [ansible@hk-elk-redis1 lookups]$ ansible-playbook lookup_pipe.yaml -l 10.20.11.201
    
    PLAY [all] *******************************************************************************************************************************************************************************************************************************************************************
    
    TASK [debug lookups] *********************************************************************************************************************************************************************************************************************************************************
    ok: [10.20.11.201] => {
        "msg": "The contents is  2018-12-20 "
    }
    
    PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
    10.20.11.201               : ok=1    changed=0    unreachable=0    failed=0 
    

     4、lookups template

    template跟file的方式有点类似,都是读取文件,但是template在读取文件之前需要吧jinja模板渲染完后再读取,下面我们指定一个jinja模板文件

    [ansible@hk-elk-redis1 lookups]$ cat lookup.j2 
    worker_processes {{ ansible_processor_cores }};
    IPaddress {{ ansible_ens192.ipv4.address }}
    [ansible@hk-elk-redis1 lookups]$ cat lookup_template.yaml 
    ---
      - hosts: all
        gather_facts: True
        vars:
          contents: "{{ lookup('template','./lookup.j2') }}"
        tasks:
          - name: debug lookups
            debug: msg="The contents is {% for i in contents.split("
    ") %} {{ i }} {% endfor %}"
    [ansible@hk-elk-redis1 lookups]$ ansible-playbook lookup_template.yaml 
    
    PLAY [all] *******************************************************************************************************************************************************************************************************************************************************************
    
    TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
    ok: [10.20.11.202]
    ok: [10.20.11.201]
    
    TASK [debug lookups] *********************************************************************************************************************************************************************************************************************************************************
    ok: [10.20.11.201] => {
        "msg": "The contents is  worker_processes 1;  IPaddress 10.20.11.201   "
    }
    ok: [10.20.11.202] => {
        "msg": "The contents is  worker_processes 1;  IPaddress 10.20.11.202   "
    }
    
    PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
    10.20.11.201               : ok=2    changed=0    unreachable=0    failed=0   
    10.20.11.202               : ok=2    changed=0    unreachable=0    failed=0
    
  • 相关阅读:
    Nodejs的下载和安装以及环境配置
    java的JDK的安装和环境变量配置
    CSS3与页面布局——概要、选择器、特殊性与刻度单位
    angular2.x 多选框事件
    NiceFish的ERROR in AppModule is not an NgModule问题
    angularjs分页组件
    获取当前日期
    angularjs实现星星评分
    angularjs1 自定义轮播图(汉字导航)
    获取未来几天的日期以及是星期几
  • 原文地址:https://www.cnblogs.com/jcici/p/10150565.html
Copyright © 2011-2022 走看看