yum install python3 -y
pip3 install boto
pip3 install boto3
wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py
wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini
chmod +x ec2.py
chmod +x ec2.ini
export AWS_REGION='ap-south-1'
export AWS_ACCESS_KEY_ID='XXXXX'
export AWS_SECRET_ACCESS_KEY='XXXXXX'
ansible-galaxy init aws_instance
- name: provision os in aws
ec2:
key_name: "mykey_ssh1"
region: "ap-south-1"
image: "{{ ami_id }}"
instance_type: "{{ instance_type }}"
vpc_subnet_id: "{{ vpc_subnet_id }}"
count: 1
state: present
wait: yes
assign_public_ip: yes
aws_access_key: "{{ akey }}"
aws_secret_key: "{{ skey }}"
group_id: "{{ security_group }}"
- name: Refresh inventory to ensure new instances exist in inventory
meta: refresh_inventory
- name: wait for a while
pause:
minutes: 2
ami_id: "ami-052c08d70def0ac62"
instance_type: "t2.micro"
vpc_subnet_id: "subnet-8ad2e8e2"
security_group: "sg-02d0e9263a788f8aa"
ansible-galaxy init webserver
- name: Install Required Package
package:
name: python3
state: present
become: true
- name: install httpd package
package:
name: "httpd"
state: present
when: ansible_distribution == "RedHat"
become: true
register: x
- name: document root dir
file:
state: directory
dest: "{{ dcdir }}"
register: y
become: true
- name: conf web server
template:
dest: /etc/httpd/conf.d/gaurav.conf
src: templates/localconf.conf.j2
when: x.rc == 0
notify: restart services
become: true
- name: copy web page from url
get_url:
dest: "{{ dcdir }}"
url: "https://raw.githubusercontent.com/Gaurav-2001/Ansible-Task2/master/index.html"
when: y.failed == false
become: true
dcdir: "/var/www/gaurav/"
port: 81
~
- name: restart services
service:
name: httpd
state: restarted
become: true
~
~
Listen {{ port }}
<VirtualHost {{ ansible_default_ipv4.address }}:{{ port }}>
DocumentRoot {{ dcdir }}
</VirtualHost>
- hosts: localhost
vars_files:
- secure.yml
roles:
- aws_instance
- hosts: ec2
user: ec2-user
roles:
- webserver
Great work.