how to get the server Site & location name get automatically when playbook will run

15 views
Skip to first unread message

Karn Kumar

unread,
Oct 30, 2017, 5:32:08 AM10/30/17
to Ansible Project
I'm instaling collectd service using ansible version 2.4 and making some changes into the colectd config and that's working fine.
In my playbook i'm using `lineinfile` module to make the chnages in the collectd confg file using the regex , here i have two `lineinfile` chnages one is to chnage 
the default Hostname from the config file using `ansible_hostname` variable while in another line i have some Prefix's which i'm changing and that the Key point for my playbook..

For clarification belwo the line where i need the playbook to be opting the values dynamically 
Example:
a) this is what is value looks like
Prefix  "collectd.<ServiceName>.<Changable_StateName>.<Changable_location>"
b) this is what i'm appending values manually in my play as of now ..
Prefix "collectd.OpenDJ.EMEA.sophia.


My Invetory looks like as follows( this is based on host:var and using private keys over)
----------------------------------------------------------------------------------------
[hosts]
dbserver-bijing
dbserver-endicott
dbserver-delhi
[hosts:vars]
ansible_ssh_user=root
ansible_ssh_private_key_file=/root/ansiblseckeys

Changable_StateName :
--------------------
EMEA
APAC
NA
IN

Changable_location
------------------
sophia
bijing
endicott
delhi



Below is my Playboook peice which makes the changes ...
-----------------------------------------------------
 - name: Committing changes to collectd configuration....
    lineinfile:
      dest: "{{ item.dest }}"
      regexp: "{{ item.regexp }}"
      line: "{{ item.line }}"
      backrefs: yes
    with_items:
      - { dest: '/opt/collectd/etc/collectd.conf', regexp: '#Hostname    "dbserver-repo"', line: 'Hostname  "{{ ansible_hostname }}"' }
      - { dest: '/opt/collectd/etc/collectd.conf', regexp: '#    Prefix "collectd.unix."', line: '    Prefix "collectd.OpenDJ.EMEA.sophia."' }

So, the problem is , when i run my playbook each time hostname gets chnages and same as the `stateName` and `statelocation`.
Hence looing around to spin these type of chnage in the playbook.
Reply all
Reply to author
Forward
0 new messages