Home > Software engineering >  Ansible refuses to print variable with debug even though it obviously exists
Ansible refuses to print variable with debug even though it obviously exists

Time:01-26

Ansible will not print my variable, even though it obviously exists in debug. I am calling the correct settings file as well.

Here is my playbook:

############### Cust VM ###############
- name: Perform changes on customer name space vm
  hosts: custvm
  gather_facts: no
  roles:
    - { role: cust_vm_create }

Here is the role

- name: Include vars
  include_vars:
      file: ../../../settings.yaml
      name: settings

- debug:
    var: settings.cust_int

Here is the settings file

---
cust_int: 'ens224'
cust_sub_int: 'ens224.{{ cust_int }}'

Here is the inventory

[custvm]
10.10.10.10

Here is the group_var file

cust: "0"
ansible_user: 'username'
ansbile_ssh_pass: 'vaultpasswordused'
ansbile_become_pass: 'vaultpasswordused'

Here is the command I ran:

ansible-playbook -i ./src/ansible/inventory.ini -e cust=20 ./src/ansible/customers_create.yaml --ask-vault-pass -T 40 -vvv --limit custvm

And here is the output

ansible-playbook 2.9.6
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/me/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 3.8.10 (default, Nov 14 2022, 12:59:47) [GCC 9.4.0]
Using /etc/ansible/ansible.cfg as config file
Vault password:
host_list declined parsing /home/me/ansible/inventory.ini as it did not pass its verify_file() method
script declined parsing /home/me/src/ansible/inventory.ini as it did not pass its verify_file() method
auto declined parsing /home/me/src/ansible/inventory.ini as it did not pass its verify_file() method
yaml declined parsing /home/me/src/ansible/inventory.ini as it did not pass its verify_file() method
Parsed /home/me/src/ansible/inventory.ini inventory source with ini plugin

PLAYBOOK: customers_create.yaml ****************************************************************************************
2 plays in ./src/ansible/customers_create.yaml

PLAY [Perform changes on customer name space vm] ***********************************************************************
META: ran handlers

TASK [cust_vm_create : Include vars] ***********************************************************************************
task path: /home/me/src/ansible/roles/cust_vm_create/tasks/main.yaml:1
ok: [10.10.10.10] => changed=false
  ansible_facts:
    settings:
      cust_int: ens224
  ansible_included_var_files:
  - /home/me/src/ansible/roles/cust_vm_create/tasks/../../../settings.yaml

TASK [cust_vm_create : debug] ******************************************************************************************
task path: /home/me/src/ansible/roles/cust_vm_create/tasks/main.yaml:6
ok: [10.10.10.10] =>
  settings.cust_int: 'VARIABLE IS NOT DEFINED!: ''cust_int'' is undefined'
META: ran handlers
META: ran handlers

PLAY [Perform changes on customer edge router] *************************************************************************
skipping: no hosts matched

PLAY RECAP *************************************************************************************************************
10.10.10.10               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

I have tried all manor of debug syntax as well:

- debug:
    msg: {{ settings.cust_int }}

- debug:
    var: cust_int

- debug:
    msg: {{ settings.cust_int }}

- debug:
    msg: "{{ settings.cust_int }}"

- debug: msg="{{ settings.cust_int }}"

- debug: msg="{{ cust_int }}"

Why, no matter what I try, is ansible not printing the variable even though it obviously exists from debug?

EDIT: UPdated typos, forgot to include the sub int variable I made.

CodePudding user response:

Based on the verbose logs of the include task

ok: [10.200.2.12] => changed=false
  ansible_facts:
    settings:
      custint: ens224

It looks like the variable name in settings.yaml has a typo in it and is missing the underscore. ie. custint vs cust_int.

CodePudding user response:

I figured it out. I forgot to include a variable I created which was callled cust_sub_int

cust_int: 'ens224'
cust_sub_int: '{{ cust_int }}.{{ cust }}`

The problem is cust_sub_int, the concatination is wrong, so it made the playbook fail, and the error was not clear. I still don't know how to concatenate these two, but I will create a new post for that.

  • Related