ansible playbook lookups組件

1.lookups file
支持從外部數據拉取信息,例如從數據庫里拉取信息,而後定義給一個變量的形式python

  • hosts: all
    gather_facts: true
    vars:
    contents: "{{ lookup('file', '/etc/sysconfig/network') }}"
    tasks:
    • name: debug lookups
      debug: msg="The contents is {% for i in contents.split("\n") %} {{ i }} {% endfor %}"
      結果:
      PLAY [all] ****

TASK [Gathering Facts] ****
ok: [192.168.1.1]redis

TASK [debug lookups] **
**ok: [192.168.1.1] => {
"msg": "The contents is # Created by anaconda NETWORKING_IPV6=no PEERNTP=no "
}數據庫

PLAY RECAP *
192.168.1.1 : ok=2 changed=0 unreachable=0 failed=0 **ide

2.lookups password對傳入的內容進行加密處理加密

  • hosts: all
    gather_facts: False
    vars:
    contents: "{{ lookup('password', 'ansible_book') }}"
    tasks:
    • name: debug lookups
      debug: msg="The contents is {{ contents }}"

3.lookups pipe
pipe lookups控制機上調用subprocess.Popen執行命令,而後將命令的結果傳遞給變量debug

  • 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 %}"
      結果:
      TASK [debug lookups]
      ok: [47.100.16.187] => {
      "msg": "The contents is 2018-02-03 "
      }

4.lookups redis_kv
redis_kv就是從redis數據庫中get數據,須要安裝redis python庫:yum install python-redis
ansible playbook lookups組件
ansible playbook lookups組件3d

5.lookups template
讀取文件template.j2,裏面定義的變量是每臺主機的fatcs信息
worker_processes {{ ansible_processor_cores }};
IPaddress {{ ansible_eth0.ipv4.address }}
ansible playbook lookups組件
ansible playbook lookups組件blog

相關文章
相關標籤/搜索