Showing posts with label ANSIBLE. Show all posts
Showing posts with label ANSIBLE. Show all posts

Yum module in Ansible




Yum module in Ansible | Normally to install ,upgrade or remove a package we use yum command  in linux operating system .Similarly when we are going to install a package using Ansible we use yum module. Through this module we can install, upgrade and remove our required packages.

How to update all packages in ansible

To update all packages at a time we use ‘*’ symbol in the name parameter in Ansible and you must mention the state to latest to update all packages.
- hosts: node1
  tasks:
  - name: Upgrade all yum packages 
    yum:
      name: "*"
      state: latest

If you want to update all packages except some package you must specify ‘exclude’ parameter which contains which packages you don’t want to update.

In the below example we want to update all packages except php package.

- hosts: node1
  tasks:
  - name: Exclude some packages for upgrade
    yum:
      name: "*"
      state: latest
      exclude: php*

How to install a package in Ansible

You can install a package in Ansible using yum module.Under  this yum module there are two arguments one is name and another is state.
Name: name of the package which is to be installed.
State: state of the packages. By default state of the package set to present. when we are going to upgrade a package the state will be latest. when we are going to remove  a package the state will be absent.

For an example we install httpd package.


---
- hosts: node1
  tasks:
  - name: Install  package
    yum:
      name: httpd
      state: present

Install Package in Ansible
Install Package in Ansible



 How to install a specific version of package

To install specific version of package we have to mention the version of the the package in the name argument along with package name.

- hosts: node1
  tasks:
  - name: Install specific version of a package 
    yum:
      name: httpd<package version>
      state: present

How to install the latest version of package in Ansible

When we are going to install latest version of package we have to set the state to latest and in the name argument set the package name.
Name: name of the package which is to be installed.
State: state of the packages. By default state of the package set to present. when we are going to upgrade a package the state will be latest. when we are going to remove  a package the state will be absent.
For an example we install httpd package in latest version.
- hosts: node1
  tasks:
  - name: Install latest yum package
    yum:
      name: httpd
      state: latest

How to install multiple packages in ansible

To install multiple package we use with_item module and state should be present.
You can set the state to installed instead of present.The functionality of the both is same.

In the below example we install lamp stack


- hosts: node1
  tasks:
  - name: yum
    yum:
      name: "{{ item }}"
      state: present
    with_items:
    - httpd
    - php
    - mysql

How to delete a  package in Ansible

You can remove a package in Ansible using yum module.Under  this yum module there are two arguments one is name and another is state.
Name: name of the package which is to be installed.
State: state of the packages. By default state of the package set to present. when we are going to upgrade a package the state will be latest. when we are going to remove  a package the state will be absent.

For an example we remove httpd package.
---
- hosts: node1
  tasks:
  - name: Install  package
    yum:
      name: httpd
      state: absent

If this article is helpful to know about  Yum module in Ansible please share this article.




Create Directory in Ansible

Directory  is very necessary for linux operation.You can create Directory in linux system using shell through command.But when we are going to create directory  large no of systems one after another  at a time we utilize  Ansible because it is extremely  easier to implement large no of systems at a same time. 

We are going to implement node1  machine though ansible.

