Ansible Loops
4700 ワード
一、紹介
通常、1つのタスクでは、大量のユーザーを作成したり、多くのパッケージをインストールしたり、特定のステップをポーリングしたりして、何らかの結果条件までAnsibleがサポートしてくれます.ループに関するいくつかの文を次に示します.
with_items #ひょうじゅんサイクル
with_nested #ネストされたループ
with_dict #辞書ループ(Looping over hashed)
with_file #ループファイル
with_fileglob #ワイルドカード・ループ・ファイル
with_together #ループマッピング
with_subelements #サブエレメントループ
with_sequence #一連の数値を生成
with_random_choice #任意選択
#with_first_found #最初に見つけたものを選んで、使いません.
with_lines #ループプログラム実行の出力情報with_items:`command_result`.`stdout_lines`
with_flattened #ループリストのリストのitem
二、実例
通常、1つのタスクでは、大量のユーザーを作成したり、多くのパッケージをインストールしたり、特定のステップをポーリングしたりして、何らかの結果条件までAnsibleがサポートしてくれます.ループに関するいくつかの文を次に示します.
with_items #ひょうじゅんサイクル
with_nested #ネストされたループ
with_dict #辞書ループ(Looping over hashed)
with_file #ループファイル
with_fileglob #ワイルドカード・ループ・ファイル
with_together #ループマッピング
with_subelements #サブエレメントループ
with_sequence #一連の数値を生成
with_random_choice #任意選択
#with_first_found #最初に見つけたものを選んで、使いません.
with_lines #ループプログラム実行の出力情報with_items:`command_result`.`stdout_lines`
with_flattened #ループリストのリストのitem
二、実例
---
- hosts: local #with_item with_nested
vars:
Sta: false
User: [ "Aheahe","yunzhonghe" ]
tasks:
- name: Standard Loops
debug: msg="{{ item.name }},{{ item.fun }}"
with_items: #
- { name: Loops, fun: xx }
- { name: Conditional, fun: yy }
when: Sta
- name: Nested Loops
debug: msg= "name={{ item[0] }},priv={{ item[1] }}"
with_nested: #
- "{{ User }}" # User 。
- [ "Student","Engineer","Friends" ]
---
- hosts: local #with_dict
# vars_files:
# hash.yml
vars:
users:
alice:
name: Alice Appleworth
telephone: 123-456-7890
bob:
name: Bob Bananarama
telephone: 987-654-3210
tasks:
- name: Print phone records
debug: msg="User item.key is item.value.name item.value.telephone "
with_dict: users # 。
---
- hosts: local #with_file with_fileglob 。
vars:
test_file: false
tasks:
- name: test file
shell: echo {{ item }}
with_file: #
- test #test 。
- test1
when: test_file
- name: Test file Glob
shell: echo {{ item }}
with_fileglob: # 。
- /home/aheahe/playbook/*
---
- hosts: local #with_together
vars:
alpha: [ 'a', 'b', 'c', 'd' ]
numbers: [ 1, 2, 3, 4 ]
test: [ 'A', 'B', 'C', 'D' ]
tasks:
- debug: msg="{{ item.0 }} and {{ item.1 }} and {{ item.2 }}"
with_together: #
- "`alpha`"
- "`numbers`"
- "`test`"
---
- hosts: local #with_subelement
vars:
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"
tasks:
- name: Test1
shell: echo "name={{ item.name }}"
with_items: "`users`"
- name: Test2
shell: echo "User={{ item.0.name }},key={{ item.1 }}"
with_subelements: # 。
- users
- authorized
---
- hosts: local
tasks:
- name: Learn loop sequence
debug: msg={{ item }}
with_sequence: start=0 end=6 stride=2
---
- hosts: local # with_random_choice
tasks:
- name: Learn Random
debug: msg={{ item }}
with_random_choice:
- "go through the door"
- "drink from the goblet"
- "press the red button"
- "do nothing"
---
- hosts: local #with_lines
tasks:
- name: test result
shell: echo {{ item }}
with_lines: /bin/df -Th
- name: flattened loop demo #with_flatted
yum: name={{ item }} state=installed
with_flattened:
- "`packages_base`"
- "`packages_apps`"
----
# file: roles/foo/vars/main.yml
packages_base:
- [ 'foo-package', 'bar-package' ]
packages_apps:
- [ ['one-package', 'two-package' ]]
- [ ['red-package'], ['blue-package']]