Skip to content
This repository

add add_host action plugin - add hosts to inventory during a playbook #1491

Closed
wants to merge 1 commit into from

2 participants

Seth Vidal Michael DeHaan
Seth Vidal
  • lets act on those hosts in the next play
Michael DeHaan
Owner

I am thinking we need a way to persist inventory to make this really workable.

Thus, it seems, we really have to make the inventory objects serializable.

This opens a whole other can of worms as we don't want to rewrite that file in place, and some of the inventory variables are also made available with group_vars/host_vars.

I am tentatively thinking that this doesn't become a core plugin for that reason. I WANT it to be, but we have to solve those problems first.

If you are using EC2/other as a remote inventory source, maybe all you need is a way to invalidate the cache from the inventory source?

Michael DeHaan mpdehaan closed this October 30, 2012
Seth Vidal

So here's the thing - I don't want to permanently add to the inventory. I want to just add to the inventory for purposes of the in-memory playbook, nothing more. Is this still not acceptable?

maybe if I rename "add_temp_host" :)

Michael DeHaan
Owner
Michael DeHaan
Owner
Seth Vidal
Michael DeHaan
Owner

Yeah I think I follow your point of usage.

I think I want to enforce that the hosts /do/ get added to inventory.

The way I would probably have done is it I would have a script that installs the new guest, fairly bare bones, not driven by an ansible playbook at all...

and then at the end of that it finishes with calling

ansible-playbook -i "192.168.10.50," playbook.yml

The idea of using ansible to drive the very top layer of provisioning is fine if people want to do it, but to me, perhaps based on whether I come from with Cobbler land and so forth, feels weird to me.

I see it as "provision -- then run ansible"

So, BYOP(provisioning) basically.

I also have this rather baseless though that I want to keep the module list in core /reasonably/ short, so if a module is to drop in core I want it to be something a really large amount of people are going to want to use, as there's a cognitive need to at least skim over all of them, and gut feel is this one is a little weird.

That all being said, I understand your use case. It just seems like it's an ansible-provisioning thing to me, and I am (off list) talking to dag a bit about possibly not making that part of the core project anyway. I just think it can be made a lot smoother and I'm not comfortable with ansible driving the top end of provisioning.

That being said, it clearly CAN do that, and I'm not going to stop that from happening either -- I just don't think I feel comfortable with that being the interface for how you describe how you create hosts.

Seth Vidal

okay - I am clearly confused.

How are you distinguishing creating a new instance and provisioning? What is it that you want to keep ansible separate from vs what cobbler does?

At this point I see ansible as primarily an orchestration tool. A way to perform a series of actions across multiple systems coordinating between them.

Remember rocketship recovery? That's what I'm thinking about.

I have 150 systems, I want to duplicate critical ones into the cloud in a single command. Some of them have persistent ips, some are just consumers and can have variable ips. But I do not want to set them up one by one.

I want to be able to write playbooks that describe setting up complete clusters of systems.

Which involves creating them, configuring them and dealing with their interconnectedness.

What part of that is something ansible should NOT do?

Michael DeHaan
Owner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Oct 30, 2012
Seth Vidal add add_host action plugin - add hosts to inventory during a playbook
run - lets act on those hosts in the next play
4ae9d72
This page is out of date. Refresh to see the latest.
70  lib/ansible/runner/action_plugins/add_host.py
... ...
@@ -0,0 +1,70 @@
  1
+# Copyright 2012, Seth Vidal <skvidal@fedoraproject.org>
  2
+#
  3
+# This file is part of Ansible
  4
+#
  5
+# Ansible is free software: you can redistribute it and/or modify
  6
+# it under the terms of the GNU General Public License as published by
  7
+# the Free Software Foundation, either version 3 of the License, or
  8
+# (at your option) any later version.
  9
+#
  10
+# Ansible is distributed in the hope that it will be useful,
  11
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
  12
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13
+# GNU General Public License for more details.
  14
+#
  15
+# You should have received a copy of the GNU General Public License
  16
+# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
  17
+
  18
+import ansible
  19
+
  20
+from ansible.callbacks import vv
  21
+from ansible.errors import AnsibleError as ae
  22
+from ansible.runner.return_data import ReturnData
  23
+from ansible.utils import parse_kv, template
  24
+from ansible.inventory.host import Host
  25
+from ansible.inventory.group import Group
  26
+
  27
+class ActionModule(object):
  28
+    ''' Create inventory hosts and groups in the memory inventory'''
  29
+
  30
+    ### We need to be able to modify the inventory
  31
+    BYPASS_HOST_LOOP = True
  32
+
  33
+    def __init__(self, runner):
  34
+        self.runner = runner
  35
+
  36
+    def run(self, conn, tmp, module_name, module_args, inject):
  37
+        args = parse_kv(self.runner.module_args)
  38
+        if not 'hostname' in args:
  39
+            raise ae("'hostname' is a required argument.")
  40
+
  41
+        vv("created 'add_host' ActionModule: hostname=%s"%(args['hostname']))
  42
+
  43
+
  44
+        result = {'changed': True}
  45
+
  46
+        new_host = Host(args['hostname'])
  47
+        inventory = self.runner.inventory
  48
+        
  49
+        # add the new host to the 'all' group
  50
+        allgroup = inventory.get_group('all')
  51
+        allgroup.add_host(new_host)
  52
+        result['changed'] = True
  53
+        
  54
+        # add it to the group if that was specified
  55
+        if 'groupname' in args:
  56
+            if not inventory.get_group(args['groupname']):
  57
+                new_group = Group(args['groupname'])
  58
+                inventory.add_group(new_group)
  59
+                
  60
+            ngobj = inventory.get_group(args['groupname'])
  61
+            ngobj.add_host(new_host)
  62
+            vv("created 'add_host' ActionModule: groupname=%s"%(args['groupname']))
  63
+            result['new_group'] = args['groupname']
  64
+            
  65
+        result['new_host'] = args['hostname']
  66
+        
  67
+        return ReturnData(conn=conn, comm_ok=True, result=result)
  68
+
  69
+
  70
+
23  library/add_host
... ...
@@ -0,0 +1,23 @@
  1
+# -*- mode: python -*-
  2
+
  3
+DOCUMENTATION = '''
  4
+---
  5
+module: add_host
  6
+short_description: add a host (and alternatively a group) to the ansible-playbook in-memory inventory
  7
+description:
  8
+  - Use variables to create new hosts and groups in inventory for use in later plays of the same playbook
  9
+version_added: 0.9
  10
+options:
  11
+  hostname:
  12
+    description:
  13
+    - The hostname/ip of the host to add to the inventory
  14
+    required: true
  15
+  groupname:
  16
+    description:
  17
+    - The groupname to add the hostname to.
  18
+    required: false
  19
+author: Seth Vidal
  20
+examples:
  21
+  - description: add host to group 'just_created'
  22
+    code: add_host hostname=${ip_from_ec2create} groupname=just_created
  23
+'''
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.