ady8531
ady8531

Reputation: 678

Ansible - use with_items and with_sequence at the same time

Is there any way to make a part of role like this: I need to run five times commands that are in the nrpe.cfg (there 5 commands in the config file - so 5 x 5 commands) ?

- 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

Upvotes: 2

Views: 10374

Answers (2)

SebNob
SebNob

Reputation: 11

You can use 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

Upvotes: 1

udondan
udondan

Reputation: 60039

This is currently not possible but should again be with the release of Ansible 2.0. With Ansible 2 you can use with_items together with include, so you will be able to have your check_before task which has the with_sequence loop inside a separate yml file and then include it together with with_items.

Something along these lines:

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

I don't know when Ansible 2 will be released, but you could use the devel branch from github and see if it fits your needs.

Upvotes: 1

Related Questions