Shashikant shah

Saturday 17 October 2020

What is Variables and Debug part-4

 Ansible variables and Debug  

Variables :-
In playbooks, the variable is very similar to using the variables in a programming language. It helps you to assign a value to a variable and use it anywhere in the playbook.
 
1.Valid variable names in Ansible
package_name
package_name2
user_input_package
Package
2.Invalid variable names in Ansible
mysql version (multiple words)
mysql.port (a dot)
5 (a number)
user-input (a hyphen)
 
1.define with the file playbook directly.
2.passsing from external files.
3.passing from hosts inventory.
4.using group_vars or hosts_vars and so on.
 
1.    Direct variable
# direct.yml
---
- name: direct varable
  hosts: jenkins
  become: true
  vars:
    - tomcat_url: http://tomcat.com
  tasks:
  - name: install tomcat
    debug:
      msg: "{{ tomcat_url }}"
 
2.    passing from external files.
i)Create variable file.
 
# vim var_file_web
var1: this is a first web
var2: this is second web
 
ii) ext_var.yml
---
- name: ext file varable
  hosts: jenkins
  become: true
  vars_files:
    - var_file_web
  tasks:
  - name: install tomcat
    debug:
      msg: "{{ var1 }}"
 
3.    passing from hosts inventory.
 
Jenkins group use to varable.
# vim hosts
[jenkins]
192.168.1.10
[jenkins:vars]
web_url=http://localhost
port=8080
# vi Jenkins_file.yml
- hosts: jenkins
  tasks:
    - name: Print the value of global fact 'web_url'
      debug:
        msg: 'Web URL: {{web_url}}'
What is debug module :-
This module prints statements during execution and can be useful for debugging variables or expressions without necessarily halting the playbook.
Debug module accepts three parameters.
1.    msg
2.    var
3.    verbosity   -- for skip task
 
# msgfile.yml
-       name: This is a simple play  
hosts: localhost
          tasks:
             - name: Print the msg
                 debug:
                     msg:
                          - “welcome to ansible”
                          - “these are basic conepts”
# ansible-play  msgfile.yml  --syntax-check
 
# var.yml
---
- name: Debug Example Uptime
  hosts: localhost
  connection: local
 
  tasks:
   - name: Find Uptime
     shell: /usr/bin/uptime
     register: result
   - name: Print debug message
     debug:
       var: result.stdout







# vi verbosity.yml

---

- name: Debug Example Uptime

  hosts: localhost

  connection: local

  tasks:

   - name: Find Uptime

     shell: /usr/bin/uptime

     register: result

   - name: Print debug message

     debug:

           msg: "{{ result.stdout}}”

          verbosity: 2



debug looping :-

- hosts: all

  gather_facts: true

  tasks:

    - name: Ansible fact for mac address

      debug:

        msg: "{{ item.name }} is {{ item.ram }}"

      with_items:

         - name: server1

           disks: 3gb

           ram: 15Gb

         - name: server2

           disks: 4gb

           ram: 20Gb

      loop_control:

       label: "{{ item.name }}" 












No comments:

Post a Comment