Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include external yaml file in ansible task

Tags:

ansible

I am creating an ecs_taskdefinition in ansible, but I would like the task-defintion in a sperate file. Can I somehow do something like this:

ecs_taskdefintion:
  containers: {{ load_external_yaml containers.yaml }}
  volumes: {{ load_external_yaml_volumes.yaml }}

So I want to load the yaml data from external files.

like image 901
Nathan Avatar asked Oct 04 '16 12:10

Nathan


People also ask

How do you pass extra vars in Ansible?

To pass a value to nodes, use the --extra-vars or -e option while running the Ansible playbook, as seen below. This ensures you avoid accidental running of the playbook against hardcoded hosts.

How do you use files in Ansible roles?

To create a Ansible roles, use ansible-galaxy command which has the templates to create it. This will create it under the default directory /etc/ansible/roles and do the modifications else we need to create each directories and files manually. where, ansible-glaxy is the command to create the roles using the templates.

How do I import tasks into Ansible?

This module is part of ansible-core and included in all Ansible installations. In most cases, you can use the short module name import_tasks even without specifying the collections: keyword.

Does Ansible use YAML or JSON?

This page provides a basic overview of correct YAML syntax, which is how Ansible playbooks (our configuration management language) are expressed. We use YAML because it is easier for humans to read and write than other common data formats like XML or JSON.


2 Answers

You may try to combine file lookup and from_yaml filter like this:

{{ lookup('file','containers.yaml') | from_yaml }}

Remember that lookups are local, so containers.yaml should be on ansible control host.

like image 50
Konstantin Suvorov Avatar answered Nov 15 '22 12:11

Konstantin Suvorov


Since your file is YAML, you may use include_vars from https://docs.ansible.com/ansible/latest/collections/ansible/builtin/include_vars_module.html :

- name: Setup vars
  tags: ["always"]
  include_vars:
    file: "./vars/tintin.yaml"
    name: tintin

Use tintin as a normal var everywhere!

like image 27
Thomas Decaux Avatar answered Nov 15 '22 10:11

Thomas Decaux