i have this ansible structure its part of large code base :
it seems that when running the main playbook the defaults/main.yml app_type:game
don't pass on to the current_version.yml and give error
why it doesn't use the default var in defaults/main.yml
also when i pass the same variable name from --extra-vars it gives me the same error
when i print the variable from tasks/main.yml
it prints the right value
what is wrong with the include_role?
C:.
├───defaults
│ main.yml
│
└───tasks
main.yml
current_version.yml
legacy_version.yml
main.yml
---
app_type: game
tasks/main.yml
#- name: set lagacy_package true
# set_fact:
# lagacy_package: true
- name: select legacy
include_role:
tasks_from: legacy_version
name: package_install
public: true
vars:
app_type: "{{ app_type }}"
when: (lagacy_package is defined) and (lagacy_package == true)
- name: select Current
include_role:
tasks_from: current_version
name: package_install
public: true
vars:
app_type: "{{ app_type }}"
when: lagacy_package is not defined
current_version.yml
- debug:
msg: "System 2 ################ {{ app_type }} ######################################## "
error from current_version.yml
2020-07-01 17:04:09,118 p=11546 u=ec2-user n=ansible | fatal: [10.0.5.71]: FAILED! =>
msg: 'An unhandled exception occurred while templating ''{{ app_type }}''. Error was a <class ''ansible.errors.AnsibleError''>, original message: An unhandled exception occurred while templating ''{{ app_type }}''. Error was a <class ''ansible.errors.AnsibleError''>, original message: An unhandled exception occurred while templating ''{{ app_type }}''. Error was a <class ''ansible.errors.AnsibleError''>, original message: An unhandled exception occurred while templating ''{{ app_type }}''. Error was a <class ''ansible.errors.AnsibleError''>, original message: An unhandled exception occurred while templating ''{{ app_type }}''. Error was a <class ''ansible.errors.An
Problem is in the below code of "select legacy" task which is going to a loop for assigning app_type with itself.
app_type: "{{ app_type }}"
Changing the name of either of the variables should work like:
app_type: "{{ other_app_type }}"
Related
We switched to ansible 2.10
Before it was azure_rm_networkinterface_facts (working) now is azure_rm_networkinterface_info
- name: "Get facts for network interface by it's name"
azure_rm_networkinterface_facts:
resource_group: "{{ target_resourcegroup }}"
name: "{{ target_nic_name }}"
- name: "Define private IP address"
set_fact:
private_ip_address: "{{ ansible_facts | json_query(query) }}"
vars:
query: "azure_networkinterfaces[0].properties.ipConfigurations[0].properties.privateIPAddress"
when: azure_networkinterfaces|length > 0
Error I get is:
4 TASK [azure_preconditions : Define private IP address] *************************
00:01:38.844 [0;31mfatal: [40.118.86.58]: FAILED! => {"msg": "The conditional check 'azure_networkinterfaces|length > 0' failed. The error was: error while evaluating conditional (azure_networkinterfaces|length > 0): 'azure_networkinterfaces' is undefined\n\nThe error appears to be in '/var/lib/jenkins/workspace/PA-28544-ansible-version-upgrade/roles/azure_preconditions/tasks/main.yml': line 143, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: \"Define private IP address\"\n ^ here\n"}[0m
00:01:38.844
azure_networkinterfaces seems to be a return value of azure_rm_networkinterface_facts so it's normal that it doesn't exist...
I'd write a playbook like this:
- name: "Get facts for network interface by it's name"
azure_rm_networkinterface_facts:
resource_group: "{{ target_resourcegroup }}"
name: "{{ target_nic_name }}"
register: output
I suggest you debug the output variable to check your return values. I don't know the version of Azure you are using and a lot of changes can exist depending on it.
- name: "Display return value"
debug:
msg: "{{ output }}"
You could play with output.azure_networkinterfaces or output.networkinterfaces
Following the last documentation, its seems azure_rm_networkinterface_fact is deprecated, replaced by azure_rm_networkinterface_info
azure_rm_networkinterface_info
I'm new to Ansible and trying to get a service state where the service name is dynamic and set by set_fact before in the playbook.
How can you build a variable within another variable?
I wish I could use something like that to display my service state :
{{ ansible_facts.services['{{ servicename }}'].state }}
But well it doesn't work.
So I tried this way with vars :
- name: set service name
ansible.builtin.set_fact:
servicename: "'myservice'"
when: ansible_distribution_major_version == "7"
- name: print service state
debug: msg={{ vars['ansible_facts.services[' + servicename + '].state'] }}
vars:
servicename: "{{ servicename }}"
I got the following error :
fatal: [myhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute u\"ansible_facts.services['myservice'].state\"\n\nThe error appears to be in '/etc/ansible/playbook/myplaybook.yaml': line 20, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: print service state\n ^ here\n"}
When the following works just fine :
- name: print service state
debug:
msg: "{{ ansible_facts.services['myservice'].state }}"
Inside a {{ … }} block, you can access the variables directly. And from my point of view, you can leave the ansible_facts out, as well as the variable assignment in the second task.
This will do what you want (and as Zeitounator already wrote):
- hosts: localhost
vars:
services:
myservice:
state: foo
tasks:
- name: set service name
ansible.builtin.set_fact:
servicename: "myservice"
- name: print service state
debug:
msg: "{{ services[servicename].state }}"
Team,
Am trying to variablize all the parameter part of the aws_secret plugin. But not able to assign variable for the region. Have tried something like '{{ aws_secret_region }}' , '"{{ aws_secret_region }}"', it's throwing error as Provided region_name '{{aws_secret_region}}' doesn't match a supported format.". When trying to pass the region as the environment variable or arg, it's throwing ```You must specify a region`` error Any suggestions?
Code :
hosts: localhost
vars:
aws_secret_key: "{{ aws_secret_key }}"
db_password: "{{ lookup('aws_secret', '{{ aws_secret_name }}', region='us-east-1') | from_json | json_query('\"' + aws_secret_key + '\"') }}"
Error (when passing the region as env var):
fatal: [10.1.24.158]: FAILED! => {"msg": "An unhandled exception occurred while templating '{{ lookup('aws_secret', '{{ aws_secret_name }}') | from_json | json_query('\"' + aws_secret_key + '\"') }}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: An unhandled exception occurred while running the lookup plugin 'aws_secret'. Error was a <class 'botocore.exceptions.NoRegionError'>, original message: You must specify a region."}
I'm trying to write a playbook that will load vars from a group vars file then check if a variable exists
my playbook is like this:
---
- hosts: "{{ target }}"
roles:
- app
tasks:
- name: alert if variable does not exist
fail:
msg: "{{ item }} is not defined"
when: "{{ item }}" is not defined
with_items:
- country
- city
- street
...
My inventory file contains
[app]
ansible-slave1
ansible-slave2
[db]
ansible-db
[multi:children]
app
db
and I have the roles/app/vars/main.yml containing
country: "France"
city: "Paris"
What I was expecting is the playbook to output "street is not defined" but I have a syntax issue I can't resolve
[vagrant#ansible-master vagrant]$ ansible-playbook --inventory-file=ansible_master_hosts test_variables.yml --extra-vars "target=ansible-slave1" --syntax-check
ERROR! Syntax Error while loading YAML.
The error appears to have been in '/vagrant/test_variables.yml': line 10, column 24, but may be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
msg: "{{ item }} is not defined"
when: "{{ item }}" is not defined
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
I'd be happy with any hints.
Thanks
You have "" in invalid place of "when" statement. This should be like this:
msg: "{{ item }} is not defined"
when: "{{ item }} is not defined"
So the output will be:
failed: [hostname] (item=street) => {"changed": false, "item": "street", "msg": "street is not defined"}
there is on open issue conditional is defined fails to capture undefined var .
as a workaround I'd suggest to change the where condition to the following:
when: "{{ item }}" == ""
How can I access a variable of other host? I'd like to access the slack_token varaiable of my localhost on the working_host.
- hosts: localhost
vars:
slack_token: 123123123
tasks:
- block:
- name: test
debug: msg="{{ slack_token }}"
- hosts: "{{ working_host }}"
vars:
slack_token: "{{ hostvars['localhost']['slack_token'] }}"
tasks:
- block:
- name: test2
debug: msg={{ slack_token }}
The error message:
fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field
'args' has an invalid value, which appears to include a variable that
is undefined. The error was: {{ hostvars['localhost']['slack_token']
}}: 'dict object' has no attribute 'slack_token'
Any idea?
Just answered a somewhat same question in my previous post.
Here's what I used:
set_fact:
myVar: "{{ hostvars[groups['all'][0]]['slack_token'] | default(False) }}"
But you're using two plays in a playbook.
You can also try to copy a file to a machine stating the fact.
To access slack_token from everywhere, either:
pass it as extra variable with -e slack_token=zzzz
define it in your inventory under all group