Skip to content

Instantly share code, notes, and snippets.

@zeitounator
Last active March 16, 2021 16:55
Show Gist options
  • Save zeitounator/5c6004a107c3d4a6e349ea2824badb5b to your computer and use it in GitHub Desktop.
Save zeitounator/5c6004a107c3d4a6e349ea2824badb5b to your computer and use it in GitHub Desktop.
---
all:
vars:
ansible_connection: local
children:
monitoring:
children:
dev:
vars:
env: dev
hosts:
host_1:
prod:
vars:
env: prod
hosts:
host_2:
dev_vars:
hostname: monitor.dev.com
pass: 12345
prod_vars:
hostname: monitor.prod.com
pass: 6789
user: monitor
- hosts: monitoring
gather_facts: false
vars_files:
- "vars/env_vars.yml"
vars:
env_vars: "{{ lookup('vars', env + '_vars') }}"
tasks:
- name: Create user
debug:
msg: "I would create user {{ user }}"
- name: Copy file to relevant environment
debug:
msg: "I would copy template with values {{ env_vars.hostname }} and {{ env_vars.pass }}"
$ ansible-playbook -i inventories/monitoring.yml monitoring_playbook.yml
PLAY [monitoring] **********************************************************************************************************************************************************************************************************************
TASK [Create user] *********************************************************************************************************************************************************************************************************************
ok: [host_1] => {
"msg": "I would create user monitor"
}
ok: [host_2] => {
"msg": "I would create user monitor"
}
TASK [Copy file to relevant environment] ***********************************************************************************************************************************************************************************************
ok: [host_1] => {
"msg": "I would copy template with values monitor.dev.com and 12345"
}
ok: [host_2] => {
"msg": "I would copy template with values monitor.prod.com and 6789"
}
PLAY RECAP *****************************************************************************************************************************************************************************************************************************
host_1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
host_2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
@sivanagireddyb
Copy link

sivanagireddyb commented Mar 9, 2021

You are printing values as 'env_vars.hostname', I know this way. I am trying to use the variable as it is 'hostname', not env_vars.hostname. I am looking is there a way to load the array for the task and read the values as a string. like below.
I know this won't work, just a rough plan.

- name: print values
  debug: msg={{ hostname }}
  when: "'dev' in group_names"
  vars: {{ dev_vars }}

- name: print values
  debug: msg={{ hostname }}
  when: "'prod' in group_names"
  vars: {{ prod_vars }}

'
Let me make this simple. Forgot about environments, Let's say there is a template & variable file as below and how can we copy it, two hosts, without changing the template & without declaring sub groups in inventory.

Template:

hostname={{hostname}}
pass={{password}}

varsfile:

host1_vars
 hostname: server1 
 pass: 12345

host2_vars:
  hostname: server2
  pass: 9875

Inventory

[dev]
host1
host2

@zeitounator
Copy link
Author

What you are describing is exactly how host_vars and group_vars work but you don't want to use them. Well then... good luck ! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment