ansible: 'dict object' has no attribute 'port' - ansible

i'm trying to run the ansible-kafka playbook clone from the github repo and happen to be receiving below 'dict object' has no attribute 'port'
where am i going wrong with this
github repo : https://github.com/jaytaylor/ansible-kafka
playbook.yml
---
- hosts: dev
roles:
- ansible-kafka
vars:
zookeeper_hosts: "kafka-d1:2181,kafka-d2:2181,kafka-d3:2181"
kafka_zookeeper_hosts: [ 'kafka-d1','kafka-d2','kafka-d3' ]
kafka_zookeeper_port: 2181
kafka_hosts:
- dev
kafka_healthcheck_address: "kafka-d1:2181,kafka-d2:2181,kafka-d3:2181/kafka"
kafka_server_port: 9092
kafka_connection_string: [ 'kafka-d1','kafka-d2','kafka-d3' ]
- name: "Generate the kafka hosts connection string"
set_fact: kafka_connection_string="{{ kafka_hosts | join(':' ~ kafka_server.port ~ ',') }}:{{ kafka_server.port }}"
TASK [ansible-kafka : Generate the kafka hosts connection string]
******************************************************************************************** fatal: [kafka-d1]: FAILED! => {"msg": "The task includes an option
with an undefined variable. The error was: 'dict object' has no
attribute 'port'\n\nThe error appears to have been in
'/home/ahshan.md/ansible/ansible-kafka/tasks/kafka-cfg.yml': line 31,
column 3, but may\nbe elsewhere in the file depending on the exact
syntax problem.\n\nThe offending line appears

set_fact: kafka_connection_string="{{ kafka_hosts | join(':' ~ kafka_server_port ~ ',') }}:{{ kafka_server_port }}"
Underscore instead of a dot before port. Just the way you have declared your vars.

Related

dict object' has no attribute 'ansible_facts' while checking if a host is reachable in Ansible

I have the below playbook code as suggested in order to check if the inventory hosts are reachable.
---
- hosts: "{{ENV}}_*"
user: "{{USER}}"
tasks:
- fail:
msg: "Server is UNREACHABLE."
when: hostvars[item].ansible_facts|list|length == 0
with_items: "{{ ansible_play_hosts }}"
I get the below error running the playbook:
TASK [] ****************************************
fatal: [remotehost.com]: FAILED! => {"msg": "The conditional check 'hostvars[item].ansible_facts|list|length == 0' failed. The error was: error while evaluating conditional (hostvars[item].ansible_facts|list|length == 0): 'dict object' has no attribute 'ansible_facts'\n\nThe error appears to have been in '/web/aes/admin/playbooks/restart.yml': line 9, column 10, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - fail\n ^ here\n"}
Need suggestions on how to I fix this error ?

Ansible compare stdout with a variable

I have a questions since I am giving crazy. I want to compare stdout result to one variable storaged in my vars file.
For instance, instead of using the variable but the string ( The IP 22.22.2.2), it works:
Nets.yml
- List_nets_find:
- host 22.22.2.2
Playbook:
---
- name: Black List
hosts: routers
gather_facts: false
vars_files:
- vars/Nets.yml
tasks:
- name: Check the object-group
ios_command:
commands:
- command: "show object-group BLOCK-LIST"
register: output
tags:
- see
- debug:
msg: included
when: output.stdout is search('22.22.2.2')
tags:
- see
Result:
TASK [debug] **************************************************************************************************************************************************************************************
skipping: [Router1]
ok: [Router3] => {
"msg": "included"
}
ok: [Router2] => {
"msg": "included"
}
But instead of using the string, I use the Variable, it does not work.
- name: Black List
hosts: routers
gather_facts: false
vars_files:
- vars/Nets.yml
tasks:
- name: Check the object-group
ios_command:
commands:
- command: "show object-group BLOCK-LIST"
register: output
tags:
- see
- debug:
msg: included
when: output.stdout is search('{{List_net_find}}')
tags:
- see
This is the error:
fatal: [Router1]: FAILED! => {"msg": "The conditional check 'output.stdout is search('{{List_net_find}}')' failed. The error was: error while evaluating conditional (output.stdout is search('{{List_net_find}}')): 'List_net_find' is undefined\n\nThe error appears to be in '/home/ansible/Ios_ACL_LAB/object-group.yml': line 23, column 10, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n - see\n - debug:\n ^ here\n"}
fatal: [Router3]: FAILED! => {"msg": "The conditional check 'output.stdout is search('{{List_net_find}}')' failed. The error was: error while evaluating conditional (output.stdout is search('{{List_net_find}}')): 'List_net_find' is undefined\n\nThe error appears to be in '/home/ansible/Ios_ACL_LAB/object-group.yml': line 23, column 10, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n - see\n - debug:\n ^ here\n"}
fatal: [Router2]: FAILED! => {"msg": "The conditional check 'output.stdout is search('{{List_net_find}}')' failed. The error was: error while evaluating conditional (output.stdout is search('{{List_net_find}}')): 'List_net_find' is undefined\n\nThe error appears to be in '/home/ansible/Ios_ACL_LAB/object-group.yml': line 23, column 10, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n - see\n - debug:\n ^ here\n"}
Any suggestion? or other way to do it?
Thanks so much!
Your vars/Nets.yml does not declare a variable named List_nets_find, it declares an anonymous list whose first element is a dict whose only key is named List_nets_find
If you want a variable named that, then change - List_nets_find: to be List_nets_find:
Separately, the next question you are going to ask is "why does my search not match anything" and that's because List_nets_find is a list of str, so you will want to change that test to be output.stdout is search(List_nets_find | map("regex_escape") | join("|")), using the regex_escape filter to turn those strings into something that can be safely fed into a search alternation

Ansible, Issue while using variable content in playbook

I need to use variable defined for some hosts (in inventory), on another host.
Here i define it in my inventory
[mygroup:vars]
service_url=my_front_url
Where mygroup contain other groups, containing my hosts.
Then my playbook :
- name: Get variable
hosts: 127.0.0.1
tasks:
- debug:
var: hostvars[groups['{{ platform }}'][0]]['service_url']
- debug:
msg: "{{ hostvars[groups['\"{{ platform }}\"'][0]]['service_url'] }}"
Where platform is an extra-var (setting which "mygroup" to use)
and where 127.0.0.1 is my ansible host, distinct from my target hosts.
ex:
ansible-playbook test.yaml --extra-vars='platform=my-group'
TASK [debug] ********************************************************************************************************************************************************************
ok: [127.0.0.1] => {
"hostvars[groups['idi_se_prod'][0]]['service_url']": "my-front-url"
}
TASK [debug] ********************************************************************************************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute '\"{{ platform }}\"'\n\nThe error appears to have been in 'XXXX/ansible/test.yaml': line 6, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n var: hostvars[groups['{{ platform }}'][0]]['service_url']\n - debug:\n ^ here\n"}
If i set static group name in yaml, this work fine.
- name: Get variable
hosts: 127.0.0.1
tasks:
- debug:
var: hostvars[groups['{{ platform }}'][0]]['service_url']
- debug:
msg: "{{ hostvars[groups['mygroup'][0]]['service_url'] }}"
TASK [debug] ********************************************************************************************************************************************************************
ok: [127.0.0.1] => {
"hostvars[groups['my-group'][0]]['service_url']": "my-front-url"
}
TASK [debug] ********************************************************************************************************************************************************************
ok: [127.0.0.1] => {
"msg": "my_front_url"
}
It look like a syntax probleme but i tried so many ways that i think i could use some help.
Thank you
Nicolas
Everything inside {{ and }} is more or less just python, so don't use recursive templates like you have:
msg: "{{ hostvars[groups['\"{{ platform }}\"'][0]]['service_url'] }}"
instead just reference the variable like it is, a variable:
msg: "{{ hostvars[groups[platform][0]]['service_url'] }}"

ansible playbook - vars_files - "undefined variable" error

I am trying to use a vars file within a playbook - in this case to set a machine's hostname.
$ cat data_machine_test.yml
---
host_name: test02
$ cat os-build_hostname_set.yml
---
- name: Set hostname
hosts: all
gather_facts: no
vars_files:
- data_machine_test.yml
tasks:
- name: Set hostname
hostname:
name: "{{ host_name }}"
The variable file appears to be being read ...
PLAY [Set hostname] ************************************************************
META: ran handlers
Read vars_file 'data_machine_test.yml'
... but I keep getting the error ...
"msg": "The task includes an option with an undefined variable. The error was: 'host_name' is undefined\n\nThe error appears to have been in '/var/lib/awx/projects/_6__unix/os-build_hostname_set.yml': line 10, 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: Set hostname\n ^ here\n",
"_ansible_no_log": false
Code:
$ cat data_machine_test.yml
---
host_name: test02
$ cat os-build_hostname_set.yml
---
- name: Set hostname
hosts: all
gather_facts: no
vars_files:
- data_machine_test.yml
tasks:
- name: Set hostname
hostname:
name: "{{ host_name }}"
Output:
It appears that the variables file is being read ...
PLAY [Set hostname] ************************************************************
META: ran handlers
Read vars_file 'data_machine_test.yml'
... but I keep getting the error ...
"msg": "The task includes an option with an undefined variable. The error was: 'host_name' is undefined\n\nThe error appears to have been in '/var/lib/awx/projects/_6__unix/os-build_hostname_set.yml': line 10, 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: Set hostname\n ^ here\n",
"_ansible_no_log": false

using inventory_hostname as part of a dictionary lookup

I would like to use inventory_hostname as part of a dictionary lookup to configure servers that use the same template, but with different variables
/etc/ansible/hosts
[nodes]
node1 ansible_host=192.168.0.2
roles/role/defaults/main.yml
node1:
if1: eth1
ip1: 1.1.1.1/11
node2:
if1: eth4
ip1: 4.4.4.4/44
roles/role/tasks/main.yml
- name: test
debug:
msg: "{{ [inventory_hostname].[if1] }} end test "
expected this to display eth1 on the console but got the error
fatal: [node1]: FAILED! => {
"msg": "The task includes an option with an undefined variable. The error was: 'if1' is undefined\n\nThe error appears to have been in '/root/ansible/roles/role/tasks/main.yml': line 6, 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: test\n ^ here\n"
}
What you are looking for is vars:
- name: test
debug:
msg: "{{ vars[ansible_hostname]['if1'] }} end test"

Resources