diff --git a/ansible/main.yaml b/ansible/main.yaml index 3d7532363a609935198057194e15a3908fcbe860..63b0553cfc26f97c270b7bf08a249e9d7e83681f 100644 --- a/ansible/main.yaml +++ b/ansible/main.yaml @@ -1,11 +1,4 @@ --- - hosts: all - tasks: - - name: update etc/hosts - lineinfile: - dest: /etc/hosts - line: "{{ hostvars[item].ansible_host }} {{ hostvars[item].inventory_hostname }} {{ hostvars[item].inventory_hostname_short }}" - state: present - with_items: "{{ groups.all }}" - become: yes - + roles: + - common diff --git a/ansible/roles/common/tasks/main.yaml b/ansible/roles/common/tasks/main.yaml new file mode 100644 index 0000000000000000000000000000000000000000..601e6347565fb1dd48543dddd27106713d9b11ce --- /dev/null +++ b/ansible/roles/common/tasks/main.yaml @@ -0,0 +1,7 @@ + - name: update etc/hosts + lineinfile: + dest: /etc/hosts + line: "{{ hostvars[item].ansible_host }} {{ hostvars[item].inventory_hostname }} {{ hostvars[item].inventory_hostname_short }}" + state: present + with_items: "{{ groups.all }}" + become: yes diff --git a/chi-appliance.py b/chi-appliance.py index 97839548bac3f38ec854e8eee639969b186b8cbd..26183e81e9a3065beb72ec6312aac6af09f49058 100755 --- a/chi-appliance.py +++ b/chi-appliance.py @@ -51,7 +51,7 @@ def do_create(argv): template = os.path.abspath(argv.template) try: ret = shade_client.create_stack(argv.name, template_file=template, - wait=True, **extra_args) + wait=argv.wait, **extra_args) print(json.dumps(ret, indent=4)) except shade.exc.OpenStackCloudHTTPError as e: print(e) @@ -60,7 +60,7 @@ def do_create(argv): def do_delete(argv): """Delete an appliance with .""" shade_client = get_shade_client(argv.region) - ret = shade_client.delete_stack(argv.name) + ret = shade_client.delete_stack(argv.name, wait=argv.wait) if ret: print("Appliance successfully deleted.") else: @@ -144,9 +144,16 @@ def do_configure(argv): # local playbook mypath = os.path.abspath(os.path.dirname(__file__)) confpath = os.path.join(mypath, 'ansible') + # remove potential ./ansible/ from playbook path + playpath = os.path.split(argv.playbook)[1] playbook = ('---\n' '- hosts: all\n' + ' gather_facts: no\n' ' tasks:\n' + ' - name: Wait for frontend\n' + ' wait_for_connection:\n' + ' - name: Gather info about frontend\n' + ' setup:\n' ' - name: Ensure dependencies are installed\n' ' package:\n' ' name: "{{ item }}"\n' @@ -157,7 +164,7 @@ def do_configure(argv): ' become: yes\n' ' - name: Copy ansible configuration to the frontend\n' ' synchronize:\n' - ' src: '+confpath+'\n' + ' src: ' + confpath + '\n' ' dest: ~/\n' ' - name: Generate ssh-key for appliance\n' ' user:\n' @@ -165,14 +172,14 @@ def do_configure(argv): ' state: present\n' ' generate_ssh_key: yes\n' ' - name: Execute ansible on frontend\n' - ' command: ansible-playbook -i inventory main.yaml\n' + ' shell: ANSIBLE_HOST_KEY_CHECKING=False' + ' ansible-playbook -i inventory.yaml ' + playpath + '\n' ' args:\n' ' chdir: ~/ansible\n' ' register: config\n' ' - debug: var=config.stdout_lines') # generate files - # BAD ERROR HANDLING HERE - remote_inv_path = "./ansible/inventory.yaml" + remote_inv_path = os.path.join(confpath, "inventory.yaml") local_temp = NamedTemporaryFile(mode='w+', encoding='utf8', delete=False) play_temp = NamedTemporaryFile(mode='w+', encoding='utf8', delete=False) with open(remote_inv_path, "w+", encoding='utf8') as remote_inv: @@ -190,7 +197,7 @@ def do_configure(argv): universal_newlines=True) while True: err = proc.poll() - if not err: + if err is None: print(proc.stdout.readline(), end='', flush=True) else: if err == 0: @@ -216,6 +223,8 @@ def main(): # create a lease parser_create = subparsers.add_parser("create", help="Create an appliance") + parser_create.add_argument("--wait", action='store_true', + help="Wait for the operation to complete") parser_create.add_argument("name", help="Name of the appliance") parser_create.add_argument("lease", help="Lease for the appliance") parser_create.add_argument("template", help="Appliance template") @@ -225,6 +234,8 @@ def main(): parser_create.set_defaults(func=do_create) parser_delete = subparsers.add_parser("delete", help="Delete an appliance") + parser_delete.add_argument("--wait", action='store_true', + help="Wait for the operation to complete") parser_delete.add_argument("name", help="Name of the appliance") parser_delete.set_defaults(func=do_delete) @@ -238,6 +249,8 @@ def main(): parser_config = subparsers.add_parser("configure", help="Configure an appliance") parser_config.add_argument("name", help="Name of the appliance") + parser_config.add_argument("playbook", default="main.yaml", nargs='?', + help="Playbook for remote configuration") parser_config.set_defaults(func=do_configure) args = parser.parse_args()