本帖最后由 yun 于 2019-9-11 17:14 编辑
ansible是一个系列文章,我们会尽量以通俗易懂的方式总结ansible的相关知识点。 "ansible系列"中的每篇文章都建立在前文的基础之上,所以,请按照顺序阅读这些文章,否则有可能在阅读中遇到障碍。
老套路,今天继续来认识两个用于循环的关键字,它们是"with_dict"和"with_subelements"
先聊聊"with_dict",顾名思义,"with_dict"的使用场景应该与"字典"有关,我们先来看一个小例子,结合示例更加容易说明白,示例playbook如下: - ---
- - hosts: test70
- remote_user: root
- gather_facts: no
- vars:
- users:
- alice: female
- bob: male
- tasks:
- - debug:
- msg: "{{item}}"
- with_dict: "{{users}}"
复制代码在上例中,我们使用字典的方式定义了users变量,users中一共有两个用户,alice和bob,从变量的键值对可以看出,alice是女性,bob是男性,然后,我们使用"with_dict"关键字处理了这个字典格式的变量,那么我们来看一下执行效果,上例playbook执行后debug模块的输出信息如下: - TASK [debug] *************************************
- ok: [test70] => (item={'value': u'male', 'key': u'bob'}) => {
- "changed": false,
- "item": {
- "key": "bob",
- "value": "male"
- },
- "msg": {
- "key": "bob",
- "value": "male"
- }
- }
- ok: [test70] => (item={'value': u'female', 'key': u'alice'}) => {
- "changed": false,
- "item": {
- "key": "alice",
- "value": "female"
- },
- "msg": {
- "key": "alice",
- "value": "female"
- }
- }
复制代码从输出信息可以看出,字典格式的users变量经过"with_dict"处理后,字典中的每个键值对被放到了item变量中,而且,键值对中的"键"被放入了"key"关键字中,键值对中的"值"被放入了"value"关键字中,所以,我们可以通过key关键字和value关键字分别获取到字典中键值对的"键"和"值",示例playbook如下 - ---
- - hosts: test70
- remote_user: root
- gather_facts: no
- vars:
- users:
- alice: female
- bob: male
- tasks:
- - debug:
- msg: "User name: {{item.key}} , User's gender: {{item.value}} "
- with_dict: "{{users}}"
复制代码上例执行后debug模块输出如下: - TASK [debug] ************************************************
- ok: [test70] => (item={'value': u'male', 'key': u'bob'}) => {
- "changed": false,
- "item": {
- "key": "bob",
- "value": "male"
- },
- "msg": "User name: bob , User's gender: male "
- }
- ok: [test70] => (item={'value': u'female', 'key': u'alice'}) => {
- "changed": false,
- "item": {
- "key": "alice",
- "value": "female"
- },
- "msg": "User name: alice , User's gender: female "
- }
复制代码你一定已经明白了"with_dict"的作用,那么我们来扩展一下,将上例中字典格式的变量变得"丰富"一点,示例playbook如下: - ---
- - hosts: test70
- remote_user: root
- gather_facts: no
- vars:
- users:
- alice:
- name: Alice Appleworth
- gender: female
- telephone: 123-456-7890
- bob:
- name: Bob Bananarama
- gender: male
- telephone: 987-654-3210
- tasks:
- - debug:
- msg: "{{item}}"
- with_dict: "{{users}}"
复制代码如上例所示,我们将alice和bob的信息完善了,每个人都有自己姓名,性别,电话等信息,上例playbook执行后debug输出信息如下: - TASK [debug] *****************************************
- ok: [test70] => (item={'value': {u'gender': u'male', u'name': u'Bob Bananarama', u'telephone': u'987-654-3210'}, 'key': u'bob'}) => {
- "changed": false,
- "item": {
- "key": "bob",
- "value": {
- "gender": "male",
- "name": "Bob Bananarama",
- "telephone": "987-654-3210"
- }
- },
- "msg": {
- "key": "bob",
- "value": {
- "gender": "male",
- "name": "Bob Bananarama",
- "telephone": "987-654-3210"
- }
- }
- }
- ok: [test70] => (item={'value': {u'gender': u'female', u'name': u'Alice Appleworth', u'telephone': u'123-456-7890'}, 'key': u'alice'}) => {
- "changed": false,
- "item": {
- "key": "alice",
- "value": {
- "gender": "female",
- "name": "Alice Appleworth",
- "telephone": "123-456-7890"
- }
- },
- "msg": {
- "key": "alice",
- "value": {
- "gender": "female",
- "name": "Alice Appleworth",
- "telephone": "123-456-7890"
- }
- }
- }
复制代码从输出信息可以看出,gender、name、telephone都被放入了value关键字中,聪明如你一定想到了,这种情况下,如果想要获取到gender、name、telephone的值,则可以使用如下playbook中的方法 - ---
- - hosts: test70
- remote_user: root
- gather_facts: no
- vars:
- users:
- alice:
- name: Alice Appleworth
- gender: female
- telephone: 123-456-7890
- bob:
- name: Bob Bananarama
- gender: male
- telephone: 987-654-3210
- tasks:
- - debug:
- msg: "User {{ item.key }} is {{ item.value.name }}, Gender: {{ item.value.gender }}, Tel: {{ item.value.telephone }}"
- with_dict: "{{users}}"
复制代码聊完"with_dict",现在来聊聊另一个关键字:"with_subelements",单单靠语言描述"with_subelements"的作用有些麻烦,也不容易理解,不如先来看一个小示例,示例playbook如下: - ---
- - hosts: test70
- remote_user: root
- gather_facts: no
- vars:
- users:
- - name: bob
- gender: male
- hobby:
- - Skateboard
- - VideoGame
- - name: alice
- gender: female
- hobby:
- - Music
- tasks:
- - debug:
- msg: "{{ item }}"
- with_subelements:
- - "{{users}}"
- - hobby
复制代码如上例所示,我们定义了一个复合结构的字典变量,users变量,users变量列表中有两个块序列,这两个块序列分别代表两个用户,bob和alice,alice是个妹子,bob是个汉子,bob的爱好是滑板和打游戏,alice的爱好是听音乐,上例中,我们使用"with_subelements"关键字处理了users变量,在处理users变量的同时,还指定了一个属性,"hobby属性",细心如你一定已经发现了,"hobby属性"正是"users"变量中每个用户的"子属性",换句话说,"hobby属性"是users中每个块序列的子元素,而且,hobby属性是一个"键值对",其"值"是一个列表,因为每个人可以有多个爱好,那么经过"with_subelements"处理后,每个item是什么样子的呢?我们来看一下执行效果,执行上例playbook后,debug模块输出如下: - TASK [debug] ***********************************************************
- ok: [test70] => (item=({u'gender': u'male', u'name': u'bob'}, u'Skateboard')) => {
- "changed": false,
- "item": [
- {
- "gender": "male",
- "name": "bob"
- },
- "Skateboard"
- ],
- "msg": [
- {
- "gender": "male",
- "name": "bob"
- },
- "Skateboard"
- ]
- }
- ok: [test70] => (item=({u'gender': u'male', u'name': u'bob'}, u'VideoGame')) => {
- "changed": false,
- "item": [
- {
- "gender": "male",
- "name": "bob"
- },
- "VideoGame"
- ],
- "msg": [
- {
- "gender": "male",
- "name": "bob"
- },
- "VideoGame"
- ]
- }
- ok: [test70] => (item=({u'gender': u'female', u'name': u'alice'}, u'Music')) => {
- "changed": false,
- "item": [
- {
- "gender": "female",
- "name": "alice"
- },
- "Music"
- ],
- "msg": [
- {
- "gender": "female",
- "name": "alice"
- },
- "Music"
- ]
- }
复制代码从上述输出信息可以看出,一共输出了三个item,第一个item中,bob的名字和性别组成了一个小整体,与bob的滑板爱好组合在了一起,第二个item中,bob的名字和性别组成了一个整体,与bob的打电动爱好组合在了一起,第三个item中,alice的名字与性别组成了一个整体,与alice的音乐爱好组合在了一起,你一定看出了一些规律,规律就是,"with_subelements"会将hobby子元素列表中的每一项作为一个整体,将其他子元素作为一个整体,然后组合在一起,所以,我们可以将上例的playbook修改一下,将msg信息的可读性提高一点,示例如下: - ---
- - hosts: test70
- remote_user: root
- gather_facts: no
- vars:
- users:
- - name: bob
- gender: male
- hobby:
- - Skateboard
- - VideoGame
- - name: alice
- gender: female
- hobby:
- - Music
- tasks:
- - debug:
- msg: "{{ item.0.name }} 's hobby is {{ item.1 }}"
- with_subelements:
- - "{{users}}"
- - hobby
复制代码由于item由两个整体组成,所以,我们通过item.0获取到第一个小整体,即gender和name属性,然后通过item.1获取到第二个小整体,即hobby列表中的每一项,上例执行后,最终每个msg信息的输出如下: - "msg": "bob 's hobby is Skateboard"
- "msg": "bob 's hobby is VideoGame"
- "msg": "alice 's hobby is Music"
复制代码经过上述示例,你一定已经明白了"with_subelements"的用法了,"with_subelements"可以处理一个像上例中一样的复合结构的字典数据,在处理这个字典的同时,需要指定一个子元素,这个子元素的值必须是一个列表,之后,"with_subelements"会将子元素的列表中的每一项作为一个整体,将其他子元素作为一个整体,然后将两个整体组合成item。
"with_dict"和"with_subelements"的用法就先总结到这里,希望这篇文章能够对你有所帮助
|