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
Related
I need to modify a playbook that expects to have a yaml file loaded via --extra-vars because the extra vars file needs to be generated with user input every time it's used.
This playbook currently overwrites a file and then modifies it, replacing placeholders with the values in my-extra-vars.yml. This is a destructive step; if a variable is not defined then the placeholder is left untouched.
This is working, sort of, but the error message is not what I expect or want.
vars:
- required_vars:
- 'varname1'
- 'varname2'
tasks:
- name: Check for required variables
fail:
msg: "variable '{{item}}' is not defined"
when: lookup('vars', item) is undefined
with_items: "{{required_vars}}"
Running the playbook as ansible playbook -K --extra-vars '#test-vars.yml' test.yml with only varname1 defined gives me the following output.
TASK [Check for required variables] **************************************************************************************
skipping: [127.0.0.1] => (item=varname1)
fatal: [127.0.0.1]: FAILED! => {"msg": "The conditional check 'lookup('vars', item) is undefined' failed. The error was: error while evaluating conditional (lookup('vars', item) is undefined): No variable found with this name: varname2\n\nThe error appears to be in '/home/me/projects/test/test.yml': line 21, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: Check for required variables\n ^ here\n"}
If all variables are defined, then there is no syntax error. Otherwise, it is dying, so at least that aspect of the problem is managed.
Because the lookup, itself is failing, so the when condition fails and your tasks is not failing as it normally should.
A way around this failure would be to use the default of the vars lookup, but from there it all depends what are your acceptable values for those variables.
An example, considering I do not want blank string in them would be:
- name: Check for required variables
fail:
msg: "variable '{{item}}' is not defined"
when: lookup('vars', item, default='') == ''
loop: "{{ required_vars }}"
vars:
required_vars:
- 'varname1'
- 'varname2'
Which would yield your expected:
TASK [Check for required variables] **************************************
failed: [localhost] (item=varname1) => changed=false
ansible_loop_var: item
item: varname1
msg: variable 'varname1' is not defined
failed: [localhost] (item=varname2) => changed=false
ansible_loop_var: item
item: varname2
msg: variable 'varname2' is not defined
A better way, though, is to use the undef keyword, this way, if you do template the variable, they will automatically raise the undefined hint provided.
Given the playbook
- hosts: localhost
gather_facts: no
vars:
varname1: "{{ undef(hint='Please provide a value for `varname1`') }}"
tasks:
- debug:
msg: "{{ varname1 }}"
It yields:
atal: [localhost]: FAILED! =>
msg: |-
The task includes an option with an undefined variable.
The error was: {{ undef(hint='Please provide a value for `varname1`') }}:
Please provide a value for `varname1`
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 ?
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
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"
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.