Is there a GNU/Linux command to write ini files?

I'm presently writing some documentation (specifically on how to set up Ansible with a local Vagrant machine) and I like to list commentary in the form of commands to issue. For example:

  1. Set up the SSH agent:

    ssh-agent bash
    ssh-add .vagrant/machines/default/virtualbox/private_key
    

However, sometimes I find I want to document a change in a config file, which is not so easy to describe. I presently do this:

  1. Configure Ansible to use port 2222 for Vagrant

    Modify /etc/ansible/ansible.cfg to use remote_port = 2222
    

I'd rather do something like this (a theoretical command) as it could be issued quickly and in a more automated fashion than reaching for a text editor:

  1. Configure Ansible to use port 2222 for Vagrant

    ansible-config remote_port 2222
    

This appears to be an ini file, so if the ansible binary doesn't support this (and I can't see that it does), is there a general Linux/Unix way to do this? Failing the above, how about:

iniwrite /etc/ansible/ansible.cfg defaults.remote_port 2222

Of course I could write a script of some kind, but it'd be better not to reinvent the wheel!

I am using Ubuntu 14.04 LTS.

Answers


I don't know about such a tool and it is definitely not part of the default toolchain you can expect to be installed on your user's systems.

You can use sed:

sed -i 's/^remote_port=.*/remote_port=2222/' ansible.cfg

All GNU/Linux users will understand that especially since you describe what it is doing.


That's precisely one of the problems Ansible is supposed to solve - so you can use it to write its own configuration file. Just create a bootstrap playbook that does all the necessary things on localhost (aside from installing Ansible, of course).

A predecessor to these tools is Augeas. It comes with a default IniFile "lens" that you could use to write various ini entries easily.


The ansible-config script could be as simple as:

#!/bin/sh
CFG=/etc/ansible/ansible.cfg
awk '{BEGIN {found=0} END {if(!found){print keyword" = "value}} if($0~"^ *"keyword" *="){print keyword" = "value; found=1}else{print $0}}' \
    keyword="$1" value="$2" $CFG > $CFG.new \
    && mv $CFG.new $CFG \
    || rm -f $CFG.new

Some error checking would be required, etc..., but it gives you the idea.


Whilst the main point of my question was "how to automate the writing of ini files in general", I have found out how to avoid it for Ansible. The SSH port can be added into the /etc/ansible/hosts file, thus:

127.0.0.1:2222

This overrides the remote_port in the /etc/ansible/ansible.cfg configuration ini file.


Need Your Help

Jenkins: Dynamic List Subversion Tags

svn dynamic jenkins parameters tags

I have a Jenkins task for deploy SVN tags.

SQL Server column comparison including Null/Not Null

sql sql-server

Doing a comparison of column pairs, trying to find any that don't match