How can I write variables inside the tasks file in ansible

ansible variables example
ansible set variable in task
ansible assign variable to another variable
ansible/group_vars
ansible include_vars
ansible save facts to file
ansible global variables
ansible list variables

I have this play.yml

---
- hosts: 127.0.0.1
  connection: local
  sudo: false

  tasks:
     - include: apache.yml

My Apache look like this:

vars:
    url: czxcxz

- name: Download apache
  shell: wget {{url}} 

This is giving me error.

If I remove vars then it works. But I want to include the vars inside tasks so that I can keep different vars for different tasks separate.

NOTE: Using set_fact as described below sets a fact/variable onto the remote servers that the task is running against. This fact/variable will then persist across subsequent tasks for the entire duration of your playbook.

Also, these facts are immutable (for the duration of the playbook), and cannot be changed once set.


ORIGINAL ANSWER

Use set_fact before your task to set facts which seem interchangeable with variables:

- name: Set Apache URL
  set_fact:
    apache_url: 'http://example.com/apache'

- name: Download Apache
  shell: wget {{ apache_url }}

See http://docs.ansible.com/set_fact_module.html for the official word.

Using Variables — Ansible Documentation, Inside a template you automatically have access to all variables that are in scope There are other places where variables can come from, but these are a type of hosts: webservers tasks: - name: create directory for ansible custom facts file:� How can I write variables inside the tasks file in ansible. Whenever you have a module followed by a variable on the same line in ansible the parser will treat

I know, it is long ago, but since the easiest answer was not yet posted I will do so for other user that might step by.

Just move the var inside the "name" block:

- name: Download apache
  vars:
    url: czxcxz
  shell: wget {{url}} 

Variables — Ansible Documentation, See the Inventory document for multiple ways on how to define variables in inventory. Inside a template you automatically have access to all of the variables that are Users can also write custom facts modules, as described in the API guide. hosts: webservers tasks: - name: create directory for ansible custom facts file:� Loads YAML/JSON variables dynamically from a file or directory, recursively, during task runtime. If loading a directory, the files are sorted alphabetically before being loaded. This module is also supported for Windows targets. To assign included variables to a different host than inventory_hostname, use delegate_to and set delegate_facts=yes.

Variable definitions are meant to be used in tasks. But if you want to include them in tasks probably use the register directive. Like this:

- name: Define variable in task.
  shell: echo "http://www.my.url.com"
  register: url

- name: Download apache
  shell: wget {{ item }}
  with_items: url.stdout

You can also look at roles as a way of separating tasks depending on the different roles roles. This way you can have separate variables for each of one of your roles. For example you may have a url variable for apache1 and a separate url variable for the role apache2.

include_vars – Load variables from files, dynamically within a task , Loads YAML/JSON variables dynamically from a file or directory, recursively, If the path is relative and the task is inside a role, it will look inside the role's vars/ x == 0 - name: Load a variable file based on the OS type, or a default if not found. [stableinterface]; This module is maintained by the Ansible Core Team. [core]� How can I write variables inside the tasks file in ansible (3) NOTE: Using set_fact as described below sets a fact/variable onto the remote servers that the task is running against. This fact/variable will then persist across subsequent tasks for the entire duration of your playbook.

In Your example, apache.yml is tasklist, but not playbook

In depends on desired architecture, You can do one of:

  1. Convert apache.yml to role. Then define tasks in roles/apache/tasks/mail.yml and variables in roles/apache/defaults/mail.yml (vars in defaults can be overriden when role applied)

  2. Set vars in play.yml playbook

play.yml

---
- hosts: 127.0.0.1
  connection: local
  sudo: false

  vars:
    url: czxcxz

  tasks:
     - include: apache.yml

apache.yml

- name: Download apache
  shell: wget {{url}} 

set_fact – Set host facts from a task — Ansible Documentation, Variables are set on a host-by-host basis just like facts discovered by the setup module. include_vars – Load variables from files, dynamically within a task: The More information related to variable precedence and which type of variable� Variables files¶ Ansible will look in Inventory directory and Playbook directory for directories named host_vars or group_vars. Inside those directories, you can put a single Variables file with the same name as a host or group (respectively) and Ansible will use those When/how variables are resolved definitions.

