Lets add our cluster of servers to Ansible infrastucture
In this demo i will be only using
AnsibleSlaves-YourProdServer as my prod server.
sudo nano /etc/ansible/hosts
- In this file you will find templetes to add hosts
- lets add our production group of servers
- next line , give a name to server which you are going to add.
ansible_ssh_host=your ip address of your slave.
[production] app1 ansible_ssh_host= ipaddress of your slave
- To test all connections
ansible -m ping all
- To test all connections in a group
ansible -m ping production
- To test connection of a server named app1 in a group
ansible -m ping app1
Below image shows the result of the ping
- Modules are the commands which help to perform task,in other words modules are kind of library that help to perform tasks.
- List of all modules - link
What are Tasks?
A set of instructions performed with modules.
Playbooks are written in YAML also Playbooks are lists of tasks that automatically execute against hosts.
Lets create an nginx playbook
ansible modules - https://docs.ansible.com/ansible/2.9/modules/list_of_all_modules.html
- lets create a YAML file
sudo nano nginxplaybook.yaml
- yaml scriping start with three dashes
- then you can give a name with start a single dash
- now we will update the next parameter which is our hosts, in this case it will be
productionif you wanna do for all servers use
- so if we need to run as root on targets, we will be setting
- define task now under
--- - name: install nginx server hosts: production become: yes tasks: - name: install nginx apt: name: nginx state: latest
- Before running the playbook lets check it.
ansible-playbook nginxplaybook.yaml --check
- Now lets run the play book
- Here we will deploy a website to the target server using Ansible.
--- - name: install nginx server hosts: production become: yes tasks: - name: install nginx apt: name: nginx state: latest - name: Clone a repo with separate git directory ansible.builtin.git: repo: https://github.com/anuvindhs/CLOUD-is-AWSome.git dest: /var/www/html/app1
- Lets run the play book
- Now go to your http://yourslaveip/app1 to see the magic
🎉🎉🎉🎉Congratulations on you first ansible infrastucture deployment with aws using playbook🎉🎉🎉🎉.
I highly recommend this Tips & Tricks with Examples from spacelift
, this is def comes handy when you are writing complex ansible scripts.