ansible循环

参考:http://www.zsythink.net/archives/2797

with_items:

[root@localhost ~]#vim test2.yml
---
- hosts: B
  remote_user: root
  tasks:
  - debug: msg={{item}}
    with_items:
    - a
    - b
    - c

或者
[root@localhost ~]#vim test2.yml
---
- hosts: B
  remote_user: root
  tasks:
  - debug: msg={{item}}
    with_items: [a,b,c]

或者
[root@localhost ~]#vim test2.yml
---
- hosts: B
  remote_user: root
  vars:
   list:
   - a
   - b
   - c
  tasks:
  - debug: msg={{item}}
    with_items: "{{list}}"

或者
[root@localhost ~]#vim test2.yml
---
- hosts: B
  remote_user: root
  vars:
   list: [a,b,c]
  tasks:
  - debug: msg={{item}}
    with_items: "{{list}}"

以上四种方式结果一样

循环嵌套循环
[root@localhost ~]#vim test2.yml
---
- hosts: B
  remote_user: root
  tasks:
  - debug: msg={{item}}
    with_items:					#wtih_items会遍历嵌套循环中的每个元素 
    - [1,2,3]
    - [a,b]

[root@localhost ~]#ansible-playbook test2.yml 


PLAY [B] **********************************************************************************************************

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

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

PLAY RECAP ********************************************************************************************************
192.168.91.132             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

with_list:

上例的with_items换成with_list				#只处理最外层循环
[root@localhost ~]#ansible-playbook test2.yml 

PLAY [B] **********************************************************************************************************

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

TASK [debug] ******************************************************************************************************
ok: [192.168.91.132] => (item=[1, 2, 3]) => {
    "msg": [
        1, 
        2, 
        3
    ]
}
ok: [192.168.91.132] => (item=[u'a', u'b']) => {
    "msg": [
        "a", 
        "b"
    ]
}

PLAY RECAP ********************************************************************************************************
192.168.91.132             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

with_flattened:

[root@localhost ~]#vim test2.yml
---
- hosts: B
  remote_user: root
  tasks:
  - debug: msg={{item}}
    with_flattened:					#效果与with_items相同
    - [1,2,3]
    - [a,b]

[root@localhost ~]#ansible-playbook test2.yml 

PLAY [B] **********************************************************************************************************

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

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

PLAY RECAP ********************************************************************************************************
192.168.91.132             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

with_together:

[root@localhost ~]#vim test2.yml
---
- hosts: B
  remote_user: root
  tasks:
  - debug: msg={{item}}
    with_together:
    - [1,2,3]
    - [a,b]

[root@localhost ~]#ansible-playbook test2.yml 

PLAY [B] **********************************************************************************************************

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

TASK [debug] ******************************************************************************************************
ok: [192.168.91.132] => (item=[1, u'a']) => {
    "msg": [
        1, 
        "a"
    ]
}
ok: [192.168.91.132] => (item=[2, u'b']) => {
    "msg": [
        2, 
        "b"
    ]
}
ok: [192.168.91.132] => (item=[3, None]) => {
    "msg": [
        3, 
        null
    ]
}

PLAY RECAP ********************************************************************************************************
192.168.91.132             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

with_cartesian:

[root@localhost ~]#cat test2.yml 
---
- hosts: B
  remote_user: root
  tasks: 
  - file: path=/data/{{item.0}}/{{item.1}} state=directory
    with_cartesian:					#笛卡尔乘积
    - [1,2,3]
    - [a,b]

[root@centos7 ~]#tree /data/
/data/
├── 1
│   ├── a
│   └── b
├── 2
│   ├── a
│   └── b
└── 3
    ├── a
    └── b

with_squence:

[root@localhost ~]#vim test2.yml
---
- hosts: B
  remote_user: root
  tasks:
  - debug: msg={{item}}
    with_sequence: start=1 end=4 stride=1				#stride:步进

[root@localhost ~]#ansible-playbook test2.yml 

PLAY [B] **********************************************************************************************************

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

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

PLAY RECAP ********************************************************************************************************
192.168.91.132             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

[root@localhost ~]#vim test2.yml
---
- hosts: B
  remote_user: root
  tasks:
  - debug: msg={{item}}
    with_sequence: count=4				
