Shashikant shah

Monday 19 October 2020

What is Ansible Playbooks part-7

 What is Ansible Playbooks.

We have preferred playbook where we need to perform any task on repeat basis means suppose we want to take backup of our all server, we can write a playbook for that. Playbooks are written in YAML language.

debug module
yum
shell
command
template
user:
stat - check if a file exists
apt
copy
service
file
lineinfile
archive/unarchive
git
package
ec2

##############################
debug module
vars:
    - tomcat_url: http://tomcat.com
  tasks:
  - name: install tomcat
    debug:
      msg: "{{ tomcat_url }}"
#################################
 yum/apt/package:
     name: httpd
     state: present
#################################
shell
- name: Change the working directory to somedir/ before executing the command.
  shell: somescript.sh >> somelog.txt
  args:
    chdir: somedir/
#################################
command
- name: Run the app installer
  command: "/app/install.sh"
or
- name: return motd to registered var
  command: cat /etc/motd
  register: mymotd
#################################
template
- name: Ensure MOTD file is in place
  template:
    src: templates/motd.j2
    dest: /etc/motd
#################################
user:
- name: Ensure user ricardo exists
  user:
    name: ricardo
    group: users
    groups: wheel
    uid: 2001
    password: "{{ 'mypassword' | password_hash('sha512') }}"
    state: present
##################################
stat:
- name: Check that the somefile.conf exists
stat
    path: /etc/file.txt
    register: stat_result
###################################
copy
  copy:
    src: files/motd
    dest: /etc/motd
####################################
service
service:
    name: sshd
    state: started
######################################
file (delete)
 file:
    path: /tmp/Shashi.txt
    state: absent
########################################
lineinfile
- name: Ensure root cannot login via ssh
  lineinfile:
    path: /etc/ssh/sshd_config
    regexp: '^PermitRootLogin'
    line: PermitRootLogin no
    state: present
#########################################
archive/unarchive
- name: Extract contents of app.tar.gz
  unarchive:
    src: /tmp/app.tar.gz
    dest: /app
    remote_src: yes
#########################################
git
- git:
    repo: 'https://foosball.example.org/path/to/repo.git'
    dest: /srv/checkout
    version: release-0.22
########################################
ec2:
 - name: create an ec2 instance

      ec2:
         key_name: "{{ keypair }}"
         group: "{{ security_group }}"  # security group name
         instance_type: "{{ instance_type}}"
         image: "{{ image }}"
         wait: true
         region: "{{ region }}"
         count: 1  # default
         count_tag:
            Name: Demo
         instance_tags:
            Name: Demo
         vpc_subnet_id: subnet-fb2bf1da
         assign_public_ip: yes

1)  -C option this cause ansible to report what changes would have occurred if the playbook were executed. but does not make any actual changes to managed hosts.  

# ansible-playbook  -C  webserver.yml

2) one by one task execute and showing Y or N Option. 
# ansible-playbook --step  webserver.yml

3) playbook syntax check. 
# ansible-playbook  webserver.yml  --syntax-check


Ansible playbook structure flow:-



























---

- hosts: jenkins                      ===> Remote name/group name 

  gather_facts: True                ===> default Variable True /False

  become: yes                        ===> root user privilege

  # remote_user: ansadm      ===> remote user defined 

  vars:                                   ===> Variable defined

          my_content: "this file created using var concept"   ===> variable name "my_content"  

  tasks:                                                        ===> First Play 1

   - name: create a file var_file.txt..        ===>  First Task and  Task Name

     copy:                                                    ===>  Module name

            dest: /tmp/var_file.txt

            content: "{{ my_content }}"

   - name: install nginx service                ===> Second Task 

     yum:

         name: httpd

         state: present

   - name: httpd service start                    ===> third Task

     service:

          name: httpd

          state: started

- name: remove file                                ===> fourth Task

   file:

          path: /tmp/Shashi.txt

          state: absent

 1. How to check playbook content . 

# ansible-playbook httpd.yml --syntax-check

# ansible-playbook -i host httpd.yml
        OR
# ansible-playbook httpd.yml

2. Logs redirect to output.log file.

# ansible-playbook httpd.yml | tee -a output.log


yum state :- 

  • latest.
  •  present.
  • installed.
  • absent.
  • removed.

Services : state :-

  • reloaded
  • restarted
  • running
  • started
  • stopped

1.  Restarting a Network Interface eth0
---
- name: Restart network service for interface eth0
  service:
    name: network
    state: restarted
    args: eth0

2. Copy Module
Copying Files from Local to Remote Linux
 
---
- name: Copy file with owner and permissions
  copy:
    src: /etc/files/jk.conf
    dest: /srv/jk.conf
    owner: shashi
    group: shashi
    mode: '0644'
 
3. File Module.

i) Perform Linux File Permissions.
---
- name: Change file ownership, group, and permissions
  file:
    path: /etc/jk.conf
    owner: root
    group: root
    mode: '0644'
 
ii) Delete Linux File.
---
- name: Remove file (delete file)
   file
     path: /etc/shashi.conf
     state: absent
 
iii) Create a Directory.
---
- name: create a directory if it doesn’t exist
  file:
    path: /etc/mydirectory
    State: directory
    mode: '0777'
 
iv) Recursiverly Delete a Directory.
---
- name: Recursively deleting a  directory
  file:
    path: /etc/shashi.conf
    state: absent
 
4. Lineinfile Module.
 
i) Manipulate Files in Linux.
---
 - name: Ensure SELinux is set to enforcing mode
  lineinfile:
    path: /etc/selinux/config
    regexp: '^SELINUX='
    line: SELINUX=disabled
 
The play above sets SELINUX value to disabled.
SELINUX=disabled
 
ii) Alter Files in Linux.
---
- name: Add a line to a file if the file does not exist, without         passing regexp
  lineinfile:
    path: /etc/hosts
    line: 10.200.50.51 shashi.com
    create: yes
 
This adds the entry 10.200.50.51 shashi.com to the /etc/hosts file.
 
5. Archive Module.
 
i) Create a Archive File.
- name: Compress directory /path/to/shashi_dir/ into /path/to/shashi.tgz
  archive:
    path: /path/to/shashi_dir
    dest: /path/to/shashi.tgz
 
ii) Create a Archive File and Remove.
- name: Compress regular file /path/to/shashi into /path/to/foo.gz and remove it
  archive:
    path: /path/to/shashi
    dest: /path/to/shashi.tgz
    remove: yes
 
iii) Create a Archive File.
- name: Create a bz2 archive of /path/to/shashi
  archive:
    path: /path/to/shashi
    format: bz2
 
6. Git Module.
---
- hosts: localhost
gather_facts: no
vars:
username: decodingdevops
token: d6sdfshhghyjggj448t8tnt9h
repo_name: devops
 
tasks:
- name: Checkout The Code From Github Using Ansible.
git:
repo: 'https://{{ token }}@github.com/{{ username }}/{{ repo_name }}.git'
dest: /root/mycode
 
7. Command Module.

i) cat command.
- name: Executing a command using the command module
  command: cat helloworld.txt
 
ii) Check Uptime of Remote Linux.
---
 - name: Check the remote host uptime
    hosts: servers
    tasks:
      - name: Execute the Uptime command over Command module
        register: uptimeoutput
        command: "uptime"
 
- debug:
          var: uptimeoutput.stdout_lines
 
8.Variables to Retrieve the Results of Running Commands.

i) Check Disk Space
---
 - hosts: all
   become: yes
 
   tasks:
     - name: Execute /boot usage on Hosts
       command: 'df -Th /'
       register: df
     - debug: var=df.stdout


No comments:

Post a Comment