I have the following playbook and struggling to understand how to pass it the application_name variable:
---
- name: Initial configuration for a {{ application_name }} server
hosts: all
vars:
- update_apt_cache: yes
vars_files:
- roles/base/defaults/main.yml
tasks:
- debug: msg="reading from group {{ groups }} and application name {{ application_name }}"
The issue is that the first application_name (in the name parameter) never works, though the second instance (defined in the task) works fine. How can I get the first instance to work as well? I've tried both defining the variable in the inventory...
[mygroup:vars]
application_name=my_application_name
...and in group_vars (./group_vars/mygroup/vars.yml)...
application_name: my_application_name
...but both of these do not fill the name parameter - only the {{ application_name }} found in the tasks section of the playbook.
Is there a way to fill in the first application_name variable?
I'm not sure if you can use the playbook name in the manner you want to. group_vars aren't loaded until after the playbook name is displayed. To fill in the application_name in the playbook name you would either have to define it in the playbook vars section
- name: Initial configuration for a {{ application_name }} server
hosts: all
vars:
- update_apt_cache: yes
- application_name: foo
or with extra-vars on the command line
ansible-playbook -e "application_name=foo" your_playbook.yml
Just a wild guess, did you tried to quote variable?
- name: 'Initial configuration for a {{ application_name }} server'
Related
I have an ansible playbook as follows:
---
- name: test variables
hosts: all
tasks:
- name: test
command: echo {{ ansible_role_dir }}
register: result
- name: show result
debug:
msg="{{ result.stdout }}"
#roles: #line12
# - "{{ ansible_role_dir }}/testrole" #line13
the variable ansible_role_dir is defined under group_vars/all.yaml.
if I run the playbook which comments out the line12 and line13, it shows the result of variable correctly. Obviously it knows where the variable ansible_role_dir is defined. But if I uncomment line12 and line13, it shows error ERROR! 'ansible_role_dir' is undefined. Why it does not know where the ansible_role_dir is defined this time?
Ansible does not like jinja tags without quotes. try this:
---
- name: test variables
hosts: all
tasks:
- name: test
command: "echo {{ ansible_role_dir }}"
register: result
- name: show result
debug:
msg="{{ result.stdout }}"
#roles: #line12
# - "{{ ansible_role_dir }}/testrole" #line13
EDIT:
sorry, i think i misread the question.
have you tried putting in the real path instead of the var, just to see if it works? usually role paths are given in ansible's config, i never saw them run with a direct path
my playbook structure looks like:
- hosts: all
name: all
roles:
- roles1
- roles2
In tasks of roles1, I define such a variable
---
# tasks for roles1
- name: Get the zookeeper image tag # rel3.0
run_once: true
shell: echo '{{item.split(":")[-1]}}' # Here can get the string rel3.0 normally
with_items: "{{ret.stdout.split('\n')}}"
when: "'zookeeper' in item"
register: zk_tag
ret.stdout:
Loaded image: test/old/kafka:latest
Loaded image: test/new/mysql:v5.7
Loaded image: test/old/zookeeper:rel3.0
In tasks of roles2, I want to use the zk_tag variable
- name: Test if the variable zk_tag can be used in roles2
debug: var={{ zk_tag.stdout }}
Error :
The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout'
I think I encountered the following 2 problems:
When registering a variable with register, when condition is added, this variable cannot be used in all groups. How to solve this problem? How to make this variable available to all groups?
is my title, How to use variables between different roles in ansible?
You're most likely starting a new playbook for a new host. Meaning all previous collected vars are lost.
What you can do is pass a var to another host with the add_host module.
- name: Pass variable from this play to the other host in the same play
add_host:
name: hostname2
var_in_play_2: "{{ var_in_play_1 }}"
--- EDIT ---
It's a bit unclear. Why do you use the when statement in the first place if you want every host in the play for it to be available?
You might want to use the group_vars/all.yml file to place vars in.
Also, using add_host should be the way to go as how I read it. Can you post your playbook, and the outcome of your playbook on a site, e.g. pastebin?
If there is any chance the var is not defined because of a when condition, you should use a default value to force the var to be defined when using it. While you are at it, use the debug module for your tests rather than echoing something in a shell
- name: Debug my var
debug:
msg: "{{ docker_exists | default(false) }}"
I am new to ansible, I am writing a small playbook where it has to collect the fact value from the destination host and use that as a variable within the play. Can someone help me how to do that.
---
- hosts: all
gather_facts: True
become: true
become_method: sudo
become_user: root
vars:
BUILD_PATH: /opt/services/dev
pre_tasks:
- setup:
filter: ansible_env
- set_fact:
tag: "{{ ansible_env.DATA_AGGREGATOR_ENV }}"
- debug: var=ENV
tasks:
- name: Copy to Build to DATA
shell: cp -pr {{ BUILD_PATH }} {{ ENV }}
Note: DATA_AGGREGATOR_ENV is environmental variable defined in all servers and the value vary from one server to other.
You set the variable tag, but you use the variable ENV.
You have to set the ENV variable, if you want to use the ENV variable.
- set_fact:
ENV: "{{ ansible_env.DATA_AGG_ENV }}"
Or you have to use the tag variable, if you have set the tag variable.
shell: cp -pr {{ BUILD_PATH }} {{ tag }}
btw: the filter line lacks a space.
I have a playbook that contains roles for localhost and roles for remote hosts.
In one of the localhost roles I set a fact called git_tag.
I want to use this fact in a template for the remote hosts.
I tried:
- name: Read Version
set_fact:
git_tag: "{{ package_json.stdout | from_json | json_query('version')}}"
delegate_to: "test-server"
But when Ansible reaches the role that reads the template that has {{ git_tag }} it says that git_tag is undefined.
I'm sure I'm doing something wrong. How can I do it?
You should use a hostvars magic variable:
{{ hostvars['localhost']['git_tag'] }}
you can use this
{{ hostvars['test-server']['git_tag'] }}
fairly new to ansible.
having following role e.g.: my-role - i have trouble overriding the default variables from the playbook
follwing files:
my-role/tasks/main.yml
my-role/defaults/main.yml
sample-playbook.yml
my-role/tasks/main.yml
- name: "Add Test User"
user: name={{ my_config_test_user }} comment="{{ my_config_test_user }}" group={{ my_config_test_user }}
my-role/defaults/main.yml
my_config_test_user: "test"
playbook:
- name: TestCase
hosts: all
remote_user: root
vars:
my_config_test_user: "override"
roles:
- my-role
in the task the value of my_config_test_user stays test instead of my expected result override
any hints?
regards
In the current version of Ansible order of precedence says that the value of my_config_test_user should be override not test so I think you probably have a typo somewhere. Maybe the variable is not spelled correctly?
I suggest removing defaults/main.yml temporarily to ensure that you do not get an undefined variable error. I would also suggest using the debug module to check the value of the variable in your tasks/main.yml
- debug: var={{ my_config_test_user }}
For reference here is the order of precedence (starting with the greatest) in the current version of Ansible:
Vars set on the command line -e foo=set_on_cmd_line
Vars set in the vars_files: block in the play
Vars set in the vars: block in the play
Vars set in host_vars/
Vars set in group_vars/
Role default vars roles/.../defaults/main.yml