Ansible playbook循环

具体见文档

http://www.ansible.com.cn/docs/playbooks_loops.html

- name: 安装依赖包
  yum:
    name: "{{ item.line }}"
    state: present
  with_items:
    - { line: 'percona-xtrabackup-24.x86_64' }
    - { line: 'percona-xtrabackup-24-debuginfo.x86_64' }
    - { line: 'percona-xtrabackup-test-24.x86_64' }
    - { line: 'percona-toolkit.x86_64' }
    - { line: 'percona-toolkit-debuginfo.x86_64' }
    - { line: 'percona-toolkit.x86_64' }
    - { line: 'qpress' }
    - { line: 'man' }
    - { line: 'libaio' }
    - { line: 'gcc' }
    - { line: 'cmake' }
    - { line: 'numactl' }

- name: 设置环境变量 .bashrc是给ansible用的
  lineinfile:
    path: "{{ item[0] }}"
    line: "{{ item[1] }}"
  with_nested:
    - { '/root/.bash_profile', '/root/.bashrc' }
    - { 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mysql/lib', 'export PATH=/usr/local/mysql/bin:$PATH' }

- name: 拷贝脚本
  template:
    src: "{{ item.j2 }}"
    dest: "{{ item.sh }}" 
    mode: 0755
  with_items:
    - { j2: 'login_db.sh.j2', sh: '/data/bin/login_db.sh' }
    - { j2: 'start_db.sh.j2', sh: '/data/bin/start_db.sh' }
    - { j2: 'stop_db.sh.j2', sh: '/data/bin/stop_db.sh' }
    - { j2: 'init_mysql.sh.j2', sh: '/data/bin/init_mysql.sh' }
    - { j2: 'pt_stalk_func_thds_conn_runn.sh.j2', sh: '/data/scripts/bin/pt_stalk_func_thds_conn_runn.sh' }

对文件列表使用循环

with_fileglob 可以以非递归的方式来模式匹配单个目录中的文件.如下面所示:

---
- hosts: all
  tasks:
    # first ensure our target directory exists
    - file: dest=/etc/fooapp state=directory
    # copy each file over that matches the given pattern
    - copy: src={{ item }} dest=/etc/fooapp/ owner=root mode=600
      with_fileglob:
        - /playbooks/files/fooapp/*

对并行数据集使用循环

假设你通过某种方式加载了以下变量数据:

---
alpha: [ 'a', 'b', 'c', 'd' ]
numbers: [ 1, 2, 3, 4 ]

如果你想得到’(a, 1)’和’(b, 2)’之类的集合.可以使用’with_together’:

tasks:
    - debug: msg="{{ item.0 }} and {{ item.1 }}"
      with_together:
        - "{{alpha}}"
        - "{{numbers}}"

对哈希表使用循环

假如你有以下变量:

---
users:
  alice:
    name: Alice Appleworth
    telephone: 123-456-7890
  bob:
    name: Bob Bananarama
    telephone: 987-654-3210

你想打印出每个用户的名称和电话号码.你可以使用 with_dict 来循环哈希表中的元素:

tasks:
  - name: Print phone records
    debug: msg="User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})"
    with_dict: "{{users}}"

Do-Until循环

有时你想重试一个任务直到达到某个条件.比如下面这个例子:

- action: shell /usr/bin/foo
  register: result
  until: result.stdout.find("all systems go") != -1
  retries: 5
  delay: 10

上面的例子递归运行shell模块,直到模块结果中的stdout输出中包含”all systems go”字符串,或者该任务按照10秒的延迟重试超过5次.”retries”和”delay”的默认值分别是3和5.
该任务返回最后一个任务返回的结果.单次重试的结果可以使用-vv选项来查看. 被注册的变量会有一个新的属性’attempts’,值为该任务重试的次数.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值