Step1. Go to AWS account and Create 3 ec2 instances in the same AZ.
Step2. Take access to all machines via ssh.
Step3. Now go inside the Ansible node server and install the ansible package.
$ wget package_name $ apt install package_name -y $ apt update
Step4. Now we have to install all the packages simultaneously.
$ apt install ansible python openssl -y
Step5. Go to ansible.cfg file inside ansible server and uncomment two lines.
$ nano /etc/ansible/ansible.cfg 1.host_key_checking = False 2.remote_user = ubuntu
Step6. Now create the inventory.ini file inside the Ansible server and paste ansible_host, ansible_ssh_user, ansible_ssh_pass.
$ nano inventory.ini
Step7. Generate ssh key and copy the public key inside the Ansible server and paste the public key inside server hostA, hostB.
$ ssh-keygen
$ cd .ssh $ cat id_rsa.pub
Step8. Now login hostA, hostB server, and past the public key inside known_hosts file.
$ ssh -i pem-filename ubuntu@ip $ apt update $ cd .ssh $ nano known_hosts
Step9. Connect ansible server to hostA and hostB server.
$ ansible webservers -m ping
i. Ad-hoc command is commands which can be run individually to perform Quick function.
ii. These commands are not used for configuration management and deployment because these commands are of one-time usage.
iii. That can be used to ansible is an ad-hoc command that uses the /usr/bin/ansible command-line tool to automate a single task.
$ ansible ansiblehosts -a "df -Th"
i. The Ansible is a ships number of module libraries we can be executed in a direction on remote hosts through a 'playbook'.
ii. The library modules can reside in any machine; no servers, daemons, or databases are required.
For example:
$ ansible all -b -m service -a "name=apache2 state=restarted"
i. Playbook is ansible are written in YAML format.
ii. It is a human-readable data serialization language. It is commonly used for configuration files.
iii. Playbook is like a file where you write codes consist of vars, tasks, handlers, files, templates, and roles.
iv. Each playbook is composed of one or more 'modules' in a list module is a collection of configuration files.
v. Playbook is divided into many sections like -
a. Target section:- Define the host against which playbooks task has to be executed.
b. Variable section:- define variables
c. Task section:- List of all modules that need to run, in order.
i. Ansible user variables which are defined previously to enable more flexibility in playbook and roles. That is used to loop through a set of given values, access various information like the hostname in a system, and replace certain strings in a template with specific values.
ii. Put variable section above tasks so that we define it first & use it later.
i. A handler is the same as a task, but it will run when called by another task.
ii. The Handler is just like a regular task in an ansible playbook but is only run if it contains a notify directive and indicates that it changed something.
For example: Now create one playbook
$ nano nginx.yml - name: the install nginx hosts: webservers sudo: yes vars: pkgname: nginx tasks: - name: install nginx action: apt name=’{{pkgname}}’ state=installed - name: 'copy file an directory on webserver1' copy: src: /home/baldevpal/terraform/templatemo_525_the_town/ dest: /var/www/html/ notify: restart nginx handlers: - name: restart nginx service: name=nginx state=restarted $ ansible-playbook -i inventory.ini nginx.yml
i.The Ansible allows keeping for sensitive data such as passwords or keys in encrypted files, rather than plaintext in our playbooks.
$ ansible-vault encrypt target.yaml $ ansible-vault decrypt target.yaml
i.We can use two techniques for reusing a set of tasks includes and roles.
ii. Roles are good for organizing tasks and encapsulating data needed to accomplish those tasks.
Ansible-roles:
iii. We can organize playbooks into a directory structure called roles.
iv. Adding more and more functionality to the playbook will make it difficult to
maintain in a single file.
For example:
$ ansible-galaxy init my-role