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 : / / / /