0

I have a list of strings that I expect in some command's output. How can I create an ansible script that tests and - in case one or several of the entries is not contained - executes a task?

So my ansible script might look like:

vars:
  musthave:
    - value1
    - value2
tasks:
- name: Check the configured values
  command: "cat configfile"
  register: current_configuration
  changed_when: false

- set a variable if one or more of the musthave's are missing in current_configuration.stdout
  ...

- name: Execute task to reconfigure system
  command: "reconfigure..."
  when: true = variable

So is there something like

variable = false
for s in musthave:
    if not s in current_configuration.stdout:
        variable |= true

2 Answers 2

1

To solve this problem, you don't need an intermediate variable. According to the Ansible documentation, use the difference filter to select the difference between your list of must have values and the output of your config file.

vars:
  musthave:
    - value1
    - value2

tasks:
- name: Check the configuration values
  command: "seq -f 'value%g' 2 1 5"
  register: current_configuration
  changed_when: false

- debug: var=current_configuration.stdout_lines

- name: Execute task to reconfigure system
  debug: msg="{{ musthave | difference(current_configuration.stdout_lines) }}"
  when: musthave | difference(current_configuration.stdout_lines)

The last task debug is executed only if the difference is not empty.

1
  • Using the difference operator is the best solution I saw so far but it requires to have lists of items to subtract from each other. While this is true for musthave, in my case current_configuration.stdout_lines was not, so I had to hammer this into a suitable list.
    – queeg
    Feb 19, 2021 at 5:45
0

My original request was looking to check for existence of specific values that may appear somewhere in the command's output. Preparing a list for difference to work is a disadvantage, the side effect is that you can calculate the difference in both directions.

So now I can not only check for missing entries but also for those that are too much.

- name: print hostvars
  debug:
    var: hostvars[{{ ansible_host }}]['certificate_domains']

- name: print certificate
  delegate_to: silver.fritz.box
  command: "openssl x509 -in /home/hiran/homeserver.crt -noout -text"
  register: certificate

- name: grab subject's common name and alternative names
  set_fact:
    commonName: "{{ certificate.stdout | regex_search('Subject.*CN\\s=\\s([\\.a-zA-Z]+)', '\\1') }}"
    altNames: "{{ certificate.stdout | regex_findall('DNS:([\\.a-zA-Z]+)') }}"
- name: prepare the lists so they can be subtracted from each other
  set_fact:
    contained: "{{ (commonName + altNames) | unique }}"
    musthave: "{{ hostvars[{{ ansible_host }}]['certificate_domains'] | unique }}"
- name: calculate differences
  set_fact:
    missing: "{{ musthave | difference(contained) }}"
    toomuch: "{{ contained | difference(musthave) }}"

- name: show difference
  debug:
    msg: Something is wrong
  when: (missing | length)>0 or (toomuch | length)>0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .