what is the purpose of main.yml file in vars folder in ansible role?

Viewed 2959

my task is to copy file in file folder, template in templates folder to remote host ``

template file has nothing just text.

templates/templateexample.txt.j2 =>This is the example template to be transferred.

vars/main.yml

---
project_files:
  files:
    - src: "examplefile1.txt"
      dst: "~/examplefile1.txt"
      mode: '0755'
      force: false
  templates:
    - src: "templateexample.txt.j2"
      dst: "~/templateexample.txt"
      mode: '0755'
      force: false
2 Answers

Q: What is the purpose of main.yml file in vars folder in ansible role?

A: Main reason is to customize role's default variables.

The variables defined in role/vars/main.yml override all variables defined in roles defaults, inventories, group_vars, host_vars, and all variables defined in the play before the role was included in the play. See Variable precedence: Where should I put a variable? for details.

For example, let's create a playbook

shell> cat playbook.yml 
- hosts: localhost
  vars:
    test_var: test_var defined in the playbook vars
  roles:
    - role1

and let's create a role with empty roles/role1/vars/main.yml and simple debug task in roles/role1/tasks/main.yml

shell> cat roles/role1/tasks/main.yml
- debug:
    var: test_var

shell> cat roles/role1/vars/main.yml 
---
# vars file for role1

The play will display the variable defined in the playbooks' vars

shell> ansible-playbook test-14.yml 
  ...
  test_var: test_var defined in the playbook vars

Now, define the variable test_var in role's role1/vars/main.yml

shell> cat roles/role1/vars/main.yml 
---
# vars file for role1
test_var: test_var defined in the role vars

The variable defined in the role's vars overridden the variable defined in the playbook's vars.

shell> ansible-playbook playbook.yml 
  ...
  test_var: test_var defined in the role vars

Notes:

  • Do not put roles' vars/main.yml into the Ansible Galaxy git. This will preserve local customizations from updates and upgrades.

#file: copy-static.yml

  • name: Copy a file copy: src=examplefile1.txt dest=/home/user/example.txt

#file: copy-template.yml

  • name: sample template - x template: src: templateexample.txt.j2 dest: /home/user/newexample.txt with_items: var_x

#file: ngnix.yml

- name: install nginx
 apt: name=nginx update_cache=yes state=latest
- name: displaying message
  debug: msg="yayy!! nginx installed"
  

#file: mainplaybook.yml

  • hosts: localhost connection: local become: yes become_method: sudo roles:
    • fresco_roles/sample_role

#file: tasks/main.yml

  • include: nginx.yml
  • include: copy-static.yml
  • include: copy-template.yml

#file: hosts [localhost] localhost

#file: vars/main.yml

project_files: files: - src: "examplefile1.txt" dst: "~/examplefile1.txt" mode: '0755' force: false templates: - src: "templateexample.txt.j2" dst: "~/templateexample.txt" mode: '0755' force: false var_x:

  • 'variable x' var_y:
  • 'variable y'
Related