Whenever you have a module followed by a variable on the same line in ansible the parser will treat the reference variable as the beginning of an in-line dictionary. For example:

- name: some example
  command: {{ myapp }} -a foo

The default here is to parse the first part of {{ myapp }} -a foo as a dictionary instead of a string and you will get an error.

So you must quote the argument like so:

- name: some example
  command: "{{ myapp }} -a foo"

How to Include Variables in Ansible + Examples, Variables defined from included files and roles; Using Variables: Inside a template you automatically have access to all of the variables that are in scope for a host. these are a type of variable that are discovered, not set by the user. folder inside the role) is the most malleable and easily overridden. This is similar to writing Ansible tasks, even though it is a configuration file, and makes it easy to define variables and make changes. This can be expanded further if you are using group_vars, which allows you to define variables for all systems and specific groups (e.g., production vs. development). This makes it easier to manage variables

How to Use Variables in Ansible Playbook, I'm aware of two ways to include variables from another file in Ansible: The include_vars module (works in any list of tasks, such as a playbook or role); The name_vars.yml can be located in the same directory as hello_world.yml , or inside ./vars/ (I Despite having include_vars after set_fact , the output will look like this:. Organizing host and group variables ¶ Although you can store variables in the main inventory file, storing separate host and group variables files may help you organize your variable values more easily. Host and group variable files must use YAML syntax. Valid file extensions include ‘.yml’, ‘.yaml’, ‘.json’, or no file extension.

How to Work with Ansible Variables and Facts - Part 8, Variables in playbook are used via vars keyword, inventory file, hosts: all vars: salutations: Hello guys! tasks: - name: Ansible Variable Basic Usage debug: msg: "{{ salutations }}" This will display an output in JSON format Ansible Vault can encrypt any structured data file used by Ansible. This can include “group_vars/” or “host_vars/” inventory variables, variables loaded by “include_vars” or “vars_files”, or variable files passed on the ansible-playbook command line with -e @file.yml or -e @file.json. Role variables and defaults are also included.

Ansible - Variables, hosts: all vars: greeting: Hello world! tasks: - name: Ansible Basic Variable Example debug: msg: Another type of Ansible variable is the dictionary variable. Inside this directory, create a file(s) with a .fact extension. In Ansible there are multiple methods to create new empty files. You can also set different permissions, different group permissions, set the owner of the file, create files with content in them etc. Let us go through some ways in which it can be done. Creating an empty file in Ansible. You can Create an empty file using the file module.

Comments
  • It seems to be a better solution since you can use the fact as a variable.
  • Note this subtle difference compared with variables: This module allows setting new variables. Variables are set on a host-by-host basis just like facts discovered by the setup module. These variables will survive between plays.
  • Sidenote: it seems, that you can't change a variable in place using set_fact... so if the the name is already taken by a variable it won't modify that... which can be error prone to setting a variable to the same name on a higher level of a playbook. So be sure you won't do that (e.g. use a potentially unique name for example, not 'item').
  • Note on your note... Also, these facts are immutable (for the duration of the playbook), and cannot be changed once set. -- This isn't strictly true. You can overwrite a fact created with set_fact in an earlier task, you just can't overwrite a variable set in any other way... As an example, if you loop through a set of tasks, and at the start of each loop iteration you use set_fact: loop_item='{{ item }}', then use debug: msg='{{ loop_item }}', you'll see the fact changes with each loop iteration.
  • I thought register only worked with output from a command? How can I make a static variable in a role's roles/<role>/tasks/main.yml ?
  • @ThorSummoner you mean this like a regular variable? docs.ansible.com/… and maybe this? docs.ansible.com/playbooks_roles.html#role-default-variables
  • This solution is a total overkill. U send the var value over ssh to the remote host then retrieve it back, to use it locally on the control node!!
  • This is true, but it doesn't answer the question.