title: Specify SSH configuration file at run time in Ansible 1.9
body: <p>I'm managing two server environments that are configured differently.  I access the two environments by specifying different SSH configurations on the command line because I need to specify a different <code>User</code>, <code>ProxyCommand</code>, and a list of other options for SSH.</p>
  
  <p>e.g. </p>
  
  <pre><code>ssh oldserver.example.org -F config_legacy
  
  ssh newserver.example.org -F config
  </code></pre>
  
  <p>To configure and maintain state on my servers, I've been using Ansible (version 1.9.0.1), which reads an SSH configuration file that is specified by a line in its <code>ansible.cfg</code>:</p>
  
  <pre><code>...
  ssh_args =  -F some_configuration_file
  ...
  </code></pre>
  
  <p>The <code>ansible.cfg</code> is <a href="https://github.com/ansible/ansible/blob/b89071e4858e5bf37846b347fab43d95b4785aef/lib/ansible/constants.py#L67-L82:" rel="nofollow">loaded a number of ways</a>:  </p>
  
  <pre><code>def load_config_file():
      ''' Load Config File order(first found is used): ENV, CWD, HOME, /etc/ansible '''
  
      p = configparser.ConfigParser()
  
      path0 = os.getenv("ANSIBLE_CONFIG", None)
      if path0 is not None:
          path0 = os.path.expanduser(path0)
      path1 = os.getcwd() + "/ansible.cfg"
      path2 = os.path.expanduser("~/.ansible.cfg")
      path3 = "/etc/ansible/ansible.cfg"
  
      for path in [path0, path1, path2, path3]:
          if path is not None and os.path.exists(path):
              try:
                  p.read(path)
              except configparser.Error as e:
                  print("Error reading config file: \n{0}".format(e))
                  sys.exit(1)
              return p
      return None
  </code></pre>
  
  <p>I could use this behavior to set an environmental variable before each command to load an entirely different ansible.cfg, but that seems messy as I only need to fiddle the ssh_args.  Unfortunately, Ansible doesn't expose the command switch to specify an SSH config.</p>
  
  <p>I'd like to not maintain any modifications to Ansible I'd like to not wrap all calls to the <code>ansible</code> or <code>ansible-playbook</code> commands.  To preserve the behavior of Ansible's commands, I believe my options are:</p>
  
  <ul>
  <li>a) have the target of <code>ssh_args =  -F &lt;&lt;config_file&gt;&gt;</code> be a script that's opened</li>
  <li>b) have the target of <code>p.read(path)</code> be a script that gets expanded to generate a valid ansible.cfg</li>
  <li>c) just maintain different ansible.cfg files and take advantage of the fact that Ansible picks this file in the order of environmental variable, cwd.</li>
  </ul>