Midokate
Midokate

Reputation: 95

how to run a task if only a variable is defined

I have the following task :

-name : task name
 cron:
     ...
     ...
     ...     
 when:  "{{ vars[cname].cron }}" is defined

i want to run a task only if the variable server1.cron for the the host server1 and server2.cron for the host server2 etc.. is defined but ansible don't like the syntax when i let only vars[cname].cron ansible with ou " " it doesn't replace vars[cname].cron with server1.cron or server2.cron it interpret it as "vars[cname].cron" variable which does not exist. Have you encountered this issue? Thanks in advance ^^ !

Upvotes: 0

Views: 5998

Answers (2)

itiic
itiic

Reputation: 3712

Try this on:

---

- hosts: all
  gather_facts: False

  vars:
    my_vars:
      cname1:
        cron: "val1"
      cname2:
        cron2: "val2"

  tasks:
    - name: task name 1
      debug:
        msg: "test"
      when:  my_vars['cname1']['cron'] is defined

    - name: task name 2
      debug:
        msg: "test"
      when:  my_vars['cname2']['cron'] is defined

The output is

PLAY [all] ***************************************************************************************************************************************************

TASK [task name 1] *******************************************************************************************************************************************
ok: [host] => {
    "msg": "test"
}

TASK [task name 2] *******************************************************************************************************************************************
skipping: [host]

PLAY RECAP ***************************************************************************************************************************************************
host                    : ok=1    changed=0    unreachable=0    failed=0

Or another approach:

Inventory

[all]
staging cname=server1_prod
testing cname=server2_prod

Playbook:

---

- hosts: all
  gather_facts: False

  vars:
    my_vars:
      server1_prod:
        cron: "val1"
      server2_prod:
        cron2: "val2"

  tasks:
    - name: task name 1
      debug:
        msg: "test"
      when:  my_vars[cname]['cron'] is defined

You should skip quotations in when section. https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html

Upvotes: 0

Midokate
Midokate

Reputation: 95

I have in the inventory file :

    server1-prod     cname=server1_prod
    server2-prdo    cname=server2_prod

......
.....

in my role i have in vars a file like this for every server: server1_pprod server2_pp etc ... <== this is the name of the files which are in role/vars/

cron:
  job1:
        name: "******"
        job: "********"
        minute: "0"
        hour: "0"
        day: "*"
        month: "*"
        weekday: "*"
        disabled: "no"
        backup: yes
        job: "*******************************"


cron_special_time:
  job1:
        name: "******"
        special_time: "reboot"
        disabled: "yes"
        backup: yes
        job: "*****"
  job2:
        name: "*****"
        special_time: "reboot"
        disabled: "yes"
        backup: yes
        job: "*****"

I include the file with this task so that i have a variabl serveX_prod for every file serverX_prod which corespend to a serveX-prod in the inventory

- name: Loading system cron  file to vars
  include_vars:
    file: "{{ cname }}"
    name: "{{ cname  }}"

then i access the variable in a task :

- name: Add system  cron jobs
  cron:
              .....
             ......
  with_dict:
        - "{{ vars[cname].cron }}"
   when:  "{{ vars[cname].cron }}" is defined

you see the problem is i cant put " " in when sentence and i cant get a dynamic variable without " " , i am confused

Upvotes: 0

Related Questions