playbook lookups

Posted jcici

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了playbook lookups相关的知识,希望对你有一定的参考价值。

Ansible playbook lookups

1、lookups file

读取文件内容

[[email protected] 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 %}}"
[[email protected] 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

传入内容进行加密

[[email protected] 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 }}"
      
[[email protected] 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执行命令,然后将命令传递给变量

[[email protected] 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 %}"
[[email protected] 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模板文件

[[email protected] lookups]$ cat lookup.j2 
worker_processes {{ ansible_processor_cores }};
IPaddress {{ ansible_ens192.ipv4.address }}
[[email protected] 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 %}"
[[email protected] 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

 

以上是关于playbook lookups的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp kstat_lookup和kstat_read用法的示例kstat片段

Jenkins中的Ansible Playbook错误

3 ansible-playbook 条件语句-外部变量使用

从jenkinsfile启动包含vault文件引用的Ansible playbook

限制组从playbook源代码运行播放

ansible plugins简介