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("\n") %} {{ 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("\n") %} {{ 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("\n") %} {{ 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
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号