using inventory_hostname as part of a dictionary lookup - ansible

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"

Related

azure-network_interface says variable is undefined

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

Ansible with cisco devices

I try to test my audit activity on cisco device by the following task;
tasks:
- name: 1.1.1 Enable 'aaa new-model' (Scored)
ios_command:
commands: show running-config | incl aaa new-model
register: output1
- name: 1.1.1 Check results
delegate_to: localhost
lineinfile:
path: /tmp/Summary.txt
line: "1.1.1|Fail"
when: output1.stdout is 'no aaa new-model'
- name: 1.1.1 Check results
delegate_to: localhost
lineinfile:
path: /tmp/Summary.txt
line: "1.1.1|Pass"
when: output1.stdout is not 'no aaa new-model'
This activity will run ios command and register values after that I will check for condition and paste the result to my localhost(linux)
but I got this errors;
FAILED! => {"msg": "The conditional check 'output1.stdout is 'no aaa new-model'' failed. The error was: template error while templating string: expected token 'name', got 'string'. String: {% if output1.stdout is 'no aaa new-model' %} True {% else %} False {% endif %}\n\nThe error appears to be in '/etc/ansible/Cisco/1.1.yaml': line 15, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n register: output1\n - name: 1.1.1 Check results\n ^ here\n"}
Can anyone tell me what happens?
PS. My ansible version is 2.9.3
The result you stored in the "output1" variable is contained in 'output1.stdout[0]'.
'output1.stdout' is a list, and 'output1.stdout[0] is the string you are after.

Ansible - set_fact syntax issue

In the below playbook, can someone please tell me what is wrong with the syntax of the final set_fact statement. If i explicitly specify the hostname R9 (as per the first set_fact), then it works without issue. But if I assign a variable to have a value of R9, and then use this in the final set_fact statement, I get the error shown below. Argh.
---
- hosts: all
gather_facts: no
vars:
ansible_network_os: ios
ansible_connection: network_cli
tasks:
- ios_command:
commands: show ip arp
register: results
- set_fact:
some_var: "{{ hostvars['R9'].results }}"
- set_fact:
hostname: "R9"
- set_fact:
another_var: "{{ hostvars['{{hostname}}'].results }}"
- debug:
var=some_var
- debug:
var=another_var
Error:
fatal: [R7]: FAILED! =>
msg: |-
The task includes an option with an undefined variable. The error was: "hostvars['{{hostname}}']" is undefined
The error appears to be in '/root/ansible/Rapid/testsetfact.yml': line 20, column 6, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- set_fact:
^ here
fatal: [R9]: FAILED! =>
msg: |-
The task includes an option with an undefined variable. The error was: "hostvars['{{hostname}}']" is undefined
The error appears to be in '/root/ansible/Rapid/testsetfact.yml': line 20, column 6, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- set_fact:
^ here
you can use the inventory_hostname for your purpose:
- set_fact:
another_var: "{{ hostvars[inventory_hostname].results }}"

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

Resources