ansible:with_items的使用

本文详细介绍了如何在Ansible playbook中使用with_items进行循环操作,包括在playbook中定义循环、自定义值输出、通过item获取指定值、利用with_items创建文件以及与shell模块的结合使用,通过实例展示了每个用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1. 在playbook中使用循环with_items

[root@server4 ~]# vim /etc/ansible/hosts
172.25.60.2
testB.westos.com ansible_host=172.25.60.1
testC ansible_host=172.25.60.6

[testA]
test1 ansible_host=172.25.60.5

[testB]
172.25.60.3

[test:children]
testA
testB

with_items:将groups.ungrouped中返回的信息,一条一条的放到item中,并一条一条输出

[root@server4 ~]# cat xh.yml 
---
- hosts: testB
  remote_user: root
  gather_facts: no  # 禁止testB这台主机的fact的信息
  tasks:
  - debug:
      msg: "{{item}}"
    with_items: "{{groups.ungrouped}}"  

测试:

[root@server4 ~]# ansible-playbook xh.yml 

PLAY [testB] *******************************************************************

TASK [debug] *******************************************************************
ok: [172.25.60.3] => (item=172.25.60.2) => {
    "msg": "172.25.60.2"
}
ok: [172.25.60.3] => (item=testB.westos.com) => {
    "msg": "testB.westos.com"
}
ok: [172.25.60.3] => (item=testC) => {
    "msg": "testC"
}

PLAY RECAP *********************************************************************
172.25.60.3                : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

2. 自定义with_items的值输出

[root@server4 ~]# cat xh.yml 
---
- hosts: testB
  remote_user: root
  gather_facts: no  # 禁止手机testB这台主机的fact的信息
  tasks:
  - debug:
      msg: "{{item}}"
    with_items:  # 或者[1,2,3]
    - 1
    - 2
    - 3

测试:

[root@server4 ~]# ansible-playbook xh.yml 

PLAY [testB] *******************************************************************

TASK [debug] *******************************************************************
ok: [172.25.60.3] => (item=1) => {
    "msg": 1
}
ok: [172.25.60.3] => (item=2) => {
    "msg": 2
}
ok: [172.25.60.3] => (item=3) => {
    "msg": 3
}

PLAY RECAP *********************************************************************
172.25.60.3                : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

3. 使用item.获取指定值

[root@server4 ~]# cat xh.yml 
---
- hosts: testB
  remote_user: root
  gather_facts: no  
  tasks:
  - debug:
      msg: "{{item.test1}}"
    with_items:
    - { test1: a, test2: b }
    - { test1: c, test2: d }

测试:

[root@server4 ~]# ansible-playbook xh.yml 

PLAY [testB] *******************************************************************

TASK [debug] *******************************************************************
ok: [172.25.60.3] => (item={u'test1': u'a', u'test2': u'b'}) => {
    "msg": "a"
}
ok: [172.25.60.3] => (item={u'test1': u'c', u'test2': u'd'}) => {
    "msg": "c"
}

PLAY RECAP *********************************************************************
172.25.60.3                : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

4. 使用with_items建立四个文件:用vars来定义文件名,使用with_items逐个建立

[root@server4 ~]# cat xh2.yml 
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    dirs:
    - "/opt/a"
    - "/opt/b"
    - "/opt/c"
    - "/opt/d"
  tasks:
    - file:
        path: "{{item}}"
        state: touch
      with_items: "{{dirs}}"

测试:

[root@server4 ~]# ansible-playbook xh2.yml 

PLAY [testB] *******************************************************************

TASK [file] ********************************************************************
changed: [172.25.60.3] => (item=/opt/a)
changed: [172.25.60.3] => (item=/opt/b)
changed: [172.25.60.3] => (item=/opt/c)
changed: [172.25.60.3] => (item=/opt/d)

PLAY RECAP *********************************************************************
172.25.60.3                : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

5. with_items与shell结合使用

[root@server4 ~]# cat xh3.yml
---
- hosts: testB
  remote_user: root
  tasks:
  - shell: "{{item}}"
    with_items:
    - "ls /opt"
    - "ls /mnt"
    register: returnvalue
  - debug:
      var: returnvalue

测试:

[root@server4 ~]# ansible-playbook xh3.yml 

PLAY [testB] *******************************************************************

TASK [Gathering Facts] *********************************************************
ok: [172.25.60.3]

TASK [shell] *******************************************************************
changed: [172.25.60.3] => (item=ls /opt)
changed: [172.25.60.3] => (item=ls /mnt)

