简体   繁体   中英

How to use variables in Ansible Playbook

I have a silly ask. I am using roles and variables in my playbook but getting an error while executing. Here is the heriarchy of my plays

my-main.yml

---
 - include: validation-and-download-tasks.yml

validation-and-download-tasks.yml

---
- hosts: localhost
  connection: local
  gather_facts: no
  roles:
     - role: valanddownload
       tags:
         - valanddownload

valanddownload main.yml

---
 - name: Validating the input variables
   #hosts: localhost
   #gather_facts: false

   vars:
     _allowed_envs:
       - dev
       - preprod
       - prod

 - name: Ensuring that directory exists to download the artifacts
   file:
     path: "{{ release_location }}/{{my_release_version}}"
     state: directory

Something wrong with my indentation? Error below

ERROR: vars is not a legal parameter in an Ansible task or handler

as written, you should define the vars on the play level, here's an example as you asked:

---
- hosts: localhost
  connection: local
  gather_facts: no
  roles:
     - role: valanddownload
       tags:
         - valanddownload
  vars:
    _allowed_envs:
      - dev
      - preprod
      - prod

You can't attach the vars part to the task level. This should work for you.

Tip: I would use a vars file. Define all your variables there. It is much more comfortable and editable to control all your vars in one file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM