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

file:獲取文件內(nèi)容


---
- 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的隨機(jī)密碼創(chuàng)建一個mysql用戶:
    - mysql_user: name={{ client }}
                  password="{{ lookup('password', '/tmp/passwordfile chars=ascii_letters length=8') }}"
                  priv={{ client }}_{{ tier }}_{{ role }}.*:ALL
# 使用只有數(shù)字的隨機(jī)密碼創(chuàng)建一個mysql用戶:
    - mysql_user: name={{ client }}
                  password="{{ lookup('password', '/tmp/passwordfile chars=digits') }}"
                  priv={{ client }}_{{ tier }}_{{ role }}.*:ALL
# 使用許多不同的字符集使用隨機(jī)密碼創(chuàng)建一個mysql用戶:
    - mysql_user: name={{ client }}
                  password="{{ lookup('password', '/tmp/passwordfile chars=ascii_letters,digits,hexdigits,punctuation') }}"
                  priv={{ client }}_{{ tier }}_{{ role }}.*:ALL

如果文件已存在吕粗,則不會向其寫入任何數(shù)據(jù)互婿。 如果文件有內(nèi)容释移,那些內(nèi)容將作為密碼讀入叭披。 空文件導(dǎo)致密碼以空字符串返回。

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') }}"

參數(shù) 默認(rèn)值 描述
file ansible.csv 要加載的文件名稱
col 1 要輸出的列玩讳,索引從0開始
delimiter TAB 文件的分隔符
default empty string 如果key不在csv文件中涩蜘,則為默認(rèn)返回值
encoding utf-8 使用的CSV文件的編碼(字符集)(added in version 2.1)

ini :讀取ini文件


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

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 參數(shù)格式

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

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

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

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_retrieve') }}

更多文章請看 Ansible 專題文章總覽

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末樟澜,一起剝皮案震驚了整個濱河市误窖,隨后出現(xiàn)的幾起案子叮盘,更是在濱河造成了極大的恐慌,老刑警劉巖霹俺,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件柔吼,死亡現(xiàn)場離奇詭異,居然都是意外死亡丙唧,警方通過查閱死者的電腦和手機(jī)愈魏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來想际,“玉大人培漏,你說我怎么就攤上這事≌恿穑” “怎么了北苟?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長打瘪。 經(jīng)常有香客問我友鼻,道長,這世上最難降的妖魔是什么闺骚? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任彩扔,我火速辦了婚禮,結(jié)果婚禮上僻爽,老公的妹妹穿的比我還像新娘虫碉。我一直安慰自己,他們只是感情好胸梆,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布敦捧。 她就那樣靜靜地躺著,像睡著了一般碰镜。 火紅的嫁衣襯著肌膚如雪兢卵。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天绪颖,我揣著相機(jī)與錄音秽荤,去河邊找鬼。 笑死柠横,一個胖子當(dāng)著我的面吹牛窃款,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播牍氛,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼晨继,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了搬俊?” 一聲冷哼從身側(cè)響起紊扬,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤曲饱,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后珠月,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡楔敌,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年啤挎,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片卵凑。...
    茶點(diǎn)故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡庆聘,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出勺卢,到底是詐尸還是另有隱情伙判,我是刑警寧澤,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布黑忱,位于F島的核電站宴抚,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏甫煞。R本人自食惡果不足惜菇曲,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一癣猾、第九天 我趴在偏房一處隱蔽的房頂上張望纬向。 院中可真熱鬧缤剧,春花似錦衷快、人聲如沸释簿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽兽狭。三九已至萧朝,卻和暖如春岔留,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背剪勿。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工贸诚, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人厕吉。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓酱固,卻偏偏與公主長得像,于是被迫代替她去往敵國和親头朱。 傳聞我的和親對象是個殘疾皇子运悲,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,037評論 2 355

推薦閱讀更多精彩內(nèi)容