Setting Ansible on Google Cloud Platform for the first time requires some steps to follow. There may be several ways but i followed the below steps:
Assuming that you have couple of Virtual compute nodes created on your google cloud platform, i will be following the below names for sample references
My virtual instances on GCP are:
my-vmgroup-control-1 : This i am using as a control node.
my-vmgroup-app-instancegroup-1-lmlv : This is my traget inventory node 1
my-vmgroup-app-instancegroup-1-nxmr: This is my traget inventory node 2
Lets setup the ansible to work with the inventory nodes and perform our tasks.
1) Setup the ssh key
2) Add public key to the meta data on GCP cloud at project level.
3) setup the ansible environment
Setup the ssh key
Open the Google cloud platform and go to Virtual instances page.
Select the control node that you have setup and click on SSH button on the right. You can also use gloud command to make a connection and open the terminal.
After you logged in to you machine, run the below command.
ssh-keygen -t rsa -f ~/.ssh/<username> -C <choose the service account name here>
Assuming myserviceuser is the user you would connect to the remote instances which has got all privileges
Ex: ssh-keygen -t rsa -f ~/.ssh/myserviceuser -C myserviceuser
- note: Here i used -C mytestmethod which is going to act like a user which later on you need to use
This will create mytestmethod and mytestmethod.pub files under .ssh folder where the first will be your private key and the later one (.pub extention) will be your public key.
cat ~/.ssh/mytestmethod.pub
-- copy the text above
Add public key to the GCP meta data
Go to GCP console and type metadata in the search.
Select Metadata Compute Engine
click on SSH Keys and click Edit on the top
Click Add ITEM and the empty box place the public key that you copied in the previous step.
Save and see you will have new entry added with name mytestmethod and its key
-- note: This impacts all instances under your project.
Setup the Ansible for accessing the remote hosts
Let us assume that you have ansible installed.
Configure ansible.cfg in your home directory
$ vi ansible.cfg
[defaults]
host_key_checking=False
inventory=./hosts
ask_become_user=True
Create inventory file
$ vi hosts
[myhostgroup]
my-vmgroup-app-instancegroup-1-lmlv
my-vmgroup-app-instancegroup-1-nxmr
Alternately, if the user you chose to connect is a different one other than what you used to login on the control node, then you can add ansible_user to specifically redirect ansible to connect to remote host as the specific user like below
$ vi hosts
[myhostgroup]
my-vmgroup-app-instancegroup-1-lmlv ansible_user = <specific user>
my-vmgroup-app-instancegroup-1-nxmr ansible_user = <specific user>
--Note: make sure to have the specific user has all the required privileges so that you can still run your play book.
create a sample yaml to run your task
$ vi play.yml
---
#test payblook
- hosts: "{{group}}"
gather_facts: False
serial: 1
tasks:
- name: test
shell: ls -ltr
register : a
- debug: var=a.stdout_lines
Execute the Ansible on control node using the private key:
ansible-playbook play.yml --extra-vars "group=myhostgroup" --key-file ~/.ssh/myserviceuser
-- the above command uses the private key of the control node and connects to the remote host through the public key of the service account defined in meta data.
Hope this article is useful for you. Thank you and appreciate any feedback.
Comments
Post a Comment