TASK [debug] *******************************************************************
ok: [172.25.60.3] => {
    "returnvalue": {
        "changed": true, 
        "msg": "All items completed", 
        "results": [
            {
                "ansible_loop_var": "item", 
                "changed": true, 
                "cmd": "ls /opt", 
                "delta": "0:00:00.003360", 
                "end": "2020-03-28 20:55:34.508632", 
                "failed": false, 
                "invocation": {
                    "module_args": {
                        "_raw_params": "ls /opt", 
                        "_uses_shell": true, 
                        "argv": null, 
                        "chdir": null, 
                        "creates": null, 
                        "executable": null, 
                        "removes": null, 
                        "stdin": null, 
                        "stdin_add_newline": true, 
                        "strip_empty_ends": true, 
                        "warn": true
                    }
                }, 
                "item": "ls /opt", 
                "rc": 0, 
                "start": "2020-03-28 20:55:34.505272", 
                "stderr": "", 
                "stderr_lines": [], 
                "stdout": "a\nb\nc\ncopytest\ncopytest.12727.2020-03-22@11:40:13~\nd\nid_rsa_ljl\nid_rsa_ljl.pub\ntest", 
                "stdout_lines": [
                    "a", 
                    "b", 
                    "c", 
                    "copytest", 
                    "copytest.12727.2020-03-22@11:40:13~", 
                    "d", 
                    "id_rsa_ljl", 
                    "id_rsa_ljl.pub", 
                    "test"
                ]
            }, 
            {
                "ansible_loop_var": "item", 
                "changed": true, 
                "cmd": "ls /mnt", 
                "delta": "0:00:00.003256", 
                "end": "2020-03-28 20:55:34.719663", 
                "failed": false, 
                "invocation": {
                    "module_args": {
                        "_raw_params": "ls /mnt", 
                        "_uses_shell": true, 
                        "argv": null, 
                        "chdir": null, 
                        "creates": null, 
                        "executable": null, 
                        "removes": null, 
                        "stdin": null, 
                        "stdin_add_newline": true, 
                        "strip_empty_ends": true, 
                        "warn": true
                    }
                }, 
                "item": "ls /mnt", 
                "rc": 0, 
                "start": "2020-03-28 20:55:34.716407", 
                "stderr": "", 
                "stderr_lines": [], 
                "stdout": "haha\nredhat.repo\nyum.repo", 
                "stdout_lines": [
                    "haha", 
                    "redhat.repo", 
                    "yum.repo"
                ]
            }
        ]
    }
}

PLAY RECAP *********************************************************************
172.25.60.3                : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

获取指定值:

[root@server4 ~]# cat xh4.yml 
---
- hosts: testB
  remote_user: root
  tasks:
  - shell: "{{item}}"
    with_items:
    - "ls /opt"
    - "ls /mnt"
    register: returnvalue
  - debug:
      msg: "{{item.stdout}}"
with_items: "{{returnvalue.results}}"

测试:

[root@server4 ~]# ansible-playbook xh4.yml 

PLAY [testB] *******************************************************************

TASK [Gathering Facts] *********************************************************
ok: [172.25.60.3]

TASK [shell] *******************************************************************
changed: [172.25.60.3] => (item=ls /opt)
changed: [172.25.60.3] => (item=ls /mnt)

TASK [debug] *******************************************************************
ok: [172.25.60.3] => (item={'stderr_lines': [], 'ansible_loop_var': u'item', u'end': u'2020-03-28 21:01:09.498190', u'stderr': u'', u'stdout': u'a\nb\nc\ncopytest\ncopytest.12727.2020-03-22@11:40:13~\nd\nid_rsa_ljl\nid_rsa_ljl.pub\ntest', u'changed': True, 'failed': False, u'delta': u'0:00:00.003239', u'cmd': u'ls /opt', 'item': u'ls /opt', u'rc': 0, u'invocation': {u'module_args': {u'warn': True, u'executable': None, u'_uses_shell': True, u'strip_empty_ends': True, u'_raw_params': u'ls /opt', u'removes': None, u'argv': None, u'creates': None, u'chdir': None, u'stdin_add_newline': True, u'stdin': None}}, 'stdout_lines': [u'a', u'b', u'c', u'copytest', u'copytest.12727.2020-03-22@11:40:13~', u'd', u'id_rsa_ljl', u'id_rsa_ljl.pub', u'test'], u'start': u'2020-03-28 21:01:09.494951'}) => {
    "msg": "a\nb\nc\ncopytest\ncopytest.12727.2020-03-22@11:40:13~\nd\nid_rsa_ljl\nid_rsa_ljl.pub\ntest"
}
ok: [172.25.60.3] => (item={'stderr_lines': [], 'ansible_loop_var': u'item', u'end': u'2020-03-28 21:01:09.724978', u'stderr': u'', u'stdout': u'haha\nredhat.repo\nyum.repo', u'changed': True, 'failed': False, u'delta': u'0:00:00.003326', u'cmd': u'ls /mnt', 'item': u'ls /mnt', u'rc': 0, u'invocation': {u'module_args': {u'warn': True, u'executable': None, u'_uses_shell': True, u'strip_empty_ends': True, u'_raw_params': u'ls /mnt', u'removes': None, u'argv': None, u'creates': None, u'chdir': None, u'stdin_add_newline': True, u'stdin': None}}, 'stdout_lines': [u'haha', u'redhat.repo', u'yum.repo'], u'start': u'2020-03-28 21:01:09.721652'}) => {
    "msg": "haha\nredhat.repo\nyum.repo"
}

PLAY RECAP *********************************************************************
172.25.60.3                : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值