How to do it…

  1. For this, we need to have a key added to OpenStack before we boot the virtual machine. The name of the key has to be unique. This is what we will specify while booting an instance. So let's add our public key as follows:
- name: adding public key
os_keypair:
name: aditya
public_key: {{ aditya_pub_key }}
  1. We have used the variable for improving the readability of the code. It also helps if we need to change the keys quickly. We will add the value of the aditya_pub_key variable to the vars/main.yml file, as follows:
---
aditya_pub_key: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCbg83WYIxUfXWJ4bQiYfZYHceDwMJxnGfJqgYtHL/DAtegVY+Nm8MX3CRZYisfskt0m9CQ6y/Ux1OITTz+O11fgxLJcroZmKJbWW0K39gfHvFqR7FIe0zuJaxqUQUuyc0i6RCBRiZPiQQOPes2yDtfHgDWx3q9knS3ZXIAXcGLZrgfC1XnIK8CLAnZDved9Rue2bhsCnO9Mleh9g/CTtehMDAzD4NeSv9eETlHYkYSpJg8gFA3BFICpBxTqWSjf1mMQGSmiudFOhRjHIxL1Tvh+pnjSoL/jrLcP3RtMVuG0ZU0qkoAts1qpTwmyAJUz9Ts2EeyDJ0tXsFAiOFbmuMd [email protected]
  1. If we ever need to, we can delete an existing key from OpenStack by using the state parameter with the task, as follows:
- name: delete a public key
os_keypair:
name: aditya
state: absent
We should note that this will not remove the keys installed in already-running virtual machines—we need to do that on our own. Of course, Ansible is a great tool to do that as well.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset