chrisluiz16

Modifile ansible+terraform

Jun 29th, 2023
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 14.61 KB | None | 0 0
  1.  
  2. ---------------------------------- Tesla- Infra ---------------
  3.  
  4. ec2-user@ip-172-31-33-5 ~]$ tree test-project/
  5. test-project/
  6. ├── aws.pem
  7. ├── httpd.conf.tmpl
  8. ├── main.yml
  9. ├── tesla-infra
  10. │   ├── main.tf
  11. │   ├── output.tf
  12. │   ├── terraform.tfstate
  13. │   ├── terraform.tfstate.backup
  14. │   ├── userdata.sh
  15. │   └── variables.tf
  16. └── virtual.conf.tmpl
  17.  
  18.  
  19.  
  20.  
  21. #### Main.tf ####
  22.  
  23.  
  24.  
  25. [ec2-user@ip-172-31-33-5 ~]$ cat test-project/tesla-infra/main.tf
  26. provider "aws" {
  27. access_key = var.access_key
  28. secret_key = var.secret_key
  29. region = var.region
  30.  
  31. default_tags {
  32. tags = {
  33. "Name"= "${var.project_name}-${var.project_env}",
  34. "Env"= var.project_env,
  35. "project" = var.project_name
  36. }
  37. }
  38. }
  39.  
  40. resource "aws_security_group" "web_access" {
  41.   name_prefix = "${var.project_name}-${var.project_env}-webaccess"
  42.   description = "Allow http and https ports from all"
  43.   ingress {
  44.     from_port        = 22
  45.     to_port          = 22
  46.     protocol         = "tcp"
  47.     cidr_blocks      = ["0.0.0.0/0"]
  48.     ipv6_cidr_blocks = ["::/0"]
  49.   }
  50.  
  51.   ingress {
  52.     from_port        = 80
  53.     to_port          = 80
  54.     protocol         = "tcp"
  55.     cidr_blocks      = ["0.0.0.0/0"]
  56.     ipv6_cidr_blocks = ["::/0"]
  57.   }
  58.  
  59.   egress {
  60.     from_port        = 0
  61.     to_port          = 0
  62.     protocol         = "-1"
  63.     cidr_blocks      = ["0.0.0.0/0"]
  64.     ipv6_cidr_blocks = ["::/0"]
  65.   }
  66.  lifecycle {
  67.     create_before_destroy = true
  68.   }
  69. }
  70.  
  71. resource "aws_instance" "webserver" {
  72.    count = 3
  73.   ami                    = var.ami_id
  74.   instance_type          = var.instance_type
  75.   key_name               = var.key
  76.   vpc_security_group_ids = [aws_security_group.web_access.id]
  77.   user_data = file("userdata.sh")
  78. }
  79.  
  80.  
  81.  
  82. #### Output.tf ####
  83.  
  84. [ec2-user@ip-172-31-33-5 ~]$ cat test-project/tesla-infra/output.tf
  85. output "private_ips"{
  86. value= aws_instance.webserver[*].private_ip
  87. }
  88.  
  89.  
  90.  
  91. #### Ansible main.yml ####
  92.  
  93. ---
  94.  
  95. - name: "Provisioning the tesla-infra using ansible+terraform"
  96.   hosts: localhost
  97.   become: true
  98.   vars:
  99.     terraform_amd64: https://releases.hashicorp.com/terraform/1.5.2/terraform_1.5.2_linux_amd64.zip
  100.   tasks:
  101.     - name: "get_url the terraform binary download"
  102.       get_url:
  103.         url: "{{ terraform_amd64 }}"
  104.         dest: /tmp/url.zip
  105.     - name: "Unarchive the zip file"
  106.       unarchive:
  107.         src: /tmp/url.zip
  108.         dest: /usr/local/bin/
  109.     - name: "Ansible Terraform module install"
  110.       community.general.ansible_galaxy_install:
  111.         type: collection
  112.         name: community.general
  113.     - name: "Cleanup Terraform installation in ansible master machine"
  114.       file:
  115.         path: /tmp/url.zip
  116.         state: absent
  117.     - name: "Terraform deployment of Tesla infra"
  118.       community.general.terraform:
  119.         project_path: ./tesla-infra
  120.         state: present
  121.         force_init: true
  122.       register: terraform_status
  123.     - name: "Printing the terraform servers private ip"
  124.       debug:
  125.         msg: "{{ item }}"
  126.       with_items:
  127.        - "{{ terraform_status.outputs.private_ips.value }}"
  128.  
  129.     - name: "Creating a dynamic inventory"
  130.       add_host:
  131.         hostname: "{{ item }}"
  132.         groups: Tesla-infra
  133.         ansible_ssh_host: "{{ item }}"
  134.         ansible_ssh_user: ec2-user
  135.         ansible_ssh_private_key_file: ./aws.pem
  136.         ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
  137.       with_items:
  138.        - "{{ terraform_status.outputs.private_ips.value }}"
  139.  
  140. - name: "Provisioning a php website"
  141.   hosts: Tesla-infra
  142.   become: true
  143.   serial: 1
  144.   vars:
  145.     packages:
  146.      - httpd
  147.       - php-fpm
  148.       - git
  149.     httpd_port: 80
  150.     httpd_site: blog.chrisich.fun
  151.     httpd_owner: apache
  152.     httpd_group: apache
  153.     repo_url: https://github.com/Chris-luiz-16/aws-elb-site.git
  154.   tasks:
  155.     - name: "Installing required packages for the website"
  156.       yum:
  157.         name: "{{ packages }}"
  158.         state: present
  159.       notify:
  160.        - httpd-restart
  161.         - php-restart
  162.     - name: "Copying the httpd.conf.tmpl to /etc/httpd/conf/httpd.conf"
  163.       template:
  164.         src: ./httpd.conf.tmpl
  165.         dest: /etc/httpd/conf/httpd.conf
  166.       notify:
  167.        - httpd-restart
  168.     - name: "Copying the virtaulhost of {{ httpd_site }} to /etc/httpd/conf.d/{{ httpd_site }}"
  169.       template:
  170.         src: ./virtual.conf.tmpl
  171.         dest: "/etc/httpd/conf.d/{{ httpd_site }}.conf"
  172.       notify:
  173.        - httpd-restart
  174.     - name: "Cloning the website to the remote server"
  175.       git:
  176.         repo: "{{ repo_url }}"
  177.         dest: /var/website/
  178.       register: git_status
  179.     - name: "Copying the website to the documentroot /var/www/html/{{ httpd_site }}/"
  180.       when: git_status.changed == true
  181.       copy:
  182.         src: /var/website/
  183.         dest: "/var/www/html/{{ httpd_site }}/"
  184.         owner: "{{ httpd_owner }}"
  185.         group: "{{ httpd_group }}"
  186.         remote_src: true
  187.       notify:
  188.        - php-restart
  189.   handlers:
  190.     - name: "httpd-restart"
  191.       service:
  192.         name: httpd
  193.         state: restarted
  194.         enabled: true
  195.     - name: "php-restart"
  196.       service:
  197.         name: php-fpm
  198.         state: restarted
  199.         enabled: true
  200.        
  201.        
  202.        
  203. #### terraform_status variable register value ######
  204.  
  205. ok: [localhost] => {
  206.     "terraform_status": {
  207.         "changed": false,
  208.         "command": "/usr/local/bin/terraform apply -no-color -input=false -auto-approve -lock=true /tmp/tmpg_fqwh7m.tfplan",
  209.         "failed": false,
  210.         "outputs": {
  211.             "private_ips": {
  212.                 "sensitive": false,
  213.                 "type": [
  214.                     "tuple",
  215.                     [
  216.                         "string",
  217.                         "string",
  218.                         "string"
  219.                     ]
  220.                 ],
  221.                 "value": [
  222.                     "172.31.35.102",
  223.                     "172.31.46.233",
  224.                     "172.31.33.85"
  225.                 ]
  226.             }
  227.         },
  228.         "state": "present",
  229.         "stderr": "",
  230.         "stderr_lines": [],
  231.         "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",
  232.         "stdout_lines": [
  233.             "aws_security_group.web_access: Refreshing state... [id=sg-0c8f08db169f46dde]",
  234.            "aws_instance.webserver[0]: Refreshing state... [id=i-079eb14222c6ddda1]",
  235.            "aws_instance.webserver[1]: Refreshing state... [id=i-0e388f456055701c4]",
  236.            "aws_instance.webserver[2]: Refreshing state... [id=i-00071fe08b8d20a58]",
  237.            "",
  238.            "No changes. Your infrastructure matches the configuration.",
  239.            "",
  240.            "Terraform has compared your real infrastructure against your configuration",
  241.            "and found no differences, so no changes are needed."
  242.        ],
  243.        "workspace": "default"
  244.    }
  245.  
  246.  
  247.  
  248.  
  249. #### Output ####
  250.  
  251. [ec2-user@ip-172-31-33-5 test-project]$ ansible-playbook main.yml
  252.  
  253.  
  254. PLAY [Provisioning the tesla-infra using ansible+terraform] ************************************************************************************************
  255.  
  256. TASK [Gathering Facts] *************************************************************************************************************************************
  257. ok: [localhost]
  258.  
  259. TASK [get_url the terraform binary download] ***************************************************************************************************************
  260. changed: [localhost]
  261.  
  262. TASK [Unarchive the zip file] ******************************************************************************************************************************
  263. ok: [localhost]
  264.  
  265. TASK [Ansible Terraform module install] ********************************************************************************************************************
  266. ok: [localhost]
  267.  
  268. TASK [Cleanup Terraform installation in ansible master machine] ********************************************************************************************
  269. changed: [localhost]
  270.  
  271. TASK [Terraform deployment of Tesla infra] *****************************************************************************************************************
  272. ok: [localhost]
  273.  
  274. TASK [Printing the terraform servers private ip] ***********************************************************************************************************
  275. ok: [localhost] => (item=172.31.35.102) => {
  276.    "msg": "172.31.35.102"
  277. }
  278. ok: [localhost] => (item=172.31.46.233) => {
  279.    "msg": "172.31.46.233"
  280. }
  281. ok: [localhost] => (item=172.31.33.85) => {
  282.    "msg": "172.31.33.85"
  283. }
  284.  
  285. TASK [Creating a dynamic inventory] ************************************************************************************************************************
  286.  
  287. changed: [localhost] => (item=172.31.35.102)
  288. changed: [localhost] => (item=172.31.46.233)
  289. changed: [localhost] => (item=172.31.33.85)
  290.  
  291. PLAY [Provisioning a php website] **************************************************************************************************************************
  292.  
  293. TASK [Gathering Facts] *************************************************************************************************************************************
  294.  
  295. ok: [172.31.35.102]
  296.  
  297. TASK [Installing required packages for the website] ********************************************************************************************************
  298. changed: [172.31.35.102]
  299.  
  300. TASK [Copying the httpd.conf.tmpl to /etc/httpd/conf/httpd.conf] *******************************************************************************************
  301. changed: [172.31.35.102]
  302.  
  303. TASK [Copying the virtaulhost of blog.chrisich.fun to /etc/httpd/conf.d/blog.chrisich.fun] *****************************************************************
  304. changed: [172.31.35.102]
  305.  
  306. TASK [Cloning the website to the remote server] ************************************************************************************************************
  307. changed: [172.31.35.102]
  308.  
  309. TASK [Copying the website to the documentroot /var/www/html/blog.chrisich.fun/] ****************************************************************************
  310. changed: [172.31.35.102]
  311.  
  312. RUNNING HANDLER [httpd-restart] ****************************************************************************************************************************
  313. changed: [172.31.35.102]
  314.  
  315. RUNNING HANDLER [php-restart] ******************************************************************************************************************************
  316. changed: [172.31.35.102]
  317.  
  318. PLAY [Provisioning a php website] **************************************************************************************************************************
  319.  
  320. TASK [Gathering Facts] *************************************************************************************************************************************
  321.  
  322. ok: [172.31.46.233]
  323.  
  324. TASK [Installing required packages for the website] ********************************************************************************************************
  325. changed: [172.31.46.233]
  326.  
  327. TASK [Copying the httpd.conf.tmpl to /etc/httpd/conf/httpd.conf] *******************************************************************************************
  328. changed: [172.31.46.233]
  329.  
  330. TASK [Copying the virtaulhost of blog.chrisich.fun to /etc/httpd/conf.d/blog.chrisich.fun] *****************************************************************
  331. changed: [172.31.46.233]
  332.  
  333. TASK [Cloning the website to the remote server] ************************************************************************************************************
  334. changed: [172.31.46.233]
  335.  
  336. TASK [Copying the website to the documentroot /var/www/html/blog.chrisich.fun/] ****************************************************************************
  337. changed: [172.31.46.233]
  338.  
  339. RUNNING HANDLER [httpd-restart] ****************************************************************************************************************************
  340. changed: [172.31.46.233]
  341.  
  342. RUNNING HANDLER [php-restart] ******************************************************************************************************************************
  343. changed: [172.31.46.233]
  344.  
  345. PLAY [Provisioning a php website] **************************************************************************************************************************
  346.  
  347. TASK [Gathering Facts] *************************************************************************************************************************************
  348.  
  349. ok: [172.31.33.85]
  350.  
  351. TASK [Installing required packages for the website] ********************************************************************************************************
  352. changed: [172.31.33.85]
  353.  
  354. TASK [Copying the httpd.conf.tmpl to /etc/httpd/conf/httpd.conf] *******************************************************************************************
  355. changed: [172.31.33.85]
  356.  
  357. TASK [Copying the virtaulhost of blog.chrisich.fun to /etc/httpd/conf.d/blog.chrisich.fun] *****************************************************************
  358. changed: [172.31.33.85]
  359.  
  360. TASK [Cloning the website to the remote server] ************************************************************************************************************
  361. changed: [172.31.33.85]
  362.  
  363. TASK [Copying the website to the documentroot /var/www/html/blog.chrisich.fun/] ****************************************************************************
  364. changed: [172.31.33.85]
  365.  
  366. RUNNING HANDLER [httpd-restart] ****************************************************************************************************************************
  367. changed: [172.31.33.85]
  368.  
  369. RUNNING HANDLER [php-restart] ******************************************************************************************************************************
  370. changed: [172.31.33.85]
  371.  
  372. PLAY RECAP *************************************************************************************************************************************************
  373. 172.31.33.85               : ok=8    changed=7    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  374. 172.31.35.102              : ok=8    changed=7    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  375. 172.31.46.233              : ok=8    changed=7    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  376. localhost                  : ok=8    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
Advertisement
Add Comment
Please, Sign In to add comment