In this tutorial we learn how to create directory in ansible,how to create multiples directories in Ansible,how to create local directory in ansible,how to change permission of a directory in ansible and how to change permission of a directory recursively. 



    Create  Directory in Ansible

     You can create a directory using ansible file module.Here we have to defined two arguments one is path and other is state.Normally we create a directory using mkdir command. When we are going to create a directory using ansible  we set the state parameters to ‘directory’  .


    ---
    - hosts: node1
      tasks:
      - name: ansible create directory example
        file:
          path: /opt/testdir
          state: directory
     
    create directory in ansible
    create directory in ansible

    Create multiple directories in Ansible

    We can create multiple directories running ansible playbook using  with_items module.
    Let’s try an example
    We are going to create testdir1,testdir2,testdir3  under /opt
    ---
    - hosts: all
      tasks:
      - name: create multiple directories
        file:
          path: "{{ item }}"
          state: directory
        with_items:
          - '/opt/testdir1'
          - '/opt/testdir2'
          - '/opt/testdir3'



    Create local directory in Ansible

    Local_action statement is used in ansible when we are going to create a directory on the local machine as well as remote machine simultaneously .Directoy is created on the local machine before creating the remote maching when we execute the playbook.
    ---
    - hosts: all
      tasks:
      - name:  create local directory in ansible
        local_action:
          module: file
          path: /opt/localdir
          state: directory


    Change  Permissions of a Directory in Ansible

    In the above example we have created  a directory with default permission .Now we want to change the directory defaults permission.So, We have to focused one arguments that is mode .
    Now we are going to change the default permission of testdir directory which is located at /opt .
    We can use the octal form or symbolic form to define the mode agument.
    ---
    - hosts: node1
      tasks:
      - name: change permission of directory in ansible
        file:
          path: /opt/testdir
          state: directory
          mode: 0777


    Change  Permissions of Directory recursively

    The above example we have changed only permission of a directory from its default permission.Now we are going to change the change the permission of this directory recursively that means change the pemission of the directory as well as change the permission of those files and directories which is inside the parent directory.

    ---
    - hosts: node1
      tasks:
      - name: change recursive permission of directory in ansible
        file:
          path: /opt/testdir
          state: directory
          mode: 0777
          recurse: yes

     Delete Directory in Ansible

    We can delete a directory in Ansible to set the state value to ‘absent’.

    ---
    - hosts: node1
      tasks:
      - name: delete directory in ansible
        file:
          path: /opt/testdir
          state: absent

    If this article is helpful to know about create directory in ansible please share and subscribe this article. 

    Create File in Ansible and copy content using ansible copy



    Create File in Ansible and copy content using ansible copy module |File is very necessary for linux operation.You can create file in linux system using shell through command.But when we are going to handle file large no of systems we use Ansible because it is very easier to implement large no of systems.

    In this article we learn how to create file in ansible using ansible file module and how ansible copy works and how to create multiple files in ansible.

    We are going to implement node1 remote machine via ansible control machine.

    Create an empty file 

    You can create an empty file using ansible file module.Here we have to defined two aguments one is path and other is state.Normally we create an empty file using touch command. When we are going to create an empty file using ansible  we set the state parameters to ‘touch’  .

    ---
    - hosts: node1
      tasks:
        - name: create blank file
          file:
             path: "/test.txt"          state: touch


    create file  ansible,ansible file module,ansible copy
    Create file  Ansible



    Create multiple files in ansible 


    We can create multiple new files running ansible playbook using file and with_items module.
    Let’s try an example
    We are going to create test1.txt,test2.txt under /
    ---
    - hosts: node1
      tasks:
        - name: create blank file
          file:
             path: "{{item}}"
             state: touch
             mode: 0777
           with_items:
             - test1.txt
             - test2.txt    


    Define permission of a file

    In the above example we have created  an empty file with default permission .Now we want to change the file defaults permission.So, We have to focused two arguments one is mode and other is owner.

    Now we are going to change the default permission of the test.txt file.
    We can use the octal form or symbolic form to define the mode argument and in the owner mode we mention the owner name.
    ---
    - hosts: node1
      tasks:
        - name: create blank file
          file:
             path: "/test.txt"
             mode: 0777
             owner: root

    Create a File with content using content argument

    Normally copy module is used to copy from source file to destination file.In the copy module there is an argument called content to create the file with content.


    ---
    - hosts: node1
      tasks:
        - name: create blank file
          copy:
             dest: "/test.txt"
             content: |
               This is test content


    Here dest argument is indicating the absolute location of the file in which the content should be written.If file is exists then ansible check any content is exists or not in the file.if content is exists then it overlap  the whole content.


    If this article is helpful to know about Create File in Ansible using ansible file module and copy content using ansible copy module  please share this article.

    Ansible playbook example



    Ansible Playbook |In this article we learn Ansible Playbook Example and create ansible playbook hosts .In previous article we have seen how to execute an ansible task using Ad-hoc command.We also knew that Ad-Hoc command is very useful to execute simple task  like reboot the server, changing permission of files and so on.But when we want to execute multitask at a time.Then ansible playbook takes a vital role to execute multitask at a time to configure and deployement on the remote machine.


    Ansible playbook is written by YAML language which is very simple language other than XML and JSON format.It is easy to understand for human being.

    Creation of ansible playbook

    Before creating ansible playbook we need to update the inventory file first which is vey necessary to gathering  the details of the remote machine on which ansible tasks will be executed.If you want  to change default inventory location ,you can specify the location of the inventory file using ‘-i’ switch.

    After that we need to create ansible playbook hosts .In this tutorial you have used  one host named host01.put its hostname in an inventory file call our inventory myhosts.

    echo “host01 ansible_ssh_user=root” >> myhosts

    How to run  ansible playbook


    To demonstrate ansible playbook example first we have create one file called yum.yaml in which ansible tasks will be written using YAML language.

    To create blank file using the below command
    touch yum.yaml

    Now we write code to execute the task.This is single task code.
    ---
    - hosts: host01
      tasks:
        - name:  ensure  latest sysstat  is installed
          yum:
            name:  sysstat
            state:   latest

    To run the playbook use ansible-playbook command
    Ansible-playbook   –i   myhosts  yum.yaml

    Ansible Playbook Example,Ansible playbook template
    Ansible Playbook Example

    Ansible should return “changed=1”indicating that the change has been occurred

    ·         ‘---‘   : start of the YAML file
    ·         hosts:  To un the tasks on which hosts.
    ·         Name:  The task name.
    ·         Yum:    Specify the module name
    ·         Name: argument of the specified module.

    If want to uninstall the package use state=absent.

    If this article is helpful to know about Ansible Playbook Example please share this article.





    How to execute ansible Ad-Hoc tasks in linux system with practical example



    Execute Ansible Ad-Hoc Commands |The simplest way to kniw about ansible basic by executing Ad- Hoc tasks. Ad-Hoc tasks are very needful when we are going to do common  tasks like changing permission of file, check the connectivity and so on.

    You can not create file using Ad-Hoc command. This is one time job.
    To start Ad-Hoc command at first we need to install latest version of Ansible.To install latest version of ansible please go though the installation procedure of Ansible.

    Before start to execute ansible command we need to gathering the details of remote hosts and which command to be execute.


    Create inventory file in ansible

    All details of all remote machines  are stored in the inventory file. By default the inventory  file is located at /etc/ansible/hosts. We can put the hostname of systems  and ip address of systems on which the command will be executed.
    In this tutorial you have used  one host named host01.

    put its hostname in an inventory file call our inventory myhosts, with
    one group called group1.
    echo “[group1]” > myhosts
    Then add the host to the group:
    echo “host01 ansible_ssh_user=root” >> myhosts


    How to execute Ad-Hoc command 


    We are going to execute date command .We have to convert the command into ansible tasks.
    To run an ad-hoc task we need to execute the below command. 

    For example, to check the current date and time of the remote host:

    ansible group1 -i myhosts -m command -a date

    ·         When we run above command ansible executes the command and give the output.


    To do this activity we need to establish passwordless connectivity between ansible control machine and remote machines. For more details on how to setup passwordless login you can refer

    https://techrideradmin.blogspot.com/2018/09/ssh-password-less-login.html

    Output:

    ansible group1 -i myhosts -m command -a date

    Ad-Hoc command
    Ad-Hoc command

    If this article is helpful to know about Execute Ansible Ad-Hoc Tasks please share this article.

    Ansible-YAML Syntax and Concept

    Ansible YAML  |Ansible uses YAML language to make ansible playbook through which control machine is executed automation tasks on remote machine.Yaml language is straightforward language and straightforward for the human being,but we should know yaml Syntax in all respects unmistakably .We use YAML on the grounds that it is simpler for people to peruse and compose than other format like XML or JSON.


    YAML Syntax


    To define Yaml syntax we should know the below points.

     YAML file must starts with “---” and should  ends with “...”.

    YAML supports  “|” to denote  newlines while showing multiple lines and “>” to suppress newlines while showing multiple lines.

    YAML Concept


    YAML utilizes basic key-value pair to speak to the data. 

    We should maintain a gap  among : and value.

    Employee record for an example:

    ---
    rahul:
       name: rahul sharma
       empNo: 120
       sex: male
    …

    How to represent list inside directory


    We can also represent list within directory using yaml language.

    For an example:

    ---
    rahul:
       name: rahul sharma
       empNo: 120
       sex: male
       dept:
       - sales
       - hr
       - marketing
    …

    How to represent list using yaml


    All members of lists are starting at same level and starting with “-”
    Note − There should be space after “_”.
    To represent list using yaml let take an example

    For an Example:

    ---
    Games:
       - Cricket
       - Football
       - polo
    …

    Create list for many dictionaries


    We can create list for many directories using yaml language. 

    YAML concept,Ansible,yaml syntax
    ansible YAML Syntax and Concept

    ---
    - rahul:
       name: rahul sharma
       empNo: 120
       sex: male
       dept:
       - sales
       - hr
       - marketing

    - rahim:
       name: rahim kumar
       empNo: 130
       sex: male
       dept:
       - finance
       - it
     ...


    If this article is helpful to know about Ansible YAML and yaml syntax please share this article.