Last active
March 16, 2021 16:55
-
-
Save zeitounator/5c6004a107c3d4a6e349ea2824badb5b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
all: | |
vars: | |
ansible_connection: local | |
children: | |
monitoring: | |
children: | |
dev: | |
vars: | |
env: dev | |
hosts: | |
host_1: | |
prod: | |
vars: | |
env: prod | |
hosts: | |
host_2: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dev_vars: | |
hostname: monitor.dev.com | |
pass: 12345 | |
prod_vars: | |
hostname: monitor.prod.com | |
pass: 6789 | |
user: monitor |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- 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 }}" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 |
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
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.
'
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:
varsfile:
Inventory