Ansible的循環
1、 前言
有可能在一個任務中,可能要做很多事情,例如創建多個用戶,安裝很多個包等,那么就有可能用到循環。
2、 標准循環
重復的任務可以用下面的方式:
- name: add several users user: name={{ item }} state=present groups=wheel with_items: - testuser1 - testuser2
|
如果你定義了一個變量文件創建了一個YAML的列表,或者是在var節中,那么可以使用以下的方式:
with_items:"{{somelist}}"
|
上面的和下面的表示為相同的含義,如下:
-name:add user testuser1 user:name=testuser1 state=present groups=wheel -name:add user testuser2 user:name=testuser2 state=present groups=wheel
|
Yum和apt模塊在使用with_items的時候,可以減少包管理事務。
在遍歷的各項中,可以不是簡單的列表,如果是hash列表的時候,你可以使用子健,如下:
-name:add several users user:name={{ item.name }} state=present groups={{ item.groups }} with_items: -{name:'testuser1',groups:'wheel'} -{name:'testuser2',groups:'root'}
|
3、 嵌套循環
嵌套循環如下所示:
-name:give users access to multiple databases mysql_user:name={{ item[0] }} priv={{ item[1] }}.*:ALL append_privs=yes password=foo with_nested: -['alice','bob'] -['clientdb','employeedb','providerdb']
|
在上面的with_items中,也可以使用以前定義的變量,如下所示:
-name:here, 'users' contains the above list of employees mysql_user:name={{ item[0] }} priv={{ item[1] }}.*:ALL append_privs=yes password=foo with_nested: -"{{users}}" -['clientdb','employeedb','providerdb']
|
4、 遍歷hash
假設存在以下變量:
--- users: alice: name:Alice Appleworth telephone:123-456-7890 bob: name:Bob Bananarama telephone:987-654-3210
|
你要打印每個用戶的名稱和手機號碼,那么可以用循環的關鍵字“with_dict”,如下:
tasks: -name:Print phone records debug:msg="User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})" with_dict:"{{users}}"
|
5、 遍歷文件
“with_file”迭代建立在一系列文件的內容之上,item將被設置為序列中每個文件的內容,如下所示:
--- -hosts:all tasks: # emit a debug message containing the content of each file. -debug: msg:"{{item}}" with_file: -first_example_file -second_example_file
|
假設first_example_file包含語句“hello”並且second_example_file包含文本“world”,那么如下所示:
TASK [debug msg={{item}}] ****************************************************** ok: [localhost] => (item=hello) => { "item": "hello", "msg": "hello" } ok: [localhost] => (item=world) => { "item": "world", "msg": "world" }
|
6、 遍歷文件夾(fileglobs)
with_fileglob對應於單一目錄下的所有文件,不遞歸的匹配模式,如下:
--- -hosts:all tasks: # first ensure our target directory exists -file:dest=/etc/fooapp state=directory # copy each file over that matches the given pattern -copy:src={{ item }} dest=/etc/fooapp/ owner=root mode=600 with_fileglob: -/playbooks/files/fooapp/*
|
注意:當在role里使用with_fileglob的時候,ansible會將路徑解析為
roles/<rolename>/files 路徑
7、 遍歷數據的並行集
假設存在以下的變量:
--- alpha:['a','b','c','d'] numbers:[1,2,3,4]
|
並且你想將結果合並為(a,1),(b,2)的樣子,那么使用關鍵字with_together,如下:
tasks: -debug:msg="{{ item.0 }} and {{ item.1 }}" with_together: -"{{alpha}}" -"{{numbers}}"
|
8、 遍歷子元素
假設你想遍歷一個用戶列表,創建他們,並且用特定的ssh key來進行登錄,假設存在以下的變量內容:
--- users: -name:alice authorized: -/tmp/alice/onekey.pub -/tmp/alice/twokey.pub mysql: password:mysql-password hosts: -"%" -"127.0.0.1" -"::1" -"localhost" privs: -"*.*:SELECT" -"DB1.*:ALL" -name:bob authorized: -/tmp/bob/id_rsa.pub mysql: password:other-mysql-password hosts: -"db1" privs: -"*.*:SELECT" -"DB2.*:ALL"
|
那么將會像如下所示:
-user:name={{ item.name }} state=present generate_ssh_key=yes with_items:"{{users}}" -authorized_key:"user={{item.0.name}}key='{{lookup('file',item.1)}}'" with_subelements: -users -authorized
|
給出mysql的主機並且提供ssh密鑰的列表,那么將可以遍歷一個嵌套的子健:
-name:Setup MySQL users mysql_user:name={{ item.0.name }} password={{ item.0.mysql.password }} host={{ item.1 }} priv={{ item.0.mysql.privs | join('/') }} with_subelements: -users -mysql.hosts
|
子元素會沿着哈希表遍歷並且給出ssh密鑰在記錄之內
9、 遍歷整數系列
with_sequence生成項目數字的序列,也可以指定開始,結束,或者一個步長值。
參數也可以指定為key=value的格式,如果提供了,那么會格式化為printf的類型字符串。
數字值可以指定為2進制,10進制,16進制,負數不支持,如下:
--- -hosts:all tasks: # create groups -group:name=evens state=present -group:name=odds state=present # create some test users -user:name={{ item }} state=present groups=evens with_sequence:start=0 end=32 format=testuser%02x # create a series of directories with even numbers for some reason -file:dest=/var/stuff/{{ item }} state=directory with_sequence:start=4 end=16 stride=2 # a simpler way to use the sequence plugin # create 4 groups -group:name=group{{ item }} state=present with_sequence:count=4
|
10、 隨機選擇
random_choice表示為隨機的選擇,但是不是一個負載均衡,在有的程度上相當於負載均衡,如下:
-debug:msg={{ item }} with_random_choice: -"gothroughthedoor" -"drinkfromthegoblet" -"presstheredbutton" -"donothing"
|
提供的字符串將會有一個被隨機選中。
11、 Do-until循環
有的時候會去嘗試一個任務在特定的條件下,如下:
-action:shell /usr/bin/foo register:result until:result.stdout.find("all systems go") != -1 retries:5 delay:10
|
在默認情況下,retries值為3,delay為5秒,在查看的時候,可以使用-vv的選項來查看變量的值
12、 找到第一個匹配的文件
這不是一個循環,但是很接近。當你在引用一個文件的時候,取決於上一個文件的內容或者是名稱,那么可以如下:
-name:INTERFACES | Create Ansible header for /etc/network/interfaces template:src={{ item }} dest=/etc/foo.conf with_first_found: -"{{ansible_virtualization_type}}_foo.conf" -"default_foo.conf"
|
也有一個長形的版本,從而允許配置搜索的路徑:
-name:some configuration template template:src={{ item }} dest=/etc/file.cfg mode=0444 owner=root group=root with_first_found: -files: -"{{inventory_hostname}}/etc/file.cfg" paths: -../../../templates.overwrites -../../../templates -files: -etc/file.cfg paths: -templates
|
13、 迭代程序執行的結果
在有的時候,需要遍歷執行的結果,需要注意的是,這是在遠程機器上執行的,而不是在本機器上執行的,如下:
-name:Example of looping over a command result shell:/usr/bin/frobnicate {{ item }} with_lines:/usr/bin/frobnications_per_host --param {{ inventory_hostname }}
|
當需要遠程執行命令的時候,那么可以像以下方法做:
-name:Example of looping over a REMOTE command result shell:/usr/bin/something register:command_result -name:Do something with each result shell:/usr/bin/something_else --param {{ item }} with_items:"{{command_result.stdout_lines}}"
|
14、 在ini文件中遍歷
可以遍歷以下集合,如下:
[section1] value1=section1/value1 value2=section1/value2 [section2] value1=section2/value1 value2=section2/value2
|
如下演示使用with_ini:
-debug:msg="{{item}}" with_ini:value[1-2] section=section1 file=lookup.ini re=true
|
返回值如下所示:
{ "changed":false, "msg":"Allitemscompleted", "results":[ { "invocation":{ "module_args":"msg=\"section1/value1\"", "module_name":"debug" }, "item":"section1/value1", "msg":"section1/value1", "verbose_always":true }, { "invocation":{ "module_args":"msg=\"section1/value2\"", "module_name":"debug" }, "item":"section1/value2", "msg":"section1/value2", "verbose_always":true } ] }
|
15、 在循環中使用register
當使用register來存儲變量的時候,那么保存的是一個列表,整個屬性為results
如下:
-shell:echo "{{ item }}" with_items: -one -two register:echo
|
當使用register而不使用循環的時候返回值如下:
{ "changed":true, "msg":"Allitemscompleted", "results":[ { "changed":true, "cmd":"echo\"one\"", "delta":"0:00:00.003110", "end":"2013-12-1912:00:05.187153", "invocation":{ "module_args":"echo\"one\"", "module_name":"shell" }, "item":"one", "rc":0, "start":"2013-12-1912:00:05.184043", "stderr":"", "stdout":"one" }, { "changed":true, "cmd":"echo\"two\"", "delta":"0:00:00.002920", "end":"2013-12-1912:00:05.245502", "invocation":{ "module_args":"echo\"two\"", "module_name":"shell" }, "item":"two", "rc":0, "start":"2013-12-1912:00:05.242582", "stderr":"", "stdout":"two" } ] }
|
使用循環並使用register的結果如下:
-name:Fail if return code is not 0 fail: msg:"Thecommand({{item.cmd}})didnothavea0returncode" when:item.rc != 0 with_items:"{{echo.results}}"
|
16、 在inventory中使用循環
當你想在inventory中進行遍歷,或者是一個子集,那么可以如下所示:
# show all the hosts in the inventory -debug:msg={{ item }} with_items:"{{groups['all']}}" # show all the hosts in the current play -debug:msg={{ item }} with_items:play_hosts
|
而且可以使用插件inventory_hostname:
# show all the hosts in the inventory -debug:msg={{ item }} with_inventory_hostname:all # show all the hosts matching the pattern, ie all but the group www -debug:msg={{ item }} with_inventory_hostname:all:!www
|
17、 在include中使用循環
在include中使用循環如下:
(注意:在included的任務中含有自己的with_loop值,會覆蓋掉特定的item的值,如果既要使用include的item又要使用當前任務的item,必須使用set_fact去創建一個別名)
-include:test.yml with_items: -1 -2 -3
|
在test.yml中:
-set_fact:outer_loop="{{item}}" -debug:msg="outer item={{outer_loop}} inner item={{item}}" with_items: -a -b -c
|