Use of runtime variables to save into another variable using register in Ansible

Another major use of variables is running a command and using the result of that command to save the result into a variable

 

- hosts: all
  tasks:
  - name: Ansible register variable basic example
    shell: "find *.txt"
    args:
      chdir: "/Users/mdtutorials2/Documents/Ansible"
    register: find_output

  - debug:
      var: find_output
#
- hosts: all
  tasks:
  - name: Ansible register with_items example
    shell: "find *.txt"
    args:
      chdir: "/Users/mdtutorials2/Documents/Ansible"
    register: with_output

  - shell: "cp {{ item }} {{item}}_bkp"
    with_items:
      - "{{ with_output.stdout_lines }}"
Tagged : / /

How to use template in Ansible?

How to use template in Ansible?

##### Step 1 – Create your inventory file with #####

$ vi inventory
35.154.85.120 ansible_user=root ansible_ssh_private_key_file=remote.pem
35.154.85.120 ansible_user=root ansible_ssh_private_key_file=remote.pem

##### Step 2 – Create templates directory #####

$ mkdir templates
$ vi templates/index.j2

<!DOCTYPE html>
<html>
<body>
<h1> Welcome to DevOpsSchool.com Ansible Training</h1>
<h3> This is Deployed in OS Family - {{ ansible_os_family }} </h3>
<h3> This is Deployed in OS Family - {{ ansible_hostname }} </h3>
<h3> Company Name - {{ companyname }} </h3>
companyname

</body>
</html>

<strong>##### Step 3 – Create a playbook.yaml#####</strong>

---
- name: This sets up an httpd webserver
hosts: all
remote_user: ec2-user
become: yes
vars:
ansible_ssh_private_key_file: remote.pem
companyname: DevOpsSchool.com
pack: httpd
tasks:
- name: Install the httpd apps
yum: name={{ pack }}
- name: Deploy configuration File
template: src=templates/index.j2 dest=/var/www/html/index.html
- name: start the httpd service
service: name={{ pack }} state=started

##### Step 4 – Last comamds to execute #####

$ ansible-playbook -i inventory httpd.yaml
Tagged : / / /

Run Ansible Adhoc commands or playbook in Local mode

If you are trying to run Ansible Adhoc commands or playbook in Local mode, these articles will help you.

Ansible can’t run playbooks locally without ssh if ansible_connection=local is defined in the hosts file, although it can run playbooks locally without ssh with connection: local in the playbook or with flag –connection=local.

# # # # # # # # # # # # Method 1 # # # # # # # # # # # # # # # # # #

ansible 127.0.0.1 -m file -a "dest=/opt/a.txt mode=600 state=touch"
ansible 127.0.0.1 -m file -a "dest=/opt/a.txt mode=600 state=file"

# # # # # # # # # # # # Method 2 # # # # # # # # # # # # # # # # # #

Inventory file
127.0.0.1 ansible_connection=local
localhost ansible_connection=local

# # # # # # # # # # # # Method 3 # # # # # # # # # # # # # # # # # #

---

- name: run the playbook tasks on the localhost
hosts: 127.0.0.1
connection: local
become: yes
tasks:

- name: print out the hostname of target
command: hostname

- name: ensure aptitude is installed
command: apt-get -y install aptitude

- name: update the apt package index i.e. apt-get update
apt: update_cache=yes

- name: upgrade system packages i.e. apt-get upgrade
apt: upgrade=yes

Tagged : / / / /

Ansible Playbook Lab & Excercise – Part 2

Execution Mode – Remote

  1. Write a Ansible Playbook to create a group called “deploy”

  2. Write a Ansible Playbook to create a user called “deploy-user” which is part of group called “group” and with /bin/bash shell.

  3. Write a Ansible Playbook to install package named “httpd” in RHEL/centos.

  4. Write a Ansible Playbook to start and enable the service named “httpd”

  5. Write a Ansible Playbook to create a file called “index.html” in /var/www/html with some dummy html contents.

  6. Write a Ansible Playbook to reboot a self machine.

  7. Write a Ansible Playbook to install a package called “git”, “wget”.

  8. Write a Ansible Playbook to clone git repo. thttps://github.com/scmgalaxy/ansible-role-template

  9. Now Merge all Top Playbook into one and run and verify

