Home > Enterprise >  Run ansible task with 2 condition and having regex for the condition
Run ansible task with 2 condition and having regex for the condition

Time:07-09

I am having a host file with entries like:

[OS_SA_UAT1]
111.11.11.1
[OS_SB_UAT1]
111.11.11.2
[OS_SB]
111.11.11.3
[OS_SA]
111.11.34.2
[PA_uat1]
1.1.1.1
[PA_uat2]
2.2.2.2

I am trying to run shell command with 2 condition. First it should run if package I am providing is abc/xyz and 2nd if the host entry matches the pattern OS_* / PA_*. I am passing the environment as well. So it should check if the environment passed matches the condition. Below is the playbook. I ran the playbook passing arguments as OS_SB_UAT1 and package=abc

- hosts: {{ environment }}
      
  vars:
    package: "{{ package }}"


  tasks:
       - name: run if package is abc and {{ environment }} entry is OA_*
      shell: sh test.sh
      when: (package == "abc") and ({{ hosts | map('regex_replace', '^(OS_.*)$'}})
      
    - name: run if package is xyz and {{ environment }} entry is PA_*
      shell: sh test.sh
      when: (package == "xyz") and ({{ hosts | map('regex_replace', '^(PA_.*)$'}})

Getting the below error:

{
    "msg": "The conditional check '(package == \"abc\") and ({{ hosts | map('regex_replace', '^(OS_.*)$'}})' failed. The error was: template error while templating string: unexpected '}', expected ')'. String: (package == \"abc\") and ({{ hosts | map('regex_replace', '^(OS_.*)$'}})\n\nThe error appears to be in '/home/Playbooks/playbook.yml': line 21, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: run if package is abc and {{ environment }} \"\n      ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n"

Please help!!

CodePudding user response:

I will write tasks like these:

  tasks:
    - name: run if package is abc and entry is OA_*
      shell: sh test.sh 
      when: package == "abc" and inventory_hostname | regex_search('^OS_.*$')
      
    - name: run if package is xyz and entry is PA_*
      shell: sh test.sh 
      when: package == "xyz" and inventory_hostname | regex_search('^PA_.*$')

CodePudding user response:

You never nest {{...}} markers inside a Jinja expression, and a when statement is already an implicit Jinja expression (that's why you can write package == "abc" rather than {{ package == "abc" }}.

You need to rewrite the second part of the expression to drop the template markers...but you also don't need the map/regex_replace logic. Based on your description, I think what you want is:

- name: run if package is abc and {{ environment }} entry is OA_*
  shell: sh test.sh
  when: (package == "abc") and (environment.startswith('OS_'))
  
- name: run if package is xyz and {{ environment }} entry is PA_*
  shell: sh test.sh
  when: (package == "xyz") and (environment.startswith('PA_'))
  • Related