Which is the best way to make config changes in conf files in ansible
If it's an INI file, then your specific requirements can be probably met by ini_file
module (http://docs.ansible.com/ini_file_module.html).
If your change is limited just few lines (or eve one), then you can probably look at lineinfile
module (http://docs.ansible.com/lineinfile_module.html).
Should you however need to change large number of settings, then it may be more beneficial to use template
(http://docs.ansible.com/template_module.html) or assemble
(http://docs.ansible.com/assemble_module.html). The main disadvantage of this approach is that you may need to review your template or fragments when updating your dependency.
How to edit properly a configuration file of a service and make sure it is loaded in the service with Ansible Playbook?
After several months practicing Ansible, I've got the answer of my interrogation.
It's an Ansible designing purpose habit to take. When I design a new role that aim to set-up a new service, I have two things to do and not forget :
- Flush the handlers though the yaml command
- meta: flush_handlers
- After flush, testing the service is up, for example on a mysql service
- name: Start and enable the service
service:
name: mysql
state: started
enabled: yes
- name: Ensure mysql port is started
wait_for:
port: 3306
delay: 3
timeout: 300
If my configuration is wrong, my handler will be executed before the service execution test (thanks to the flush_handlers), so the service test will fail, and I can fix my mistake.
Rendering a specific Config file Template within Ansible Roles
i suppose you have a link between list of hostnames and file.j2 (same number for example)
- find:
path: "path of templates" # give the right folder of templates
file_type: file
patterns: '[0-9]+-.*?\.j2' #search files format xxxx-yyyy.j2
use_regex: yes
register: result
- set_fact:
hosting: "{{ hosting | d([]) + _dico }}"
loop: "{{ result.files | map(attribute='path') | list }}"
vars:
_src: "{{ item }}"
_num: "{{ (_src | basename).split('-') | first }}"
_grp: "{{ 'routers' ~ '_' ~ _num }}"
_hosts: "{{ lookup('vars', _grp) }}"
_dico: >-
{%- set ns = namespace() -%}
{%- set ns.l = [] -%}
{%- for h in _hosts -%}
{%- if h.update({'pathj2': _src}) -%}{%- endif -%}
{%- set ns.l = ns.l + [h] -%}
{%- endfor -%}
{{ ns.l }}
- name: Generate configuration files
template:
src: "{{item.pathj2}}"
dest: ~/ansible/configs/{{item.hostname}}.txt
loop: "{{ hosting }}"
the first task selects files j2 from folder templates (following the regex pattern)
the second task add the corresponding path of file j2 to the file containing the hostnames
Section aware edits to config files without complicated regex
There is ini_file module.
Example from documentation:
# Ensure "fav=lemonade is in section "[drinks]" in specified file
- ini_file: dest=/etc/conf section=drinks option=fav value=lemonade mode=0600 backup=yes
Ansible modifying files after RPM installation
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
How do you manage per env config file using ansible?
Yes you can. With jinja2 and group_vars.
So what you do in your templates/ folder create a file like such:
templates/http.conf.j2
Say you have something like this in there:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName {{ subdomain }}.{{ domain }}
ServerAlias www.{{ subdomain }}.{{ domain }}
</VirtualHost>
Your layout should look like this:
├── group_vars
│ ├── all
│ │ └── config
│ ├── dev
│ │ └── config
│ └── test
│ └── config
├── inventory
│ ├── dev
│ │ └── hosts
│ └── test
│ └── hosts
├── site.yml
└── templates
└── http.conf.j2
In group_vars/all
you would have domain: "example.com"
In group_vars/dev
you would have subdomain: dev
In group_vars/test
you would have subdomain: test
In your task, you'd have your ansible template command i.e.
- hosts: all
tasks:
- name: Copy http conf
template:
dest: /etc/apache2/http.conf
src: templates/http.conf.j2
owner: root
group: root
And run your playbook like this:
ansible-playbook -i inventory/test site.yml
The file should end up on the host looking like this:
<VirtualHost *:80>
ServerName test.example.com
ServerAlias www.test.example.com
</VirtualHost>
Related Topics
Jna Link Issue While Starting Cassandra Rhel 6.5
Configure Options for Building Mingw-64 on Linux-64 for Linux-64 (Ultimately Targetting Windows-64)
How to Grep While Avoiding 'Too Many Arguments'
Loading Executable or Executing a Library
The New Line Characted in the String Constant Isn't Being Recognized by Nasm
Bash (Or Other Shell): Wrap All Commands with Function/Script
Mod_Rewrite with Relative Path Redirects
Why Is Execution Time of a Process Shorter When Another Process Shares the Same Ht Core
How to Build an If Condition in Shell to Check Whether Curl Succeeded
Cross Compiling Linux Arm Kernel with New Driver Module
How to Find the Particular Text Stored in the File "Data.Txt" and It Occurs Only Once
Sudoers Nopasswd: Sudo: No Tty Present and No Askpass Program Specified
How Can Linux Ptrace Be Unsafe or Contain a Race Condition
Parsing Data from Ifconfig with Awk or Sed
What Is Echo $? in Linux Terminal
Triggering Autokey Script via Mouse Button - How To