Tagged : / / / /

Ansible Adhoc Commands Lab & Excercise – Part 1

Execution Mode – Local

  1. Write a Ansible Adhoc Commands to create a group called “deploy”

  2. Write a Ansible Adhoc Commands to create a user called “deploy-user” which is part of group called “group” and with /bin/bash shell.

  3. Write a Ansble Adhoc commands install package named “httpd” in RHEL/centos.

  4. Write a Ansible Adhoc commands to start and enable the service named “httpd”

  5. Write a Ansible commands to create a file called “index.html” in /var/www/html with some dummy html contents.

  6. Write a Ansible commands to reboot a self machine.

  7. Write a Ansible commands to install a package called “git”, “wget”.

  8. Write a Ansible Adhoc commands to clone git repo. thttps://github.com/scmgalaxy/ansible-role-template

Tagged : / / / /

Puppet Module Lab & Excercise – Part 1

Puppet Module Lab & Excercise – Part 1

  1. Write a puppet module to setup a webserver(httpd) only in RHEL and use the HEREDOC to craete a html code of index.html inside a directory called “/var/www/html/index.html”

  2. Write a puppet module to setup a webserver(httpd) only in RHEL and use the puppet template to create a html code of index.html inside a directory called “/var/www/html/index.html”

  3. Write a puppet module to install git, ntp and wget tools.

  4. Write a puppet module which has file named with deploy.sh under module’s files directory. the file should be copied to /opt

Tagged : / / / /

Puppet Program Lab & Excercise – Part 2

Puppet Program Lab & Excercise – Part 1

  1. Write a puppet Program to install package “git”, “ntp” and wget in RHEL.

  2. Which resource declaration can help us to deploy the “~/.ssh/authorized_keys” in 1000s of servers?

  3. Write a puppet Program to extract a tar ball in /opt

  4. Write a puppet Program to zip a directory which has multiple files.

  5. Write a puppet Program to reboot the machine?

  6. Write a puppet program to execute a shell command/script using puppet.

Tagged : / / / /

Puppet Program Lab & Excercise – Part 1

Puppet Program Lab & Excercise – Part 1

  1. Write a Puppet Program to create a file named with “file.txt” in /opt/ owner by ec2-user and execute permission?
  2. Write a Puppet Program to create a group called “scmgalaxy-admins”
  3. Write a Puppet Program to create a usser called “deploy” which should be a part of “scmgalaxy-admins” group.
  4. Wrire a Puppet Program and insert following in /etc/hosts
    10.4.5.5 test.scmgalaxy.com
  5. Write a puppet program to display following lines when Sends an arbitrary message to the agent run-time log.
    Welcome to scmGalaxy Puppet program
Tagged : / / / /

Enforce the JIRA issue id in a GIT commit message

Enforce the JIRA issue id in a GIT commit message

This can be done by using git hooks file location at .git/hooks/commit-msg. Following are the 2 way in whcih each Developers can set the Hooks in their developement envioronment.

Method – 1

# commit-msg.sh
#!/bin/sh
# This hook will make sure that the commit message contains a JIRA issue.
#
# To enable this hook, rename this file to ".git/hooks/commit-msg".
# Make sure to add execution permissions to the file.

export MESSAGE=$(<$1)
export JIRA_ISSUE_TAG='ISSUETAG-([0-9]*)'

if [[ $MESSAGE =~ $JIRA_ISSUE_TAG ]]; then
echo -e "\e[32mGreat, your commit message contains a JIRA issue!\e[0m"
exit 0;
fi

echo -e "\e[31mOh hamburgers ... You forgot to add a JIRA issue number!\e[0m";
exit 1;

Method 2 – This is another very good example to implement to force jira id in each commit message. http://nsbogan.com/tools/2015/06/04/jira-id-in-git-commit-messages

Tagged : / / / /