与上例结果相同

[root@localhost ~]#vim test2.yml
---
- hosts: B
  remote_user: root
  tasks:
  - debug: msg={{item}}
    with_sequence: start=4 end=0 stride=-2

with_dict:

[root@localhost ~]#vim test2.yml
---
- hosts: B
  remote_user: root
  tasks:
  - debug: msg={{item}}
    with_dict: [alice: female,bob: male]

[root@localhost ~]#vim test2.yml
---
- hosts: B
  remote_user: root
  vars:
   user:
   - alice: female
   - bob: male
  tasks:
  - debug: msg={{item}}
    with_dict: "{{user}}"

以上两种方式结果相同

[root@localhost ~]#ansible-playbook test2.yml 

PLAY [B] **********************************************************************************************************

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

TASK [debug] ******************************************************************************************************
ok: [192.168.91.132] => (item={'value': u'female', 'key': u'alice'}) => {
    "msg": {
        "key": "alice", 
        "value": "female"
    }
}
ok: [192.168.91.132] => (item={'value': u'male', 'key': u'bob'}) => {
    "msg": {
        "key": "bob", 
        "value": "male"
    }
}

PLAY RECAP ********************************************************************************************************
192.168.91.132             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

[root@localhost ~]#vim test2.yml
---
- hosts: B
  remote_user: root
  vars:
   user:
    alice: female
    bob: male
  tasks:
  - debug: msg=name:{{item.key}},gender:{{item.value}}
    with_dict: "{{user}}"

[root@localhost ~]#ansible-playbook test2.yml 

PLAY [B] **********************************************************************************************************

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

TASK [debug] ******************************************************************************************************
ok: [192.168.91.132] => (item={'value': u'female', 'key': u'alice'}) => {
    "msg": "name:alice,gender:female"
}
ok: [192.168.91.132] => (item={'value': u'male', 'key': u'bob'}) => {
    "msg": "name:bob,gender:male"
}

PLAY RECAP ********************************************************************************************************
192.168.91.132             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

[root@localhost ~]#vim test2.yml
---
- hosts: B
  remote_user: root
  vars:
   user:
    alice:
     name: alice a
     gender: female
     age: 20
    bob:
     name: bob b
     gender: male
     age: 25
  tasks:
  - debug: msg={{item}}
    with_dict: "{{user}}"

TASK [debug] ******************************************************************************************************
ok: [192.168.91.132] => (item={'value': {u'gender': u'male', u'age': 25, u'name': u'bob b'}, 'key': u'bob'}) => {
    "msg": {
        "key": "bob", 
        "value": {
            "age": 25, 
            "gender": "male", 
            "name": "bob b"
        }
    }
}
ok: [192.168.91.132] => (item={'value': {u'gender': u'female', u'age': 20, u'name': u'alice a'}, 'key': u'alice'}) => {
    "msg": {
        "key": "alice", 
        "value": {
            "age": 20, 
            "gender": "female", 
            "name": "alice a"
        }
    }
}

[root@localhost ~]#vim test2.yml
---
- hosts: B
  remote_user: root
  vars:
   user:
    alice:
     name: alice a
     gender: female
     age: 20
    bob:
     name: bob b
     gender: male
     age: 25
  tasks:
  - debug: msg="name is {{item.value.name}},age is {{item.value.age}},gender is {{item.value.gender}}"
    with_dict: "{{user}}"

TASK [debug] ******************************************************************************************************
ok: [192.168.91.132] => (item={'value': {u'gender': u'male', u'age': 25, u'name': u'bob b'}, 'key': u'bob'}) => {
    "msg": "name is bob b,age is 25,gender is male"
}
ok: [192.168.91.132] => (item={'value': {u'gender': u'female', u'age': 20, u'name': u'alice a'}, 'key': u'alice'}) => {
    "msg": "name is alice a,age is 20,gender is female"
}

with_file

展示ansible主机上的文件的内容,而不会被控主机上的文件
---
- hosts: test70
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{ item }}"
    with_file:
    - /testdir/testdir/a.log
    - /opt/testfile

with_fileglob

用来ansible主机上的文件名。只会匹配文件,不匹配目录
---
- hosts: test70
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{ item }}"
    with_fileglob:
    - /testdir/*
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值