Ways of Working in Ansible
- Figure out the manual steps
- Execute and verify if they are working
- for each manual step findout an ansible module which can help
Ansible Module
- this is the smallest unit of work in Ansible
- Module takes some inputs which are generally referred as parameters and it has state
- Ansible modules Refer Here
Ansible Execution Approaches
- Adhoc command: This uses the following structure and it is used for non repetitive tasks
ansible -m "<module-name>" -a "<arguments>" <where>
- Playbook: This is yaml representation of sequence of commands and it is designed to be reptitive.
Activity: install apache server on node 1
- manual steps: Refer Here
sudo apt update
sudo apt install apache2 -y
- Finding modules
- Dev Env: Use Visual studio code and install
ansible
extension from Redhat
- Refer Here for the changes
- Syntax check
ansible-playbook --syntax-check -i <path to inventory> <path to playbook.yaml>
ansible-playbook --syntax-check -i hosts activity1.yaml
* Check the execution (dry run). Note this is not always correct
ansible-playbook --check -i <path to inventory> <path to playbook.yaml>
ansible-playbook --check -i hosts activity1.yaml
* Now lets execute the playbook
ansible-playbook -i <path to inventory> <path to playbook.yaml>
ansible-playbook -i hosts activity1.yaml
* Now lets rerun
Activity 2: Install php on apache server
- Manual steps: Refer Here for getting the steps
sudo apt udpate
sudo apt install apache2 -y
sudo apt install php libapache2-mod-php php-mysql -y
- Lets create a file
/var/www/html/info.php
with the following content
<?php phpinfo(); ?>
- Now access
http://publicip/info.php
- Refer Here for the changes done
- Execute ansible playbook after syntax check and check
ansible-playbook -i hosts php.yaml
Activity 3: Perform the activity2 on redhat
- I need to setup a redhat vm with devops user
- manual steps:
sudo dnf install httpd -y
sudo dnf install php php-cli php-common php-gd php-mysqlnd php-pdo -y
- Lets create a file
/var/www/html/info.php
with the following content
<?php phpinfo(); ?>
- Refer Here for the playbook
- Execute ansible playbook after syntax check and check
ansible-playbook -i hosts php.yaml
- We need to start the service Refer Here for the changes done to start and enable the service
Activity 4: Use the same playbook for ubuntu and redhat machines
- To do this activity, the two topics which we need to understand are
- inventory groups
- facts