Ansible 小手冊系列 十八(Lookup 插件)


file:獲取文件內容


---
- hosts: all
  vars:
     contents: "{{ lookup('file', '/etc/foo.txt') }}" tasks: - debug: msg="the value of foo.txt is {{ contents }}" 

password:生成密碼字符串


---
- hosts: all tasks: # 使用只有ascii字母且長度為8的隨機密碼創建一個mysql用戶: - mysql_user: name={{ client }} password="{{ lookup('password', '/tmp/passwordfile chars=ascii_letters length=8') }}" priv={{ client }}_{{ tier }}_{{ role }}.*:ALL # 使用只有數字的隨機密碼創建一個mysql用戶: - mysql_user: name={{ client }} password="{{ lookup('password', '/tmp/passwordfile chars=digits') }}" priv={{ client }}_{{ tier }}_{{ role }}.*:ALL # 使用許多不同的字符集使用隨機密碼創建一個mysql用戶: - mysql_user: name={{ client }} password="{{ lookup('password', '/tmp/passwordfile chars=ascii_letters,digits,hexdigits,punctuation') }}" priv={{ client }}_{{ tier }}_{{ role }}.*:ALL 

如果文件已存在,則不會向其寫入任何數據。 如果文件有內容,那些內容將作為密碼讀入。 空文件導致密碼以空字符串返回。

csvfile :讀取csv文件


f.csv Symbol,Atomic Number,Atomic Mass H,1,1.008 He,2,4.0026 Li,3,6.94 Be,4,9.012 B,5,10.81 
- debug: msg="The atomic number of Lithium is {{ lookup('csvfile', 'Li file=elements.csv delimiter=,') }}" - debug: msg="The atomic mass of Lithium is {{ lookup('csvfile', 'Li file=elements.csv delimiter=, col=2') }}" 
參數 默認值 描述
file ansible.csv 要加載的文件名稱
col 1 要輸出的列,索引從0開始
delimiter TAB 文件的分隔符
default empty string 如果key不在csv文件中,則為默認返回值
encoding utf-8 使用的CSV文件的編碼(字符集)(added in version 2.1)

ini :讀取ini文件


在section下查找以key1 = value1的格式來讀取文件的內容。

users.ini
[production]
# My production information user=robert pass=somerandompassword [integration] # My integration information user=gertrude pass=anotherpassword 
  tasks:
  - debug: msg="User in integration is {{ lookup('ini', 'user section=integration file=users.ini') }}" - debug: msg="User in production is {{ lookup('ini', 'pass section=production file=users.ini') }}" 

ini 參數格式

lookup('ini', 'key [type=<properties|ini>] [section=section] [file=file.ini] [re=true] [default=<defaultvalue>]') 

第一個值必須是ini文件里的key

字段 默認值 描述
type ini 文件類型。 可以是ini或properties (對於javaproperties )。
file ansible.ini 要加載的文件名稱
section global 在哪里查找key
re False 開啟正則匹配
default empty string 如果key不在文件中,則為默認返回值

Credstash :用於使用AWS的KMS和DynamoDB管理secrets


此模塊依賴credstash庫

dig:dns查詢


此模塊依賴dnspython 庫

- debug: msg="The IPv4 address for example.com. is {{ lookup('dig', 'example.com.')}}" - debug: msg="The TXT record for example.org. is {{ lookup('dig', 'example.org.', 'qtype=TXT') }}" - debug: msg="The TXT record for example.org. is {{ lookup('dig', 'example.org./TXT') }}" 

其他


tasks:
- debug: msg="{{ lookup('env','HOME') }} is an environment variable" - debug: msg="{{ lookup('pipe','date') }} is the raw result of running this command" # redis_kv lookup requires the Python redis package - debug: msg="{{ lookup('redis_kv', 'redis://localhost:6379,somekey') }} is value in Redis for somekey" # dnstxt lookup requires the Python dnspython package - debug: msg="{{ lookup('dnstxt', 'example.com') }} is a DNS TXT record for example.com" - debug: msg="{{ lookup('template', './some_template.j2') }} is a value from evaluation of this template" # loading a json file from a template as a string - debug: msg="{{ lookup('template', './some_json.json.j2', convert_data=False) }} is a value from evaluation of this template" - debug: msg="{{ lookup('etcd', 'foo') }} is a value from a locally running etcd" # shelvefile lookup retrieves a string value corresponding to a key inside a Python shelve file - debug: msg="{{ lookup('shelvefile', 'file=path_to_some_shelve_file.db key=key_to_





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM