ansible的迭代
需求1
在某台机器上创建多个文件,安装多个包。
实现方法
- 编写yaml文件,创建file1 file2 file3 安装htop sl hping3安装包
root@linx:~/ansible/playbook# cat item.yaml
---
- hosts: all
remote_user: root
tasks:
- name: create some files
file: name={{ item }} state=touch
when: ansible_distribution_major_version == '6'
loop:
- file2
- file1
- file3
- name: install some packages
yum: name={{ item }}
when: ansible_distribution_major_version == '7'
loop:
- htop
- sl
- hping3
- name: install some packages
apt: name={{ item }}
when: ansible_distribution_major_version == '18'
loop:
- htop
- sl
- hping3
...
- ansible运行
root@linx:~/ansible/playbook# ansible-playbook item.yaml
PLAY [all] *********************************************************************************************
TASK [Gathering Facts] *********************************************************************************
ok: [172.16.6.160]
ok: [172.16.6.161]
ok: [172.16.6.164]
ok: [172.16.6.163]
TASK [create some files] *******************************************************************************
skipping: [172.16.6.164] => (item=file2)
skipping: [172.16.6.164] => (item=file1)
skipping: [172.16.6.164] => (item=file3)
skipping: [172.16.6.160] => (item=file2)
skipping: [172.16.6.160] => (item=file1)
skipping: [172.16.6.160] => (item=file3)
skipping: [172.16.6.161] => (item=file2)
skipping: [172.16.6.161] => (item=file1)
skipping: [172.16.6.161] => (item=file3)
changed: [172.16.6.163] => (item=file2)
changed: [172.16.6.163] => (item=file1)
changed: [172.16.6.163] => (item=file3)
TASK [install some packages] ***************************************************************************
skipping: [172.16.6.163] => (item=htop)
skipping: [172.16.6.163] => (item=sl)
skipping: [172.16.6.163] => (item=hping3)
skipping: [172.16.6.164] => (item=htop)
skipping: [172.16.6.164] => (item=sl)
skipping: [172.16.6.164] => (item=hping3)
ok: [172.16.6.161] => (item=htop)
ok: [172.16.6.160] => (item=htop)
ok: [172.16.6.161] => (item=sl)
ok: [172.16.6.160] => (item=sl)
ok: [172.16.6.161] => (item=hping3)
ok: [172.16.6.160] => (item=hping3)
TASK [install some packages] ***************************************************************************
skipping: [172.16.6.163] => (item=htop)
skipping: [172.16.6.163] => (item=sl)
skipping: [172.16.6.163] => (item=hping3)
skipping: [172.16.6.160] => (item=htop)
skipping: [172.16.6.160] => (item=sl)
skipping: [172.16.6.160] => (item=hping3)
skipping: [172.16.6.161] => (item=htop)
skipping: [172.16.6.161] => (item=sl)
skipping: [172.16.6.161] => (item=hping3)
ok: [172.16.6.164] => (item=htop)
ok: [172.16.6.164] => (item=sl)
ok: [172.16.6.164] => (item=hping3)
PLAY RECAP *********************************************************************************************
172.16.6.160 : ok=2 changed=0 unreachable=0 failed=0
172.16.6.161 : ok=2 changed=0 unreachable=0 failed=0
172.16.6.163 : ok=2 changed=1 unreachable=0 failed=0
172.16.6.164 : ok=2 changed=0 unreachable=0 failed=0
网友评论