Introduction
Go setup through Ansible eliminates the hassle and time-consuming nature of manual installations, making the process swift and hassle-free. With its powerful automation capabilities, Ansible allows for seamless Go installation across various environments, enhancing productivity and standardization. By diving into the specifics, one can witness how Ansible orchestrates the setup of Go across systems, configuring the necessary dependencies, paths, and environment variables effortlessly.
Leveraging Ansible for Go installation not only ensures uniformity in deployment but also simplifies maintenance and scalability, enabling developers to focus more on coding and less on the intricacies of setup across diverse systems.
Prerequisites
Key Takeaways from this post
- How to manually install Go (Linux)
- Go installation through Ansible
- How to convert commands into ansible tasks
- How to solve command not found issue after installation from Ansible
- How to create go setup role in Ansible
How to manually install Go (Linux)
- Download the go tar file:
wget https://go.dev/dl/go1.20.1.linux-amd64.tar.gz
- Delete previous installation:
rm -rf /usr/local/go
- Extract and move the new go folder to /usr/local:
sudo tar -C /usr/local -xzf go1.20.1.linux-amd64.tar.gz
- Delete downloaded tar file:
rm -rf go1.20.1.linux-amd64.tar.gz*
- Add go binary path to ~/.profile if it is not done earlier:
echo 'export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin' >> ~/.profile
- Update shell to make go binary available in current shell: source ~/.profile
- test:
go version
Go Setup through Ansible
We can convert the above commands to automate go setup into an Ansible playbook, it would look like the below
# go-setup-playbook.yml
---
- name: Go Setup
hosts: my_server
vars:
- version: 1.20.1
tasks:
- name: Download go tar file
command: wget https://go.dev/dl/go{{version}}.linux-amd64.tar.gz
- name: Delete previous installation
command: rm -rf /usr/local/go
become: true
- name: Extract and move new go folder to /usr/local
command: tar -C /usr/local -xzf go{{version}}.linux-amd64.tar.gz
become: true
- name: Delete downloaded tar file
shell: rm -rf go{{version}}.linux-amd64.tar.gz*
- name: Add go binary path to ~/.profile
lineinfile:
path: ~/.profile
line: 'export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin'
create: true
state: present
- name: Source updated profile
shell: . ~/.profile
- name: Test go version
command: go version
Now let us run the above playbook as ansible-playbook go-setup-playbook.yml
We see that every command execution succeeded except the last one(getting the error go command not found) and we wonder why it failed although it worked manually.
Let us understand why.
Ansible executes each task in a separate shell because that go version
is getting updated in a different shell as compared to the previous shell where source ~/.profile as . ~/.profile
. Because of this, the updated go binary path is not available when we run go version
.
The solution is simple: Run both commands in the same shell and it will work
The final updated playbook is as follows:
# go-setup-playbook.yml
---
- name: Go Setup
hosts: my_server
vars:
- version: 1.20.1
tasks:
- name: Download go tar file
command: wget https://go.dev/dl/go{{version}}.linux-amd64.tar.gz
- name: Delete previous installation
command: rm -rf /usr/local/go
become: true
- name: Extract and move new go folder to /usr/local
command: tar -C /usr/local -xzf go{{version}}.linux-amd64.tar.gz
become: true
- name: Delete downloaded tar file
shell: rm -rf go{{version}}.linux-amd64.tar.gz*
- name: Add go binary path to ~/.profile
lineinfile:
path: ~/.profile
line: 'export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin'
create: true
state: present
- name: Source updated profile and test go version
shell: . ~/.profile && go version
Go Setup through Ansible using role
Please go through the role in Ansible and then follow the below steps
Create a go setup role (setup-go)
Update roles/setup-go/defaults/main.yml as below
version: 1.20.1
Update roles/setup-go/tasks/main.yml as below
- name: Download go tar file
command: wget https://go.dev/dl/go{{version}}.linux-amd64.tar.gz
- name: Delete previous installation
command: rm -rf /usr/local/go
become: true
- name: Extract and move new go folder to /usr/local
command: tar -C /usr/local -xzf go{{version}}.linux-amd64.tar.gz
become: true
- name: Delete downloaded tar file
shell: rm -rf go{{version}}.linux-amd64.tar.gz*
- name: Add go binary path to ~/.profile
lineinfile:
path: ~/.profile
line: 'export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin'
create: true
state: present
- name: Source updated profile and test go version
shell: . ~/.profile && go version
Conclusion
In conclusion, the streamlined process of Go setup through Ansible encapsulates efficiency and simplicity within a single playbook. This approach not only minimizes the complexities associated with manual installations but also ensures consistency and reliability across diverse systems. By crafting a concise playbook for Go setup, Ansible empowers users to effortlessly deploy Go on multiple local or remote systems.
The playbook simplifies the Go setup process, automating the necessary steps to fetch, install, and configure Go, thereby saving time and effort for developers and administrators.
We have also extended the solution for go setup through Ansible using role.
Additionally, this centralized and automated approach through Ansible promotes scalability and maintainability, allowing for swift updates or modifications to the installation procedure without compromising accuracy.
Enjoy the post!