Search code examples
configurationrpmansibleansible-2.x

Ansible modifying files after RPM installation


As part of my Ansible playbook I down and install an RPM from an internal repository. This RPM comes packaged with a bunch of configuration. For most of my machines I don't need to change this configuration. However for a small subset, defined by an ansible group, I need to modify a few of the configuration fields.

I'm assuming I would need to do something so:

  1. Wait till my RPM is installed and "running"
  2. Stop the service
  3. Modify the configuration file somehow? Use a when clause to limit it to the group I want to modify.
  4. Restart the service

Or possibly there is a better way to achieve to do this. Can anyone out there suggest how I could achieve my general goal?


Solution

  • Your four step procedure looks good to me. Perhaps the service doesn't have to be stopped before modifying the configuration.

    I'd create an Ansible role where the necessary tasks are defined. The base structure for the role is created by ansible-galaxy init command.

    ansible-galaxy init my_role
    

    The configuration file can be modified (or rather generated) using Ansible's template module:

    - name: Modify the configuration file
      template: src=myconf.cnf.j2 dest=/etc/myconf.cnf
      when: "'my_group_name' in group_names"
      notify: Restart the service
    

    It will be run only for the hosts that belong to my_group_name group. Template myconf.cnf.j2 has to be found from my_role/templates directory. The service will be restarted only when Restart the service handler was notified in the task. The handler needs to be put to my_role/handlers/main.yml file:

    - name: Restart the service
      service: name=service_name state=restarted