Commit f8865ee4 authored by Swann Perarnau's avatar Swann Perarnau
Browse files

Merge branch 'ansible-improvements' into 'master'

Ansible improvements

See merge request !2
parents 440db486 e866ec78
--- ---
- hosts: all - hosts: all
tasks: roles:
- name: update etc/hosts - common
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
- 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
...@@ -51,7 +51,7 @@ def do_create(argv): ...@@ -51,7 +51,7 @@ def do_create(argv):
template = os.path.abspath(argv.template) template = os.path.abspath(argv.template)
try: try:
ret = shade_client.create_stack(argv.name, template_file=template, 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)) print(json.dumps(ret, indent=4))
except shade.exc.OpenStackCloudHTTPError as e: except shade.exc.OpenStackCloudHTTPError as e:
print(e) print(e)
...@@ -60,7 +60,7 @@ def do_create(argv): ...@@ -60,7 +60,7 @@ def do_create(argv):
def do_delete(argv): def do_delete(argv):
"""Delete an appliance with <name>.""" """Delete an appliance with <name>."""
shade_client = get_shade_client(argv.region) 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: if ret:
print("Appliance successfully deleted.") print("Appliance successfully deleted.")
else: else:
...@@ -144,9 +144,16 @@ def do_configure(argv): ...@@ -144,9 +144,16 @@ def do_configure(argv):
# local playbook # local playbook
mypath = os.path.abspath(os.path.dirname(__file__)) mypath = os.path.abspath(os.path.dirname(__file__))
confpath = os.path.join(mypath, 'ansible') confpath = os.path.join(mypath, 'ansible')
# remove potential ./ansible/ from playbook path
playpath = os.path.split(argv.playbook)[1]
playbook = ('---\n' playbook = ('---\n'
'- hosts: all\n' '- hosts: all\n'
' gather_facts: no\n'
' tasks:\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' ' - name: Ensure dependencies are installed\n'
' package:\n' ' package:\n'
' name: "{{ item }}"\n' ' name: "{{ item }}"\n'
...@@ -157,7 +164,7 @@ def do_configure(argv): ...@@ -157,7 +164,7 @@ def do_configure(argv):
' become: yes\n' ' become: yes\n'
' - name: Copy ansible configuration to the frontend\n' ' - name: Copy ansible configuration to the frontend\n'
' synchronize:\n' ' synchronize:\n'
' src: '+confpath+'\n' ' src: ' + confpath + '\n'
' dest: ~/\n' ' dest: ~/\n'
' - name: Generate ssh-key for appliance\n' ' - name: Generate ssh-key for appliance\n'
' user:\n' ' user:\n'
...@@ -165,14 +172,14 @@ def do_configure(argv): ...@@ -165,14 +172,14 @@ def do_configure(argv):
' state: present\n' ' state: present\n'
' generate_ssh_key: yes\n' ' generate_ssh_key: yes\n'
' - name: Execute ansible on frontend\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' ' args:\n'
' chdir: ~/ansible\n' ' chdir: ~/ansible\n'
' register: config\n' ' register: config\n'
' - debug: var=config.stdout_lines') ' - debug: var=config.stdout_lines')
# generate files # generate files
# BAD ERROR HANDLING HERE remote_inv_path = os.path.join(confpath, "inventory.yaml")
remote_inv_path = "./ansible/inventory.yaml"
local_temp = NamedTemporaryFile(mode='w+', encoding='utf8', delete=False) local_temp = NamedTemporaryFile(mode='w+', encoding='utf8', delete=False)
play_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: with open(remote_inv_path, "w+", encoding='utf8') as remote_inv:
...@@ -190,7 +197,7 @@ def do_configure(argv): ...@@ -190,7 +197,7 @@ def do_configure(argv):
universal_newlines=True) universal_newlines=True)
while True: while True:
err = proc.poll() err = proc.poll()
if not err: if err is None:
print(proc.stdout.readline(), end='', flush=True) print(proc.stdout.readline(), end='', flush=True)
else: else:
if err == 0: if err == 0:
...@@ -216,6 +223,8 @@ def main(): ...@@ -216,6 +223,8 @@ def main():
# create a lease # create a lease
parser_create = subparsers.add_parser("create", help="Create an appliance") 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("name", help="Name of the appliance")
parser_create.add_argument("lease", help="Lease for the appliance") parser_create.add_argument("lease", help="Lease for the appliance")
parser_create.add_argument("template", help="Appliance template") parser_create.add_argument("template", help="Appliance template")
...@@ -225,6 +234,8 @@ def main(): ...@@ -225,6 +234,8 @@ def main():
parser_create.set_defaults(func=do_create) parser_create.set_defaults(func=do_create)
parser_delete = subparsers.add_parser("delete", help="Delete an appliance") 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.add_argument("name", help="Name of the appliance")
parser_delete.set_defaults(func=do_delete) parser_delete.set_defaults(func=do_delete)
...@@ -238,6 +249,8 @@ def main(): ...@@ -238,6 +249,8 @@ def main():
parser_config = subparsers.add_parser("configure", parser_config = subparsers.add_parser("configure",
help="Configure an appliance") help="Configure an appliance")
parser_config.add_argument("name", help="Name of the 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) parser_config.set_defaults(func=do_configure)
args = parser.parse_args() args = parser.parse_args()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment