chrisluiz16

Ansible+terraform

Jun 29th, 2023
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 10.91 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. ------------------------------------ Additional task -------------------------------------------------
  6.  
  7.  
  8.  
  9.  
  10.  
  11. ---
  12.  
  13. - name: "Provisioning the tesla-infra using ansible+terraform"
  14.   hosts: localhost
  15.   become: true
  16.   vars:
  17.     terraform_amd64: https://releases.hashicorp.com/terraform/1.5.2/terraform_1.5.2_linux_amd64.zip
  18.   tasks:
  19.     - name: "get_url the terraform binary download"
  20.       get_url:
  21.         url: "{{ terraform_amd64 }}"
  22.         dest: /tmp/url.zip
  23.     - name: "Unarchive the zip file"
  24.       unarchive:
  25.         src: /tmp/url.zip
  26.         dest: /usr/local/bin/
  27.     - name: "Ansible Terraform module install"
  28.       community.general.ansible_galaxy_install:
  29.         type: collection
  30.         name: community.general
  31.     - name: "Cleanup Terraform installation in ansible master machine"
  32.       file:
  33.         path: /tmp/url.zip
  34.         state: absent
  35.     - name: "Terraform deployment of Tesla infra"
  36.       community.general.terraform:
  37.         project_path: ./tesla-infra
  38.         state: present
  39.         force_init: true
  40.       register: terraform_status
  41.     - name: "Printing the terraform servers private ip"
  42.       debug:
  43.         msg: "{{ item }}"
  44.       with_items:
  45.        - "{{ terraform_status.outputs.private_ips.value }}"
  46.  
  47.     - name: "Creating a dynamic inventory"
  48.       add_host:
  49.         hostname: "{{ item }}"
  50.         groups: Tesla-infra
  51.         ansible_ssh_host: "{{ item }}"
  52.         ansible_ssh_user: ec2-user
  53.         ansible_ssh_private_key_file: ./aws.pem
  54.         ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
  55.       with_items:
  56.        - "{{ terraform_status.outputs.private_ips.value }}"
  57.  
  58. - name: "Provisioning a php website"
  59.   hosts: Tesla-infra
  60.   become: true
  61.   serial: 1
  62.   vars:
  63.     packages:
  64.      - httpd
  65.       - php-fpm
  66.       - git
  67.     httpd_port: 80
  68.     httpd_site: blog.chrisich.fun
  69.     httpd_owner: apache
  70.     httpd_group: apache
  71.     repo_url: https://github.com/Chris-luiz-16/aws-elb-site.git
  72.   tasks:
  73.     - name: "Installing required packages for the website"
  74.       yum:
  75.         name: "{{ packages }}"
  76.         state: present
  77.       notify:
  78.        - httpd-restart
  79.         - php-restart
  80.     - name: "Copying the httpd.conf.tmpl to /etc/httpd/conf/httpd.conf"
  81.       template:
  82.         src: ./httpd.conf.tmpl
  83.         dest: /etc/httpd/conf/httpd.conf
  84.       notify:
  85.        - httpd-restart
  86.     - name: "Copying the virtaulhost of {{ httpd_site }} to /etc/httpd/conf.d/{{ httpd_site }}"
  87.       template:
  88.         src: ./virtual.conf.tmpl
  89.         dest: "/etc/httpd/conf.d/{{ httpd_site }}.conf"
  90.       notify:
  91.        - httpd-restart
  92.     - name: "Cloning the website to the remote server"
  93.       git:
  94.         repo: "{{ repo_url }}"
  95.         dest: /var/website/
  96.       register: git_status
  97.     - name: "Copying the website to the documentroot /var/www/html/{{ httpd_site }}/"
  98.       when: git_status.changed == true
  99.       copy:
  100.         src: /var/website/
  101.         dest: "/var/www/html/{{ httpd_site }}/"
  102.         owner: "{{ httpd_owner }}"
  103.         group: "{{ httpd_group }}"
  104.         remote_src: true
  105.       notify:
  106.        - php-restart
  107.   handlers:
  108.     - name: "httpd-restart"
  109.       service:
  110.         name: httpd
  111.         state: restarted
  112.         enabled: true
  113.     - name: "php-restart"
  114.       service:
  115.         name: php-fpm
  116.         state: restarted
  117.         enabled: true
  118.        
  119.  
  120.  
  121. #### Output ####
  122.        
  123. [ec2-user@ip-172-31-33-5 test-project]$ ansible-playbook main.yml
  124. [WARNING]: No inventory was parsed, only implicit localhost is available
  125. [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
  126.  
  127. PLAY [Provisioning the tesla-infra using ansible+terraform] ************************************************************************************************
  128.  
  129. TASK [Gathering Facts] *************************************************************************************************************************************
  130. ok: [localhost]
  131.  
  132. TASK [get_url the terraform binary download] ***************************************************************************************************************
  133. changed: [localhost]
  134.  
  135. TASK [Unarchive the zip file] ******************************************************************************************************************************
  136. ok: [localhost]
  137.  
  138. TASK [Ansible Terraform module install] ********************************************************************************************************************
  139. ok: [localhost]
  140.  
  141. TASK [Cleanup Terraform installation in ansible master machine] ********************************************************************************************
  142. changed: [localhost]
  143.  
  144. TASK [Terraform deployment of Tesla infra] *****************************************************************************************************************
  145. ok: [localhost]
  146.  
  147. TASK [Printing the terraform servers private ip] ***********************************************************************************************************
  148. ok: [localhost] => (item=172.31.35.102) => {
  149.     "msg": "172.31.35.102"
  150. }
  151. ok: [localhost] => (item=172.31.46.233) => {
  152.     "msg": "172.31.46.233"
  153. }
  154. ok: [localhost] => (item=172.31.33.85) => {
  155.     "msg": "172.31.33.85"
  156. }
  157.  
  158. TASK [Creating a dynamic inventory] ************************************************************************************************************************
  159.  
  160. changed: [localhost] => (item=172.31.35.102)
  161. changed: [localhost] => (item=172.31.46.233)
  162. changed: [localhost] => (item=172.31.33.85)
  163.  
  164. PLAY [Provisioning a php website] **************************************************************************************************************************
  165.  
  166. TASK [Gathering Facts] *************************************************************************************************************************************
  167.  
  168. ok: [172.31.35.102]
  169.  
  170. TASK [Installing required packages for the website] ********************************************************************************************************
  171. changed: [172.31.35.102]
  172.  
  173. TASK [Copying the httpd.conf.tmpl to /etc/httpd/conf/httpd.conf] *******************************************************************************************
  174. changed: [172.31.35.102]
  175.  
  176. TASK [Copying the virtaulhost of blog.chrisich.fun to /etc/httpd/conf.d/blog.chrisich.fun] *****************************************************************
  177. changed: [172.31.35.102]
  178.  
  179. TASK [Cloning the website to the remote server] ************************************************************************************************************
  180. changed: [172.31.35.102]
  181.  
  182. TASK [Copying the website to the documentroot /var/www/html/blog.chrisich.fun/] ****************************************************************************
  183. changed: [172.31.35.102]
  184.  
  185. RUNNING HANDLER [httpd-restart] ****************************************************************************************************************************
  186. changed: [172.31.35.102]
  187.  
  188. RUNNING HANDLER [php-restart] ******************************************************************************************************************************
  189. changed: [172.31.35.102]
  190.  
  191. PLAY [Provisioning a php website] **************************************************************************************************************************
  192.  
  193. TASK [Gathering Facts] *************************************************************************************************************************************
  194.  
  195. ok: [172.31.46.233]
  196.  
  197. TASK [Installing required packages for the website] ********************************************************************************************************
  198. changed: [172.31.46.233]
  199.  
  200. TASK [Copying the httpd.conf.tmpl to /etc/httpd/conf/httpd.conf] *******************************************************************************************
  201. changed: [172.31.46.233]
  202.  
  203. TASK [Copying the virtaulhost of blog.chrisich.fun to /etc/httpd/conf.d/blog.chrisich.fun] *****************************************************************
  204. changed: [172.31.46.233]
  205.  
  206. TASK [Cloning the website to the remote server] ************************************************************************************************************
  207. changed: [172.31.46.233]
  208.  
  209. TASK [Copying the website to the documentroot /var/www/html/blog.chrisich.fun/] ****************************************************************************
  210. changed: [172.31.46.233]
  211.  
  212. RUNNING HANDLER [httpd-restart] ****************************************************************************************************************************
  213. changed: [172.31.46.233]
  214.  
  215. RUNNING HANDLER [php-restart] ******************************************************************************************************************************
  216. changed: [172.31.46.233]
  217.  
  218. PLAY [Provisioning a php website] **************************************************************************************************************************
  219.  
  220. TASK [Gathering Facts] *************************************************************************************************************************************
  221. .
  222. ok: [172.31.33.85]
  223.  
  224. TASK [Installing required packages for the website] ********************************************************************************************************
  225. changed: [172.31.33.85]
  226.  
  227. TASK [Copying the httpd.conf.tmpl to /etc/httpd/conf/httpd.conf] *******************************************************************************************
  228. changed: [172.31.33.85]
  229.  
  230. TASK [Copying the virtaulhost of blog.chrisich.fun to /etc/httpd/conf.d/blog.chrisich.fun] *****************************************************************
  231. changed: [172.31.33.85]
  232.  
  233. TASK [Cloning the website to the remote server] ************************************************************************************************************
  234. changed: [172.31.33.85]
  235.  
  236. TASK [Copying the website to the documentroot /var/www/html/blog.chrisich.fun/] ****************************************************************************
  237. changed: [172.31.33.85]
  238.  
  239. RUNNING HANDLER [httpd-restart] ****************************************************************************************************************************
  240. changed: [172.31.33.85]
  241.  
  242. RUNNING HANDLER [php-restart] ******************************************************************************************************************************
  243. changed: [172.31.33.85]
  244.  
  245. PLAY RECAP *************************************************************************************************************************************************
  246. 172.31.33.85               : ok=8    changed=7    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  247. 172.31.35.102              : ok=8    changed=7    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  248. 172.31.46.233              : ok=8    changed=7    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  249. localhost                  : ok=8    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  250.  
Advertisement
Add Comment
Please, Sign In to add comment