2015-06-30 29 views
2

有什么办法可以让角色的一部分像这样: 我需要运行五次nrpe.cfg中的命令(在配置文件中有5条命令 - 所以5×5的命令)?Ansible - 同时使用with_items和with_sequence

- name: grep the commands from nagios 
    shell: grep -R check_http_ /etc/nagios/nrpe.cfg | cut -d= -f2- 
    register: nagios_check 
- name: check_before 
    shell: (printf $(echo '{{ item }}' | awk -F'country=' '{print $2}' | cut -d'&' -f1); printf ' ';{{ item }} | cut -d ' ' -f-2) >> {{ ansible_env.DATA_LOG }}/eden-{{ dateext.stdout }} 
    register: checkedenbefore 
    with_items: "{{ nagios_check.stdout_lines }}" 
    **with_sequence: count=5** 
    ignore_errors: True 

回答

1

这是目前不可能的,但应该再次发布Ansible 2.0。使用Ansible 2,您可以使用with_itemsinclude,因此您可以将check_before任务的with_sequence循环放入单独的yml文件中,然后将其与with_items一起包含在内。沿着这些线路

东西:

main.yml

- name: grep the commands from nagios 
    shell: grep -R check_http_ /etc/nagios/nrpe.cfg | cut -d= -f2- 
    register: nagios_check 
- include: check_before.yml 
    with_items: "{{ nagios_check.stdout_lines }}" 

check_before.yml

- name: check_before 
    shell: (printf $(echo '{{ item }}' | awk -F'country=' '{print $2}' | cut -d'&' -f1); printf ' ';{{ item }} | cut -d ' ' -f-2) >> {{ ansible_env.DATA_LOG }}/eden-{{ dateext.stdout }} 
    register: checkedenbefore 
    with_sequence: count=5 
    ignore_errors: True 

我不知道什么时候Ansible 2将被释放,但您可以使用devel branch from github并查看它是否符合您的需求。

+0

您的答案中的主要问题是,它不是一个单一的角色。所以基本上没有“干净”的方式来重复一个有“with_items”的任务(x次)? – ady8531

+0

不知道这是什么意思:它不是一个单一的角色 – udondan

+0

在你的答案中你使用了2个角色:main.yml和check_before.yml不是一个 – ady8531

1

您可以使用with_nested。

- name: grep the commands from nagios 
    shell: grep -R check_http_ /etc/nagios/nrpe.cfg | cut -d= -f2- 
    register: nagios_check 

- name: check_before 
    shell: (printf $(echo '{{ item.0 }}' | awk -F'country=' '{print $2}' | cut -d'&' -f1); printf ' ';{{ item.0 }} | cut -d ' ' -f-2) >> {{ ansible_env.DATA_LOG }}/eden-{{ dateext.stdout }} 
    register: checkedenbefore 
    with_nested: 
    - "{{ nagios_check.stdout_lines }}" 
    - "{{ range(0, 5) }}" 
    ignore_errors: True 
相关问题