Ansible Variable example

DevOps

YOUR COSMETIC CARE STARTS HERE

Find the Best Cosmetic Hospitals

Trusted • Curated • Easy

Looking for the right place for a cosmetic procedure? Explore top cosmetic hospitals in one place and choose with confidence.

“Small steps lead to big changes — today is a perfect day to begin.”

Explore Cosmetic Hospitals Compare hospitals, services & options quickly.

✓ Shortlist providers • ✓ Review options • ✓ Take the next step with confidence


<strong>Declare variables with vars:</strong>

- name: To create group deploy
  hosts: web
  vars:
    groupname: deploy    
    firstname: Bhavya
    lastname: sharma

  tasks:
  - name: to create group deploy
    ansible.builtin.group:
      name={{ groupname }}
      state=present
Code language: HTML, XML (xml)

<strong>Declare variables in external file:</strong>

- name: To create group deploy
  hosts: web
  vars_files:
    - vars-file.yaml

  tasks:
  - name: to create group deploy
    ansible.builtin.group:
      name={{ groupname }}
      state=present

<strong>vars-file.yaml</strong>
groupname: deploy    
firstname: Bhavya
lastname: sharma
Code language: HTML, XML (xml)

<strong>Declare variables in include file:</strong>

- name: To create group deploy
  hosts: web
  vars_files:
    - vars-file.yaml

  tasks:
  - name: to create group deploy
    ansible.builtin.group:
      name={{ groupname }}
      state=present
  - name: include default step variables
    include_vars: var-include.yml
  - name: Print the variable value
    ansible.builtin.debug:
      msg: welcome {{ firstname }} {{ lastname }}

<strong>vars-file.yaml</strong>
groupname: deploy    
firstname: Bhavya
lastname: sharma

* scope is higher if you define in include file
* value of variable will be value defined in include file
Code language: PHP (php)

<strong>Declare variables using prompt:</strong>

- name: To create group deploy
  hosts: web
  vars_files:
    - vars-file.yaml
  vars_prompt:
    - name: "myname"
      prompt: "enter your name?"
      private: false

  tasks:
  - name: to create group deploy
    ansible.builtin.group:
      name={{ groupname }}
      state=present
  - name: include default step variables
    include_vars: var-include.yml
  - name: Print the vars
    ansible.builtin.debug:
      msg: Enter your name{{ myname }}

<strong>vars-file.yaml</strong>
groupname: deploy    
firstname: Bhavya
lastname: sharma

* <strong>scope is higher if you pass from prompt</strong>
Code language: PHP (php)

<strong>Declare variables command line</strong>	<strong>:</strong>
cmd: <strong>ansible-playbook -i inventory creategroup.yaml -e firstname=bhavya -e lastname=sharma</strong>

<creategroup.yaml>

- name: To create group deploy
  hosts: web
  vars_files:
    - vars-file.yaml
  vars_prompt:
    - name: "myname"
      prompt: "enter your name?"
      private: false

  tasks:
  - name: to create group deploy
    ansible.builtin.group:
      name={{ groupname }}
      state=present
  - name: include default step variables
    include_vars: var-include.yml
  - name: Print the vars
    ansible.builtin.debug:
      msg: Enter your name{{ myname }}

<strong>vars-file.yaml</strong>
groupname: deploy    
firstname: Bhavya
lastname: sharmaCode language: PHP (php)