{"id":3288,"date":"2015-05-01T16:40:45","date_gmt":"2015-05-01T22:40:45","guid":{"rendered":"http:\/\/benincosa.com\/?p=3288"},"modified":"2015-05-02T23:11:39","modified_gmt":"2015-05-03T05:11:39","slug":"copc-metacloud-with-ansible","status":"publish","type":"post","link":"https:\/\/benincosa.com\/?p=3288","title":{"rendered":"Deploying Instances on COPC (metacloud) with Ansible"},"content":{"rendered":"<p>I wanted to show a quick example of how to deploy an instance on Cisco OpenStack Private Cloud (COPC or Cisco OPC or MetaCloud) with Ansible. \u00a0Since COPC is just a fully engineered and operated distribution of OpenStack from Cisco, this blog is also applicable to normal OpenStack environments.<\/p>\n<p>I&#8217;m a big fan of Ansible because everything is agentless. \u00a0I also think the team has done a phenomenal job on the docs. \u00a0We&#8217;ll be using the <a href=\"http:\/\/docs.ansible.com\/nova_compute_module.html\">nova compute docs<\/a> here. \u00a0I don&#8217;t have to install anything on the instances to be able to do it and I can just run it from my laptop with minimal dependencies. \u00a0Here&#8217;s how I do it with CoreOS.<\/p>\n<h2>1. \u00a0Get Credentials<\/h2>\n<p>On COPC, you can navigate to your project and download the OpenStack RC File. \u00a0This is done from the ACCESS &amp; SECURITY tab and then clicking on the API Access tab on the right.<\/p>\n<p><a href=\"http:\/\/benincosa.com\/wp-content\/uploads\/2015\/05\/COPC-AccessSecurity.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-3289\" src=\"http:\/\/benincosa.com\/wp-content\/uploads\/2015\/05\/COPC-AccessSecurity.png\" alt=\"COPC-Access&amp;Security\" width=\"1651\" height=\"603\" \/><\/a>Once you download this file, you put it in your ~\/ directory. \u00a0I use a Mac so I just added the contents to my ~\/.bash_profile.sh file. It looks like this:<\/p>\n<pre class=\"lang:sh decode:true \">export OS_AUTH_URL=http:\/\/metacloud.url.at.your.site.com:5000\/v2.0\r\nexport OS_TENANT_ID=12345123412341234123412341234\r\nexport OS_TENANT_NAME=\"TCO Application\"\r\nexport OS_USERNAME=vallard\r\nexport OS_PASSWORD=supersecret<\/pre>\n<p>Now, we&#8217;re ready to role.<\/p>\n<h2>2. Ansible Setup<\/h2>\n<p>I covered Ansible in <a href=\"http:\/\/benincosa.com\/?p=2651\">previous<\/a> posts. \u00a0So I&#8217;m going to assume you already have it. \u00a0Let&#8217;s create a few directories and files. \u00a0I put all my stuff in the ~\/Code directory and then under the projects directory. \u00a0I then make sure everything in this directory belongs to some sort of git repo. \u00a0Some of those are on github (like this one) and others are in a private gitlab, or a private github repository.<\/p>\n<h3>.\/ansible.cfg<\/h3>\n<p>This file will have our info for where our inventory is.<\/p>\n<pre class=\"lang:sh decode:true \">[defaults]\r\nnocows = 1\r\nhostfile = .\/inventory\r\nhost_key_checking = false\r\nremote_user = core\r\n<\/pre>\n<p>This will be global settings for our environment. \u00a0We tell it not to use <a href=\"http:\/\/en.wikipedia.org\/wiki\/Cowsay\">cowsay<\/a>, but you can if you want. \u00a0Its kind of cute. \u00a0You may not have it installed. \u00a0We also tell it to use the contents of the inventory directory (which we&#8217;re going to create) to go to our hosts.<\/p>\n<p>host_key_checking tells it that when we access a new server, not to worry if we&#8217;ve never seen the host before and attach to it anyway. \u00a0Finally, our remote user is core as this is the default user for the coreos instance that I&#8217;m using.<\/p>\n<h3>.\/inventory\/hosts<\/h3>\n<p>We create a directory called inventory and add the file hosts. \u00a0We then add our one machine (our localhost!) \u00a0The contents looks like this:<\/p>\n<pre class=\"lang:sh decode:true \">[local]\r\nlocalhost ansible_python_interpreter=\/usr\/local\/bin\/python<\/pre>\n<p>You&#8217;ll notice here I also added which python I wanted to use, just in case I had other versions on the system. \u00a0This might be good too if you were using virtual environments.<\/p>\n<h3>.\/vars\/copc_vars.yml<\/h3>\n<p>This is where we put the specifics of what we want deployed. \u00a0In our case we need to define the following:<\/p>\n<pre class=\"lang:sh decode:true\">---\r\nsecurity_group: default\r\ncoreos_image_id: 2f82af25-e4cf-4c65-a52f-c13ad0ea475a \r\nfloating_ip_pool: nova\r\nkeypair: tco-gold\r\n<\/pre>\n<p>The security group &#8216;default&#8217; in my project, as seen from the dashboard actually includes port 22. \u00a0This is important so that I can ssh into it after its provisioned and do more things.<\/p>\n<p>I imported my coreos image from the <a href=\"https:\/\/coreos.com\/docs\/running-coreos\/platforms\/openstack\/\">CoreOS OpenStack image website<\/a>. \u00a0After importing it in from the dashboard, I clicked on the image to see the image ID:<\/p>\n<p><a href=\"http:\/\/benincosa.com\/wp-content\/uploads\/2015\/05\/Screen-Shot-2015-05-01-at-2.50.42-PM.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-3290\" src=\"http:\/\/benincosa.com\/wp-content\/uploads\/2015\/05\/Screen-Shot-2015-05-01-at-2.50.42-PM.png\" alt=\"Screen Shot 2015-05-01 at 2.50.42 PM\" width=\"548\" height=\"484\" \/><\/a>\u00a0 \u00a0The floating IP pool is nova, I got that from looking at the dashboard as well.<\/p>\n<p>Finally, the keypair is one I generated beforehand and downloaded into my server so I can log into it afterwards.<\/p>\n<h3>copc-one.yml<\/h3>\n<p>This file is our playbook. \u00a0It will provision a server. \u00a0Let&#8217;s look at the contents:<\/p>\n<pre class=\"lang:yaml decode:true\" title=\"copc-one.yml\">- name: launch one nova instance\r\n  connection: local\r\n  hosts: localhost\r\n  vars_files:\r\n   - vars\/copc_vars.yml\r\n\r\n  tasks: \r\n \r\n  - name: Ensure New server is up. \r\n    nova_compute:\r\n      state: present\r\n      auth_url: \"{{ lookup('env', 'OS_AUTH_URL') }}\"\r\n      login_username: \"{{ lookup('env', 'OS_USERNAME') }}\"\r\n      login_password: \"{{ lookup('env', 'OS_PASSWORD') }}\"\r\n      login_tenant_name: \"{{ lookup('env', 'OS_TENANT_NAME') }}\"\r\n      name: demo-server\r\n      image_id: \"{{ coreos_image_id }}\"\r\n      key_name: \"{{ keypair }}\"\r\n      flavor_id: 5 \r\n      security_groups: \"{{ security_group }}\"\r\n      floating_ip_pools:\r\n      - \"{{ floating_ip_pool }}\"<\/pre>\n<p>The great thing about this script is that none of the secrets are put into it. \u00a0Using the environment variables that we did by sourcing the ~\/.project-openrc.sh file we are able to run the code perfectly.<\/p>\n<p>Everything here is pretty self explanatory in that we are just passing in variables to the nova_compute task to bring up a new instance. \u00a0The name will be demo-server and everything else we&#8217;ve defined. \u00a0If the instance is already up, Ansible won&#8217;t go and try to provision a new one. \u00a0Its looking for demo-server, if he&#8217;s there, he won&#8217;t touch him.<\/p>\n<h2>3. Run the Playbook<\/h2>\n<pre class=\"lang:sh decode:true\">ansible-playbook copc-one.yml<\/pre>\n<p>We&#8217;re now watch the output on the dashboard and you can see it will spawn up.<\/p>\n<p><a href=\"http:\/\/benincosa.com\/wp-content\/uploads\/2015\/05\/Screen-Shot-2015-05-01-at-3.19.13-PM.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-3291\" src=\"http:\/\/benincosa.com\/wp-content\/uploads\/2015\/05\/Screen-Shot-2015-05-01-at-3.19.13-PM.png\" alt=\"Screen Shot 2015-05-01 at 3.19.13 PM\" width=\"1365\" height=\"179\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>The next step is to make it so we can run Ansible playbooks on this host. \u00a0The problem right now is that coreos is just a stripped down barebones OS. \u00a0So there is no Python! \u00a0We&#8217;ll have to add a cloud init script or do something else to make this work. \u00a0I&#8217;ll save that for another post. \u00a0But if you were using Ubuntu or RedHat, you&#8217;d be good to go at this point.<\/p>\n<h2>Code<\/h2>\n<p>All the code in this is available at <a href=\"https:\/\/github.com\/vallard\/blog\/tree\/master\/3288-Ansible-COPC-CoreOS\">github here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to show a quick example of how to deploy an instance on Cisco OpenStack Private Cloud (COPC or Cisco OPC or MetaCloud) with Ansible. \u00a0Since COPC is just a fully engineered and operated distribution of OpenStack from Cisco, this blog is also applicable to normal OpenStack environments. I&#8217;m a big fan of Ansible&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1013,676],"tags":[530,734,733,731,735],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/3288"}],"collection":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3288"}],"version-history":[{"count":5,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/3288\/revisions"}],"predecessor-version":[{"id":3297,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/3288\/revisions\/3297"}],"wp:attachment":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}