Part 3: Ansible and Dynamic Inventory

Before you forget, make sure to cp your aws credentials AND EC2 key to the jenkins user if that's the route you are going to take:

sudo cp ~/.aws/credentials /var/lib/jenkins/.aws/
sudo cp ~/.ssh/devops.pem /var/lib/jenkins/.ssh/
sudo chown jenkins:jenkins /var/lib/jenkins/.aws/credentials
sudo chown jenkins:jenkins /var/lib/jenkins/.ssh/devops.pem
 
We want to have ansible run and utilize a Dynamic Inventory to account for EC2 instances coming and going.
 
For this we need the following directory/files:
 
/etc/ansible/aws_ec2.yaml
/etc/ansible/group_vars/tag_group_web.yaml
/etc/ansible/ansible.cfg
./ansible/static-site/site.yaml
 
 
so let's start with the /etc/ folder. Recall that the boto_profile is your credential for JENKINS in this case.
 
/var/lib/jenkins/.aws/credentials
 
/etc/ansible/aws_ec2.yaml
plugin: amazon.aws.aws_ec2
boto_profile: scottyfullstack
regions:
  - us-east-1
strict: False
keyed_groups:
  - prefix: tag
    key: 'tags'
compose:
  ansible_host: ip_address
/etc/ansible/group_vars/tag_group_web.yaml
ansible_ssh_private_key_file: /var/lib/jenkins/.ssh/devops.pem
ansible_user: ubuntu
/etc/ansible/ansible.cfg
[defaults]
host_key_checking = False

[ssh_connection]
retries=3
and finally our ansible playbook in our directory (note the image at the end should be YOUR docker image:
 
./ansible/static-site/site.yaml
---
    - name: Provision Web Servers
      hosts: tag_group_web
      tasks:
    
        - name: Install pip3
          apt:
            update_cache: yes
            name: python3-pip
          become: yes
    
        - name: Install python docker sdk
          shell: |
            pip3 install docker
          become: yes
    
        - name: Install docker
          apt:
            name: docker.io
          become: yes
    
        - name: Start Docker
          shell: |
            systemctl start docker
            systemctl enable docker
          become: yes

        - name: Run image
          shell: docker run --name hello -dit -p 80:80 -p 443:443 scottyfullstack/nginx-static
          become: yes
And that's all for Ansible.
 

 

Part 4: Jenkins Pipeline

Comments

Popular posts from this blog

Terraform

Scrum Master Interview help - Bootcamp

Kubernetes