Ansible 'aws_secret ' lookup variable - ansible

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."}

Related

Error 'list object' has no attribute 'name'

I have some problems with understanding how i can get value from array
I declared variable names using this code
- name: 'Collecting values'
set_fact:
names:
- { name: 'Primary', host: '{{ hostname1 }}'}
- { name: 'Secondary', host: '{{ hostname1 }}'}
After it i successfully used this var in the loop
- include_tasks: ./sometask1.yml
vars:
hostname_name: '{{ item_outer.name }}'
hostname_host: '{{ item_outer.host}}'
loop: '{{ names}}'
loop_control:
loop_var: item_outer
But in the second task
- import_tasks: ./sometask2.yml
vars:
hostname: '{{ names.name }}'
I have an error
fatal: [example.domain.com]: FAILED! => {
"msg": "'list object' has no attribute 'name'"
}
What i'm doing wrong?
The answer was provided in the comments:
names is a list of dictionaries, all those dictionaries having a name attribute, but the list itself does not have a name attribute. What are you trying to access / achieve with that non-working task? – β.εηοιτ.βε 2023-01-30 09:03
I'm understand, thank you. In real life second task looks like names: '{{ names.name | join(',') }}'. I need to create string with all names separated by ',' – Po_temkin 2023-01-30 09:09
Then: {{ names | map(attribute='name') | join(',') }} – β.εηοιτ.βε
2023-01-30 09:15

AnsibleError: template error while templating string: expected token 'end of print statement', got '{'

The following is a task, which is throwing me an error because jinja2 templating doesnt support this.
- name: Print the ip address
debug:
msg: "{{ ansible_{{ item }}['ipv4']['address'] }}"
with_items: "{{ ansible_interfaces|reject('search', 'lo')|list|sort }}"
The error thrown is:
"msg": "template error while templating string: expected token 'end of print statement', got '{'. String: {{ ansible_{{ item }}['ipv4']['address'] }}"
Any pointers on how to solve this issue?
You cannot use jinja2 expansion when you are already inside a jinja2 expansion expression. In other words mustaches don't stack.
In your case you can use the vars lookup to fetch your dynamically named var:
- name: Print the ip address
vars:
interface_var_name: "ansible_{{ item }}"
debug:
msg: "{{ lookup('vars', interface_var_name)['ipv4']['address'] }}"
with_items: "{{ ansible_interfaces | reject('search', 'lo') | list | sort }}"
Use lookup plugin vars. For example
- name: Print the ip address
debug:
msg: "{{ my_ifc.ipv4.address|default('Undefined') }}"
loop: "{{ ansible_interfaces|reject('search', 'lo')|list|sort }}"
vars:
my_ifc: "{{ lookup('vars', 'ansible_' ~ item) }}"
gives
ok: [localhost] => (item=eth0) =>
msg: 10.1.0.27
ok: [localhost] => (item=wlan0) =>
msg: Undefined

ansible varibals do not pass to include_role

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 }}"

Trying to use 'selectattr in' filter with Ansible playbook but it fails as per the playbook below

I am trying to use textfsm to parse the data for admin show platform in that anything with a state of 'IOS XR RUN' 'READY' or 'OK' will pass & anything else will report failed. I'm using the selectattr in option but get the following error - "msg": "The task includes an option with an undefined variable. The error was: 'intf_tests_pass' is undefined. Any ideas why this is failing as the working_state variable is defined.
name: Collect admin show platform info
iosxr_command:
commands:
- admin show platform
provider: "{{ cli }}"
register: platform_result
when: device_os == 'cisco-ios-xr'
name: retrieve status to be returned
set_fact:
working_state: ['IOS XR RUN', 'READY', 'OK']
name: parse platform_result
textfsm_parser:
file: templates_textfsm/{{ device_os }}/admin_show_platform.template
content: "{{ platform_result.stdout.0 }}"
name: platform_state
when:
platform_result.stdout is defined
platform_result.stdout[0] != none
platform_result.stdout[0] != ""
name: identify platform_result that passed
set_fact:
platform_tests_pass: "{{ ansible_facts.platform_state |
selectattr('STATE', 'in', 'working_state') | list }}"
when: ansible_facts.platform_state is defined
name: identify platform_result that failed
set_fact:
platform_tests_fail: "{{ ansible_facts.platform_state |
difference(platform_tests_pass) | list}}"
when: ansible_facts.platform_state is defined
debug:
msg:
"{{ intf_tests_pass }}"
"{{ intf_tests_fail }}"
Thanks,
Brian
Based on the output you posted, you saved facts to platform_tests_pass and platform_tests_fail, not intf_tests_pass and intf_tests_fail, so those variables are never actually defined.

Ansible: How can I access a variable of other host?

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

Resources