/ansible_jenkins

Use ansible to control ec2 instance.

Primary LanguageHCL

ansible: v2.14.1 ansible-lint: v6.10.0 python: v3.10.9

Before running the project.

  1. Install ansible.
brew install ansible
pip3 install ansible-lint # Optional
brew install hudochenkov/sshpass/sshpass

# Check
ansible --version
ansible-lint --version # Optional
  • Check syntax (Optional)
ansible-lint <your playbook>
  1. Install docker collection
ansible-galaxy collection install community.docker

# Check
ansible-galaxy collection list
  1. Customize your data.
  • Modify hosts with your hostname and user.
  • Modify ansible.cfg with your private_key_file path.
  • Modify ./infra/config/config.s3.tfbackend with your config values.
  • Run the following command to check connection.
# `server` indicate `[server]` in `hosts`.
# You can use `all` instead of `server` to ping all the inventory in `hosts`.
ansible server -m ping

Use Ansible server to run terraform on controller node (EC2).

image

  1. Setup Security group and IAM to your EC2 instance.

  2. Run playbook

ansible-playbook deploy_s3bucket.yml

# If your file is encrypted.
ansible-playbook deploy_s3bucket.yml -i hosts --ask-vault-pass

Use Ansible server to setup Jenkins on controller node (EC2).

image

  1. Setup Security group and IAM to your EC2 instance.

  2. Run playbook

ansible-playbook deploy_docker_jenkins.yml

# If your file is encrypted.
ansible-playbook deploy_docker_jenkins.yml -i hosts --ask-vault-pass
  1. Open your browser and enter your EC2's Public IPv4 DNS with port 8080 (http).

Use local Jenkins server's pipeline to run Ansible which run terraform on controller node (EC2).

image

  1. Run jenkins with docker
docker run --name myjenkins -p 8080:8080 -p 50000:50000 -v $HOME:/var/jenkins_home jenkins/jenkins:lts-jdk11
  1. Access http://localhost:8080 and authorized with admin password. Default path of password is /var/jenkins_home/secrets/initialAdminPassword.

  2. Install Ansible with available version

# Enter container with root user.
docker exec -it --user root myjenkins bash

# Install Ansible
apt update
apt install ansible

# Check version
ansible --version
  1. Follow the video to create git repo credential

  2. Follow the tutorial to setup Ansible plugin, Jenkins Pipeline

  • Run which ansible to show the path.
  • Navigate to Dashboard > Manage Jenkins > Global Tool Configuration and setup Ansible.
  1. After building the pipeline, Jenkins will load the Jenkinsfile and update the content.