How do I assign different variable values for different group system

12 views
Skip to first unread message

Xinhuan Zheng

unread,
Dec 12, 2019, 4:15:29 PM12/12/19
to Ansible Project
Hello,

I'm trying to create an Ansible role, networking, to automate /etc/resolv.conf file. I created this role like below:

production
networking.yml
group_vars/
  agroup
  bgroup
roles/
  networking/
    tasks/main.yml
    templates/resolv.conf.j2

In my resolv.conf.j2 file, I put variables in this file:


{% for item in nameserver %}
nameserver {{ item }}
{% endfor %}

The name servers will be different for different group systems.
In group_vars/agroup:

---

nameservers:
  - 192.168.10.251
  - 192.168.10.252

In group_vars/bgroup:

---

nameservers:
  - 192.168.101.251
  - 192.168.101.252

In playbook networking.yml:

---

- hosts: all
  roles:
    - networking

In production (inventory) file:

[agroup]
myserver

[bgroup]
myserver2

Here is command I want to use for play:

$ ansible-playbook -i production networking.yml -l myserver
$ ansible-playbook -i production  networking.yml -l myserver2

Does above play use agroup defined nameservers for myserver, and bgroup defined nameservers for myserver2?

Thank you,

- Xinhuan

Kai Stian Olstad

unread,
Dec 12, 2019, 7:05:57 PM12/12/19
to ansible...@googlegroups.com
On 12.12.2019 22:15, Xinhuan Zheng wrote:
> Does above play use agroup defined nameservers for myserver, and bgroup
> defined nameservers for myserver2?

Why don't you just run the code and check for yourself?
Anyway, the answer to your question is yes.


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages