0

I am newbie to ansible. I am trying to perform some deployment task in 142.23.9.23 via help of ansible using jenkins. I am hitting below error FAILED! => {"msg": "The field 'become_pass' has an invalid value, which includes an undefined variable. The error was: 'tomcat_password' is undefined"}.

I am open to new suggestion as well.

Below is the details

Directory

ansible
   |---- group_vars
           |---- MAIN
                  |---- vault.yml
   |---- hosts
           |---- host-details.yml
   |---- roles
   |----- my-playbook.yml

my-playbook.yml

- hosts: app-server
  tasks:
    - name: Print message
      debug: msg="test playbook version ansible_host = {{ ansible_host }}, ansible_ssh_user = {{ansible_ssh_user}}"
  vars_files:
    - group_vars/COMMON/vault.yml

host-details.yml


[app-server]
142.23.9.23 ansible_host=142.23.9.23 ansible_ssh_user=myuser ansible_become=yes ansible_become_user=myuser ansible_become_pass='{{ tomcat_password }}'

ansible-vault edit vault.yml

tomcat_password: password1
jenkins_password: password2
  • Unfortuntelly I cannot reproduce your error; everything works as expected in a testing case I've created from your description. However, can you please try to use double quotes instead of single ones as official Ansible documentation recommends (i.e. `"{{ tomcat_password }}"` ) and let us know the result? Also if that wouldnt work, I'd suggest including this variable into your debug message: `debug: msg="test playbook with tomcat_password = {{ tomcat_password }} ansible_host = {{ ansible_host }} ... "` – miwa May 17 '20 at 01:18

1 Answers1

0

I think you should ensure that you understand how group_vars and host_vars works: https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html

The point here, is that your app-server is a group, not an host, and it seems to not being in the group MAIN nor COMMON. You don't have to force the inclusion of vars from group_vars, adding hosts in the groups is enough.

Your inventory should be more like this:

[app-server]
142.23.9.23 ansible_host=142.23.9.23 ansible_ssh_user=myuser ansible_become=yes ansible_become_user=myuser ansible_become_pass='{{ tomcat_password }}'

[COMMON:children]
app-server

[MAIN:children]
app-server

But anyway, please read carefully how to make an inventory as it will solve most of your problems.

Sebcworks
  • 139
  • 2
  • 7