napp
napp

Reputation: 49

need to get two different config files with different set of variables using same template and playbook Ansible

I want to deploy the code in two systems....

group_vars/all has different variables, eg.

---
# group variable for webservers
username: abc

hosts i have windows group and two machine ips..

[webservers]
10.0.1.2

[databases]
10.0.1.3

role/tasks/playbook i have play which will execute on both machines...

---
- hosts: all
  roles:
    - base

Current:

Need:

is it possible?

Upvotes: 1

Views: 179

Answers (2)

udondan
udondan

Reputation: 60079

Beside group_vars there are host_vars, where you can define individual variables per host. Everything that is host dependent should be stored there and your username appears to fit there.

For hosts other than those two you can fall back to variables defined in group_vars or set a default value in your template:

{{ username | default("AxelFoley")}}

Upvotes: 3

Conrado
Conrado

Reputation: 1412

It's possible to set different variables in a number of different places. You should look at ansible's variable precedence documentation

The most common location for connection usernames is directly in the hosts file.

[webservers]
10.0.1.2      ansible_ssh_user=vagrant

[databases]
10.0.1.3      ansible_ssh_user=ubuntu

If your username variable is used to configure some service consumer, then you will should set it in group_vars or host_vars:

---
# group variable for webservers
username: abc

---
# host variables for 10.0.1.2
username: xyz

or if you need to loop over some application you are deploying, you may do it on a playbook level:

---
- hosts: webservers
  vars:
    app1:
      username: abc
  roles:
    - { configure_application, app: app1 }

- hosts: databases
  vars:
    app2:
      username: xyz
  roles:
    - { configure_application, app: app2 }

Upvotes: 2

Related Questions