From b365e0461620fed94e916c8c2d57ccde7c67a939 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Sun, 17 Feb 2013 11:22:50 -0500 Subject: [PATCH] Allow add_host to add hosts to multiple groups, groups is now an alias for groupname. --- CHANGELOG.md | 2 ++ lib/ansible/runner/action_plugins/add_host.py | 23 ++++++++++--------- library/add_host | 15 ++++++------ 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd6adaf0f5b..c3b5a32d225 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,8 @@ Ansible Changes By Release * fixed ~ expansion for fileglob * can set ansible_ssh_user and ansible_ssh_pass in inventory variables * lookup plugin macros like $FILE and $ENV now work without returning arrays in variable definitions/playbooks +* add_host module can set ports and other inventory variables +* add_host module can add modules to multiple groups (groups=a,b,c), groups now alias for groupname 1.0 "Eruption" -- Feb 1 2013 diff --git a/lib/ansible/runner/action_plugins/add_host.py b/lib/ansible/runner/action_plugins/add_host.py index 9727b8d0f16..60f953483e2 100644 --- a/lib/ansible/runner/action_plugins/add_host.py +++ b/lib/ansible/runner/action_plugins/add_host.py @@ -60,7 +60,7 @@ class ActionModule(object): # Add any variables to the new_host for k in args.keys(): - if k != 'hostname' and k != 'groupname': + if not k in [ 'hostname', 'groupname', 'groups' ]: new_host.set_variable(k, args[k]) @@ -68,17 +68,18 @@ class ActionModule(object): allgroup = inventory.get_group('all') allgroup.add_host(new_host) result['changed'] = True - + + groupnames = args.get('groupname', args.get('groups', '')) # add it to the group if that was specified - if 'groupname' in args: - if not inventory.get_group(args['groupname']): - new_group = Group(args['groupname']) - inventory.add_group(new_group) - - ngobj = inventory.get_group(args['groupname']) - ngobj.add_host(new_host) - vv("created 'add_host' ActionModule: groupname=%s"%(args['groupname'])) - result['new_group'] = args['groupname'] + if groupnames != '': + for group_name in groupnames.split(","): + if not inventory.get_group(group_name): + new_group = Group(group_name) + inventory.add_group(new_group) + grp = inventory.get_group(group_name) + grp.add_host(new_host) + vv("added host to group via add_host module: %s" % group_name) + result['new_groups'] = groupnames.split(",") result['new_host'] = args['hostname'] diff --git a/library/add_host b/library/add_host index 20d38252959..b7df0d8d0f3 100644 --- a/library/add_host +++ b/library/add_host @@ -13,14 +13,15 @@ options: description: - The hostname/ip of the host to add to the inventory required: true - groupname: + groups: + aliases: [ 'groupname' ] description: - - The groupname to add the hostname to. + - The groups to add the hostname to, comma seperated. required: false author: Seth Vidal examples: - - description: add host to group 'just_created' - code: add_host hostname=${ip_from_ec2create} groupname=just_created - - description add a host with a non-standard port local to your machines - code: add_host hostname='${new_ip}:${new_port}' ansible_ssh_port='${new_port}' ansible_ssh_host='${new_ip}' groupname=cloud_hosts -''' \ No newline at end of file + - description: add host to group 'just_created' with variable foo=42 + code: add_host hostname=${ip_from_ec2create} groups=just_created foo=42 + - description: add a host with a non-standard port local to your machines + code: add_host hostname='${new_ip}:${new_port}' +'''