Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------------------------- Tesla- Infra ---------------
- ec2-user@ip-172-31-33-5 ~]$ tree test-project/
- test-project/
- ├── aws.pem
- ├── httpd.conf.tmpl
- ├── main.yml
- ├── tesla-infra
- │ ├── main.tf
- │ ├── output.tf
- │ ├── terraform.tfstate
- │ ├── terraform.tfstate.backup
- │ ├── userdata.sh
- │ └── variables.tf
- └── virtual.conf.tmpl
- #### Main.tf ####
- [ec2-user@ip-172-31-33-5 ~]$ cat test-project/tesla-infra/main.tf
- provider "aws" {
- access_key = var.access_key
- secret_key = var.secret_key
- region = var.region
- default_tags {
- tags = {
- "Name"= "${var.project_name}-${var.project_env}",
- "Env"= var.project_env,
- "project" = var.project_name
- }
- }
- }
- resource "aws_security_group" "web_access" {
- name_prefix = "${var.project_name}-${var.project_env}-webaccess"
- description = "Allow http and https ports from all"
- ingress {
- from_port = 22
- to_port = 22
- protocol = "tcp"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- ingress {
- from_port = 80
- to_port = 80
- protocol = "tcp"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- egress {
- from_port = 0
- to_port = 0
- protocol = "-1"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- lifecycle {
- create_before_destroy = true
- }
- }
- resource "aws_instance" "webserver" {
- count = 3
- ami = var.ami_id
- instance_type = var.instance_type
- key_name = var.key
- vpc_security_group_ids = [aws_security_group.web_access.id]
- user_data = file("userdata.sh")
- }
- #### Output.tf ####
- [ec2-user@ip-172-31-33-5 ~]$ cat test-project/tesla-infra/output.tf
- output "private_ips"{
- value= aws_instance.webserver[*].private_ip
- }
- #### Ansible main.yml ####
- ---
- - name: "Provisioning the tesla-infra using ansible+terraform"
- hosts: localhost
- become: true
- vars:
- terraform_amd64: https://releases.hashicorp.com/terraform/1.5.2/terraform_1.5.2_linux_amd64.zip
- tasks:
- - name: "get_url the terraform binary download"
- get_url:
- url: "{{ terraform_amd64 }}"
- dest: /tmp/url.zip
- - name: "Unarchive the zip file"
- unarchive:
- src: /tmp/url.zip
- dest: /usr/local/bin/
- - name: "Ansible Terraform module install"
- community.general.ansible_galaxy_install:
- type: collection
- name: community.general
- - name: "Cleanup Terraform installation in ansible master machine"
- file:
- path: /tmp/url.zip
- state: absent
- - name: "Terraform deployment of Tesla infra"
- community.general.terraform:
- project_path: ./tesla-infra
- state: present
- force_init: true
- register: terraform_status
- - name: "Printing the terraform servers private ip"
- debug:
- msg: "{{ item }}"
- with_items:
- - "{{ terraform_status.outputs.private_ips.value }}"
- - name: "Creating a dynamic inventory"
- add_host:
- hostname: "{{ item }}"
- groups: Tesla-infra
- ansible_ssh_host: "{{ item }}"
- ansible_ssh_user: ec2-user
- ansible_ssh_private_key_file: ./aws.pem
- ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
- with_items:
- - "{{ terraform_status.outputs.private_ips.value }}"
- - name: "Provisioning a php website"
- hosts: Tesla-infra
- become: true
- serial: 1
- vars:
- packages:
- - httpd
- - php-fpm
- - git
- httpd_port: 80
- httpd_site: blog.chrisich.fun
- httpd_owner: apache
- httpd_group: apache
- repo_url: https://github.com/Chris-luiz-16/aws-elb-site.git
- tasks:
- - name: "Installing required packages for the website"
- yum:
- name: "{{ packages }}"
- state: present
- notify:
- - httpd-restart
- - php-restart
- - name: "Copying the httpd.conf.tmpl to /etc/httpd/conf/httpd.conf"
- template:
- src: ./httpd.conf.tmpl
- dest: /etc/httpd/conf/httpd.conf
- notify:
- - httpd-restart
- - name: "Copying the virtaulhost of {{ httpd_site }} to /etc/httpd/conf.d/{{ httpd_site }}"
- template:
- src: ./virtual.conf.tmpl
- dest: "/etc/httpd/conf.d/{{ httpd_site }}.conf"
- notify:
- - httpd-restart
- - name: "Cloning the website to the remote server"
- git:
- repo: "{{ repo_url }}"
- dest: /var/website/
- register: git_status
- - name: "Copying the website to the documentroot /var/www/html/{{ httpd_site }}/"
- when: git_status.changed == true
- copy:
- src: /var/website/
- dest: "/var/www/html/{{ httpd_site }}/"
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_group }}"
- remote_src: true
- notify:
- - php-restart
- handlers:
- - name: "httpd-restart"
- service:
- name: httpd
- state: restarted
- enabled: true
- - name: "php-restart"
- service:
- name: php-fpm
- state: restarted
- enabled: true
- #### terraform_status variable register value ######
- ok: [localhost] => {
- "terraform_status": {
- "changed": false,
- "command": "/usr/local/bin/terraform apply -no-color -input=false -auto-approve -lock=true /tmp/tmpg_fqwh7m.tfplan",
- "failed": false,
- "outputs": {
- "private_ips": {
- "sensitive": false,
- "type": [
- "tuple",
- [
- "string",
- "string",
- "string"
- ]
- ],
- "value": [
- "172.31.35.102",
- "172.31.46.233",
- "172.31.33.85"
- ]
- }
- },
- "state": "present",
- "stderr": "",
- "stderr_lines": [],
- "stdout": "aws_security_group.web_access: Refreshing state... [id=sg-0c8f08db169f46dde]\naws_instance.webserver[0]: Refreshing state... [id=i-079eb14222c6ddda1]\naws_instance.webserver[1]: Refreshing state... [id=i-0e388f456055701c4]\naws_instance.webserver[2]: Refreshing state... [id=i-00071fe08b8d20a58]\n\nNo changes. Your infrastructure matches the configuration.\n\nTerraform has compared your real infrastructure against your configuration\nand found no differences, so no changes are needed.\n",
- "stdout_lines": [
- "aws_security_group.web_access: Refreshing state... [id=sg-0c8f08db169f46dde]",
- "aws_instance.webserver[0]: Refreshing state... [id=i-079eb14222c6ddda1]",
- "aws_instance.webserver[1]: Refreshing state... [id=i-0e388f456055701c4]",
- "aws_instance.webserver[2]: Refreshing state... [id=i-00071fe08b8d20a58]",
- "",
- "No changes. Your infrastructure matches the configuration.",
- "",
- "Terraform has compared your real infrastructure against your configuration",
- "and found no differences, so no changes are needed."
- ],
- "workspace": "default"
- }
- #### Output ####
- [ec2-user@ip-172-31-33-5 test-project]$ ansible-playbook main.yml
- PLAY [Provisioning the tesla-infra using ansible+terraform] ************************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************************************************
- ok: [localhost]
- TASK [get_url the terraform binary download] ***************************************************************************************************************
- changed: [localhost]
- TASK [Unarchive the zip file] ******************************************************************************************************************************
- ok: [localhost]
- TASK [Ansible Terraform module install] ********************************************************************************************************************
- ok: [localhost]
- TASK [Cleanup Terraform installation in ansible master machine] ********************************************************************************************
- changed: [localhost]
- TASK [Terraform deployment of Tesla infra] *****************************************************************************************************************
- ok: [localhost]
- TASK [Printing the terraform servers private ip] ***********************************************************************************************************
- ok: [localhost] => (item=172.31.35.102) => {
- "msg": "172.31.35.102"
- }
- ok: [localhost] => (item=172.31.46.233) => {
- "msg": "172.31.46.233"
- }
- ok: [localhost] => (item=172.31.33.85) => {
- "msg": "172.31.33.85"
- }
- TASK [Creating a dynamic inventory] ************************************************************************************************************************
- changed: [localhost] => (item=172.31.35.102)
- changed: [localhost] => (item=172.31.46.233)
- changed: [localhost] => (item=172.31.33.85)
- PLAY [Provisioning a php website] **************************************************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************************************************
- ok: [172.31.35.102]
- TASK [Installing required packages for the website] ********************************************************************************************************
- changed: [172.31.35.102]
- TASK [Copying the httpd.conf.tmpl to /etc/httpd/conf/httpd.conf] *******************************************************************************************
- changed: [172.31.35.102]
- TASK [Copying the virtaulhost of blog.chrisich.fun to /etc/httpd/conf.d/blog.chrisich.fun] *****************************************************************
- changed: [172.31.35.102]
- TASK [Cloning the website to the remote server] ************************************************************************************************************
- changed: [172.31.35.102]
- TASK [Copying the website to the documentroot /var/www/html/blog.chrisich.fun/] ****************************************************************************
- changed: [172.31.35.102]
- RUNNING HANDLER [httpd-restart] ****************************************************************************************************************************
- changed: [172.31.35.102]
- RUNNING HANDLER [php-restart] ******************************************************************************************************************************
- changed: [172.31.35.102]
- PLAY [Provisioning a php website] **************************************************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************************************************
- ok: [172.31.46.233]
- TASK [Installing required packages for the website] ********************************************************************************************************
- changed: [172.31.46.233]
- TASK [Copying the httpd.conf.tmpl to /etc/httpd/conf/httpd.conf] *******************************************************************************************
- changed: [172.31.46.233]
- TASK [Copying the virtaulhost of blog.chrisich.fun to /etc/httpd/conf.d/blog.chrisich.fun] *****************************************************************
- changed: [172.31.46.233]
- TASK [Cloning the website to the remote server] ************************************************************************************************************
- changed: [172.31.46.233]
- TASK [Copying the website to the documentroot /var/www/html/blog.chrisich.fun/] ****************************************************************************
- changed: [172.31.46.233]
- RUNNING HANDLER [httpd-restart] ****************************************************************************************************************************
- changed: [172.31.46.233]
- RUNNING HANDLER [php-restart] ******************************************************************************************************************************
- changed: [172.31.46.233]
- PLAY [Provisioning a php website] **************************************************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************************************************
- ok: [172.31.33.85]
- TASK [Installing required packages for the website] ********************************************************************************************************
- changed: [172.31.33.85]
- TASK [Copying the httpd.conf.tmpl to /etc/httpd/conf/httpd.conf] *******************************************************************************************
- changed: [172.31.33.85]
- TASK [Copying the virtaulhost of blog.chrisich.fun to /etc/httpd/conf.d/blog.chrisich.fun] *****************************************************************
- changed: [172.31.33.85]
- TASK [Cloning the website to the remote server] ************************************************************************************************************
- changed: [172.31.33.85]
- TASK [Copying the website to the documentroot /var/www/html/blog.chrisich.fun/] ****************************************************************************
- changed: [172.31.33.85]
- RUNNING HANDLER [httpd-restart] ****************************************************************************************************************************
- changed: [172.31.33.85]
- RUNNING HANDLER [php-restart] ******************************************************************************************************************************
- changed: [172.31.33.85]
- PLAY RECAP *************************************************************************************************************************************************
- 172.31.33.85 : ok=8 changed=7 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- 172.31.35.102 : ok=8 changed=7 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- 172.31.46.233 : ok=8 changed=7 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- localhost : ok=8 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Advertisement
Add Comment
Please, Sign In to add comment