user5261351
user5261351

Reputation: 21

Puppet to Ansible, how to example

I'm in the process of setting up Ansible to take over for a puppet install.

I see puppet manifest that is setting up a ypbind file, and it pushes out a slightly different version of the yp.conf file based on hostname.

So I have the following puppet definitions:

class ypbind {
  file {'yp.conf':
   ensure   => file,
   content   => template("ypbind/yp.conf.erb"),
   owner     => 'root',
   group     => 'root',
   mode      => '0644',
   notify      => Service['ypbind'],
   path        => '/etc/yp.conf',
 }
 service {'ypbind':
     enable => true,
     ensure => running,
 }
}

Followed by this template (domains and IPs sanitized)

<% case
when @hostname =~/(^[p|d]NY-)/ -%>
#New York Data Center NIS
domain NY1.domain.com IP1
domain NY2.domain.com IP2    
domain SF1.domain.com IP3
<%when @hostname =~/(^[p|d]SF-)/ -%>
#San Fran Data Center NIS
domain SF1.domain.com IP1
domain SF2.domain.com IP2
domain NY1.domain.com IP3
<% else -%>
#Default to NY DC
domain NY1.domain.com IP1
domain NY2.domain.com IP2
<% end -%>

My question is how do I replicate this logic using Ansible? I think I figured out a way to do it with hostname, but I'd have to use multiple files. Is there a way to do the same thing in ansible as this puppet example?

---
- hosts: all
  sudo: yes
  gather_facts: yes
  user: ansible

  tasks:
  - name: update NY ypbind
    template:  src=/etc/ansible/files/yp.conf.NY dest=/etc/yp.conf mode=0644 owner=root group=root
    notify: restart ypbind
    when: ansible_hostname | match('^[p|d]NY-test01')

  - name: update SF ypbind
    template:  src=/etc/ansible/files/yp.conf.SF dest=/etc/yp.conf mode=0644 owner=root group=root
    notify: restart ypbind
    when: ansible_hostname | match('^[p|d]SF-test01')

  handlers:
    - name: restart ypbind
    - service: name=ypbind state=restarted

I think after research, I'd just use the jinja2 template system, just not sure how quite yet...

Upvotes: 1

Views: 966

Answers (1)

user5261351
user5261351

Reputation: 21

For anyone else that might be interested, it turned out to be pretty simple. It was just the following.

ypbind.yaml

---
- hosts: all
sudo: yes
gather_facts: yes
user: ansible

tasks:
- name: update NY ypbind
  template:  src=/etc/ansible/files/yp.conf.j2 dest=/etc/yp.conf mode=0644 owner=root group=root
  notify: restart ypbind

handlers:
- name: restart ypbind
  service: name=ypbind state=restarted

yp.conf.j2

{% if ansible_hostname |match ('^[p|d]sf-') %}
domain SF.domain.com server 10.200.0.1
domain SF.domain.com server 10.200.0.2
domain NY.domain.com server 10.201.0.1
{% else %}
domain NY.domain.com server 10.201.0.1
domain NY.domain.com server 10.201.0.2
domain SF.domain.com server 10.200.0.1
{% endif %}

Upvotes: 1

Related Questions