Posts

Ansible Roles using ansible-galaxy

Image
 Ansible Roles : Roles are consist of more than one playbooks like modules in puppet and cook books in Chef. Roles are used to perform mulitple tasks under one container with clean and neat directory structure. Roles are set of tasks and additional files to breakup the configuration for easy handling Tasks under role can be used by anyone if those rules/tasks maches with their needs. What is Ansible Galaxy? Ansible Galaxy is used to initiate a role which will create respective directory structure under that the role name. How to initiate a role using Galaxy? Command to initiate the role using Galaxy is   ansible-galaxy init <directory path> Eg: ansible-galaxy init /etc/ansible/roles/apache Now let us create the tasks Go to /etc/ansible/roles/apache/tasks vi  main.yml Above tasks does take care of installing httpd (apache) service, copying index.html to target server, starting service and enabling at the boot config. In single task file we are performing more th...

Ansible 4: Playbooks with notify & Handlers

Image
  Above example :  task1 => ensures httpd latest version is installed task2 => copy the apache config file from local machine to target servers upon completion of config file copy to target machine, it notifies the handler to restart the apache service task3 => Finally enable apache service to start at the boot configuration Note: There is an option to do pre-validation of how many hosts will be affected by running a particular playbook using below command synstax ansible-playbook install.yml --list-hosts

Ansible step3 (Play books)

 --- - hosts: myinventory   tasks:    - name: Install HTTPD Pkg     yum: name=httpd state=latest   - name: Start HTTPD service     service: name=httpd status=retarted That's it......above play book is ready to install httpd service and start the service , saved as 'install.yml' How to validate the playbook syntax is correct? ansible-playbook install.yml --syntax-check if there is any error then it will display the error msg on the screen How to run the playbook? ansible-playbook install.yml   if you don't mention the 'inventory' then it will take default inventory file from /etc/ansible/hosts otherwise you have to mention the inventory like below ansible-playbook -i myinventory install.yml   Wowww...you have successfully executed the playbook and it installs httpd service and service is started....playbook output will gives you number of successfully execution, failures etc. Let's write a play book for uninstalling the htt...

Ansible Step2

How to search modules? ansible-doc -l | more Grep modules which are in the name of 'users' ansible-doc -l |grep -i users Among all listed users releated modules, if i want to list the available options on any one of the module eg: 'user' ansible-doc user Sample execution of user creation command using ansible: ansible all -m user -a "name=mani password=Rhel1234" Here -m is moduel and -a is an Argument Installing httpd (apache) service using ad-hoc command mode in ansible ansible all -a "yum -y install httpd" Here -a is an argument Removing httpd (apache) service using ad-hoc command mode in ansible ansible all -a "yum -y remove httpd" Here -a is an argument Above install & remove commands are performed using '-a' argument but same can be done using '-m' module and '-a' argument, below the example Install httpd : ansible all -m yum -a "name=httpd state=present" note state=present means if not exist then...

Ansible Step1

Image
I'm following youtube channel  https://www.youtube.com/watch?v=KuTPh4GUGdc How to install Ansible in RHEL?  yum install ansible  or pip3 install ansible Checking configuration path [root@ip-172-31-77-201 ec2-user]# ansible-config --version ansible-config 2.9.12   config file = None   configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']   ansible python module location = /usr/local/lib/python3.8/site-packages/ansible   executable location = /usr/local/bin/ansible-config   python version = 3.8.0 (default, Mar  9 2020, 18:02:46) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] [root@ip-172-31-77-201 ec2-user]# List configurations  ansible-config list How ansible uses config files if you have more than one config files? eg:  1st preference goes to ANSIBLE_CONFIG variable export ANSIBLE_CONFIG =/tmp/ansible.cfg 2nd preference goes to present working directory, it could be anything /opt/myscript/a...