0

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 ^^ !

2
  • can you show the dictionary and how you pass it to the playbook? Commented Jan 21, 2019 at 16:08
  • you can see the details in the third answer Commented Jan 23, 2019 at 11:58

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

3 Comments

i tested it it didn't work for me because my variable is not cname, it is the varialble that have as a name the value of cname. the name of the variable "{{ cname }}" the value of the variable '{{ vars[cname] }}"
in your solution "when: my_vars[cname]['cron'] is defined" will run the contition against the variable. cname.cron but what i wants is server1_pprod.cron ... servern_pprod.cron
cname=server1_prod or cname=server2_prod, pls look at inventory
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.