DevOps Classroom Series – 19/Mar/2020

Ansible other modules

  • Editing a file without files/templates lineinfile
  • To Manage users, groups we have modules like user, group
  • To manage cron in linux we have module called as cron
  • Ansible is used a lot in networking, Refer Here
  • Ansible can be used for automating databased Refer Here

Idempotance

  • Genarally most of the modules of Ansible are idempotent, But some modules are not more specifically command modules
  • So when ever you use command modules it is your responsibility to make it idempotent
  • Command Modules
  • Command modules are low level commands (direct linux commands) and they execute every time
---
- hosts: all
  tasks:
    - name: execute command
      command: touch /home/devops/command.txt

  • Most commonly used solution for making non idempotent solutions idempotent is environmental variable or create files (these are checked to find out whether to execute or not)
---
- hosts: all
  tasks:
    - name: check if the file exists
      stat:
        path: /home/devops/commad.txt
      register: stat_op
    - name: execute command
      command: echo "hello" > /home/devops/commad.txt
      when: stat_op.stat.exists == False
    - name: cat text
      command: cat /home/devops/commad.txt
      register: op
      ignore_errors: yes
    - name: something wrong happened
      debug:
        var: op
      when: op.stderr != ""
    
  • Sample demonstration is
commandA
   => handler => create a file A
   when file A doesnot exist
   
commandB
   => handler => create a environmental B
   when environmentalB doesnot exist or has a value of your choice

Can i Reuse the Work done?

  • Ansible Supports reusing Playbooks, tasks and variables.
  • Ansible supports reusing in two ways
    • include_*
    • import_*
  • But we will start our discussion with Ansible Galaxy.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin