I am moving a lot of old scripts used to configure a computer room into ansible, and it really has improved the workflow. Currently, there I have several playbooks, and I need to share a common config among them. But in one task I have faced a problem: I need a hostname/ip to be a variable in the inventory. I have read a lot of tutorials and docs and maybe I am dumb or very tired, but I have not found yet a solution after many hours, it seems that it is not possible. Dynamics inventories, group_vars and so on look similar but actually are different from what I require here. I have created a mwe to easy showing the case. This mwe is a subset but the main idea remains: vars inside vars/main.yml are going to be shared among various playbooks (easy) and inventories (the question here). Thanks in advance.
- ansible.cfg:
[ssh_connection]
pipelining = True
control_path = /tmp/ansible-ssh-%%h-%%p-%%r
[defaults]
interpreter_python = python3
- inventory (ini format, also tried yaml)
[master]
192.168.10.1 ansible_connection=local
[package_clients]
192.168.10.[3:29]
[package_server]
{{PACKAGE_SERVER}} # <--- This var commes from vars/main.yml
- site.yaml:
---
- name: Configuring package server (exports spack packages)
hosts: package_server
gather_facts: false
vars_files:
- vars/main.yml
tasks:
- name: Printing package server name
debug:
msg: "PACKAGE_SERVER variable -> {{PACKAGE_SERVER}}"
- name: Creating package dir (this throws an error sin PACKAGE_SERVER is not replaced in inventory)
file:
path: /packages
state: directory
- vars/main.yml
# Server internal ip
BASE_SERVERIP: '192.168.10'
# Package Server: Simply shares a directory with spack and packages
PACKAGE_SERVER: '{{BASE_SERVERIP}}.2'
When I run this as
ansible-playbook -i inventory site.yml --check
I get the following error:
PLAY [Configuring package server (exports spack packages)] *******************************************************************************************
TASK [Printing package server name] ******************************************************************************************************************
ok: [{{PACKAGE_SERVER}}] => { "msg": "PACKAGE_SERVER variable -> 192.168.10.2"
}
TASK [Creating package dir (this throws an error sin PACKAGE_SERVER is not replaced in inventory)] ***************************************************fatal: [{{PACKAGE_SERVER}}]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname {{package_server}}: Name or service not known", "unreachable": true}
PLAY RECAP *******************************************************************************************************************************************
{{PACKAGE_SERVER}} : ok=1 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0