可以循环 x 次 [英] Ansible to loop x times

查看:35
本文介绍了可以循环 x 次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须做一些基准测试并循环 10 个命令 3 次(运行所有 10x3,而不是第一个 x3 然后第二个 x3 - 所以运行所有 10x3).我从寄存器变量中的文件中提取的 10 个命令(它不适用于 _lines: 然后是命令)并执行它们 1,2,3..,10 将输出通过管道传输到文件中,回显某些内容,然后再次执行他们……这一切都发生了 3 次

I have to do some benchmarks and loop over 10 commands 3 times (run all 10x3, not the firstx3 then the secondx3 - so run all 10x3). The 10 commands I extract from a file in a register variable (it doesn't work with_lines: and then the command) and execute them 1,2,3..,10 pipe the output in a file, echo something and then again execute them... all this 3 times

这就是我在 x3 下面的代码(nagios_check 注册变量中有 10 个命令/行):

this how I'm doing it the code below x3 (there are 10 commands/lines in the nagios_check registered variable):

... more code above
- name: get the date for naming purpose
  shell: date +%Y%m%d-%HH%MM%SS
  register: dateext
- name: grep the commands from nagios
  shell: grep -R check_http_EDEN_ /etc/nagios/nrpe.cfg | cut -d= -f2-
  register: nagios_check
- name: check_eden_before
  shell: (printf $(echo '{{ item }}' | awk -F'country=' '{print $2}' | cut -d'&' -f1); printf ' ';{{ item }} | cut -d ' ' -f-2) >> {{ ansible_env.DATA_LOG }}/eden-{{ ansible_hostname }}-{{ dateext.stdout }}
  with_items: "{{ nagios_check.stdout_lines }}"
  ignore_errors: True
- name: enter simple line
  shell: echo "=================" >> {{ ansible_env.DATA_LOG }}/eden-{{ ansible_hostname }}-{{ dateext.stdout }}

...上面的这部分我已经写了 3 次(全部),然后写了更多代码

... this part above I have it written 3 times (all of it) and after more code

有没有办法让它更简单?(它已经是一个角色了,我用了 4 次这个角色——不要让我把它变成更小的角色,因为它更复杂,我最终会得到一个巨大的剧本像 12 倍的这个角色",它看起来很可怕)

is there a way to make it more simpler ?(it's already a role, I use this role 4 times - don't make me brake it in smaller roles because it's more complex and I'll end up with a huge playbook with something like 12x"this role" and it would look horrible)

推荐答案

你可以把你想要重复的任务放在一个单独的 yaml 文件中:

You can put the tasks you want to repeat in a separate yaml file:

---
# tasks-to-repeat.yml
- name: get the date for naming purpose
  shell: date +%Y%m%d-%HH%MM%SS
  register: dateext
- name: grep the commands from nagios
  shell: grep -R check_http_EDEN_ /etc/nagios/nrpe.cfg | cut -d= -f2-
  register: nagios_check
- name: check_eden_before
  shell: (printf $(echo '{{ item }}' | awk -F'country=' '{print $2}' | cut -d'&' -f1); printf ' ';{{ item }} | cut -d ' ' -f-2) >> {{ ansible_env.DATA_LOG }}/eden-{{ ansible_hostname }}-{{ dateext.stdout }}
  with_items: "{{ nagios_check.stdout_lines }}"
  ignore_errors: True
- name: enter simple line
  shell: echo "=================" >> {{ ansible_env.DATA_LOG }}/eden-{{ ansible_hostname }}-{{ dateext.stdout }}

然后将其包含在您的剧本中 3 次:

and then include it in your playbook 3 times:

---
# Your playbook
... more code above
- include: task-to-repeat.yml
- include: task-to-repeat.yml
- include: task-to-repeat.yml

这篇关于可以循环 x 次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