美文网首页
ansible-for循环

ansible-for循环

作者: xlgao | 来源:发表于2020-07-13 22:56 被阅读0次

需求

当我们需要创建如下类型的文件时
server{
listen 80
}
server{
listen 81
}
server{
listen 82
}
可以使用循环和模板结合的方式来实现

实现方法:

  1. 编写yaml文件
[root@localhost playbook]# cat for.yaml
---
- hosts: all
  remote_user: root
  vars:
    ports:
      - 80
      - 81
      - 82

  tasks:
    - name: test for
      template: src=for.conf.j2 dest=/data/for.conf

  1. 创建模板文件
[root@localhost playbook]# cat templates/for.conf.j2
{% for port in ports %}
server {
        listen {{ port }}
}
{% endfor %}
  • 模板文件中的ports列表来源于yaml文件中vars定义的ports变量.
  • {% for语句 %}{% endfor %}是成对出现,缺一不可.
  • 重复部分直接写到for循环中间,不同的部分用变量代替.
  • for循环中的变量需要使用{{ 变量名 }}
  1. 语法检查
[root@localhost playbook]# ansible-playbook for.yaml  -C

PLAY [all] *******************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************
ok: [192.168.0.109]
ok: [192.168.0.107]
ok: [192.168.0.108]

TASK [test for] **************************************************************************************************
changed: [192.168.0.109]
changed: [192.168.0.107]
changed: [192.168.0.108]

PLAY RECAP *******************************************************************************************************
192.168.0.107              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.0.108              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.0.109              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  1. ansible运行
[root@localhost playbook]# ansible-playbook for.yaml

PLAY [all] *******************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************
ok: [192.168.0.109]
ok: [192.168.0.108]
ok: [192.168.0.107]

TASK [test for] **************************************************************************************************
changed: [192.168.0.107]
changed: [192.168.0.108]
changed: [192.168.0.109]

PLAY RECAP *******************************************************************************************************
192.168.0.107              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.0.108              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.0.109              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

  1. 验证执行结果
[root@localhost playbook]# ansible all -m shell -a 'cat /data/for.conf'
192.168.0.109 | CHANGED | rc=0 >>
server {
        listen 80
}
server {
        listen 81
}
server {
        listen 82
}

192.168.0.108 | CHANGED | rc=0 >>
server {
        listen 80
}
server {
        listen 81
}
server {
        listen 82
}

192.168.0.107 | CHANGED | rc=0 >>
server {
        listen 80
}
server {
        listen 81
}
server {
        listen 82
}


相关文章

网友评论

      本文标题:ansible-for循环

      本文链接:https://www.haomeiwen.com/subject/mehthktx.html