Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

Ansible Tutorials: Ansible Variables in Playbook

  1. Inventories: You can define variables in your inventory files, either globally for all hosts or specific to particular groups or hosts.
  2. Host or Group Variables: Create separate files for host or group variables inside the host_vars and group_vars directories within your Ansible project.
  3. Playbook-level Variables: Declare variables at the top level of your playbook using the vars keyword.

Ansible Variable in Playbook with following ways:

  • Use variables in playbook directly
  • Use variables using external var files in playbook
  • Use variables using include_vars into task in playbook
  • Use variables using User Prompt in playbook
  • Use variables using outout of task using register

List of Way to declare variables in Playbook

  1. Playbook-level Variables: Declare variables at the top level of your playbook using the vars keyword.
  2. Task-level Variables: You can also define variables within specific tasks.
  3. Register Variables: You can capture the output of a task into a variable using the register keyword.
  4. Environment Variables: You can use environment variables by accessing them using Ansible’s ansible_env dictionary.
  5. Using Facts: Ansible gathers facts about managed hosts, and you can use these facts as variables in your playbooks.
  6. Prompting for User Input: You can use the vars_prompt keyword to prompt the user for input during playbook execution.

Declare variables at the top level of your playbook using the vars keyword.


- name: Example playbook
  hosts: all
  vars:
    variable_name: value
  tasks:
    # Your tasks here
Code language: PHP (php)

Task-level Variables:


- name: Example playbook
  hosts: all
  tasks:
    - name: Task with variables
      debug:
        msg: "My variable: {{ variable_name }}"
      vars:
        variable_name: value
Code language: JavaScript (javascript)

Register Variables:


- name: Example playbook
  hosts: all
  tasks:
    - name: Task with register
      shell: echo "Hello, world!"
      register: output
    - name: Display registered variable
      debug:
        var: output.stdout
Code language: JavaScript (javascript)

Environment Variables:


- name: Example playbook
  hosts: all
  tasks:
    - name: Display environment variable
      debug:
        var: ansible_env.VARIABLE_NAME
Code language: JavaScript (javascript)

Using Facts:


- name: Example playbook
  hosts: all
  tasks:
    - name: Display fact
      debug:
        var: ansible_facts['ansible_os_family']
Code language: JavaScript (javascript)

Prompting for User Input:


- name: Example playbook
  hosts: all
  vars_prompt:
    - name: variable_name
      prompt: "Enter a value for the variable:"
  tasks:
    - name: Display input variable
      debug:
        var: variable_name
Code language: JavaScript (javascript)

Ansible Playbook with Variable for Centos/RHEL


---
- name: Update web servers
  hosts: web
  vars:
    myname: "Rajeshkumar"
    age: "18"
    packagename: "httpd"
    servicename: "httpd"
  vars_files:
    - "vars.yaml"
  vars_prompt:
    - name: "version"
      prompt: "Which version Do you want to install?"
      private: no

  tasks:
  - name: Install Apache in centos7
    ansible.builtin.yum:
      name: "{{ packagename }}"
      state: latest
  - name: Copy index.html
    ansible.builtin.copy:
      src: index.html
      dest: /var/www/html/index.html
  - name: Starting a Apache Server
    ansible.builtin.service:
      name: "{{ servicename }}"
      state: started
  - name: Print return information from the previous task
    ansible.builtin.debug:
      var: myname
  - name: include default step variables
    include_vars: tasks_var.yaml
  - name: Print return information from the previous task
    ansible.builtin.debug:
      msg: "My Name is {{ myname }} and My age is {{ age }}"
  - name: Print return information from the previous task
    ansible.builtin.debug:
      var: version
  - name: Ansible register variable basic example
    shell: "find *.txt"
    args:
      chdir: "/home/azureuser/rajesh"
    register: find_output
  - debug:
      var: find_output
  - debug:
      var: find_output.stdout_lines
  - debug:
      var: find_output.stdout_lines[0]Code language: PHP (php)

Ansible Playbook with Variable for Ubuntu

---
- name: Update web servers
  hosts: web
  vars:
    myname: "Rajeshkumar"
    age: "18"
    packagename: "apache2"
    servicename: "apache2"
  vars_files:
    - "vars.yaml"
  vars_prompt:
    - name: "version"
      prompt: "Which version Do you want to install?"
      private: no

  tasks:
  - name: Install Apache in ubuntu
    ansible.builtin.apt:
      name: "{{ packagename }}"
      state: latest
  - name: Copy index.html
    ansible.builtin.copy:
      src: index.html
      dest: /var/www/html/index.html
  - name: Starting a Apache Server
    ansible.builtin.service:
      name: "{{ servicename }}"
      state: started
  - name: Print return information from the previous task
    ansible.builtin.debug:
      var: myname
  - name: include default step variables
    include_vars: tasks_var.yaml
  - name: Print return information from the previous task
    ansible.builtin.debug:
      msg: "My Name is {{ myname }} and My age is {{ age }}"
  - name: Print return information from the previous task
    ansible.builtin.debug:
      var: version
  - name: Ansible register variable basic example
    shell: "find *.txt"
    args:
      chdir: "/root/ansible"
    register: find_output
  - debug:
      var: find_output
  - debug:
      var: find_output.stdout_lines
  - debug:
      var: find_output.stdout_lines[0]Code language: PHP (php)

Ansible Varialble Precedence


Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

Top 10 Subscription Management Software Tools in 2026: Features, Pros, Cons & Comparison

Introduction Subscription management software is designed to streamline and optimize the process of managing recurring billing, customer subscriptions, and related business operations. In 2026, with the rapid…

Read More

Top 10 AI Data Integration Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, AI data integration tools are pivotal for businesses navigating the complexities of modern data ecosystems. These tools combine artificial intelligence with data integration processes…

Read More

Top 10 Fleet Management Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, the logistics and transportation industries are evolving rapidly, and managing a fleet of vehicles has never been more complex. Fleet management software has become…

Read More

Top 10 AI Academic Plagiarism Checkers Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, AI academic plagiarism checkers have become indispensable tools for students, educators, researchers, and institutions striving to uphold academic integrity. With the rise of AI-generated…

Read More

Top 10 Travel Management Software Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, travel management software (TMS) has become a crucial tool for businesses, travel agencies, and frequent travelers. These tools automate the booking, tracking, and management…

Read More

Top 10 No-Code Platforms Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, no-code platforms have become essential for businesses and individuals looking to build powerful applications, websites, and automations without the need for programming knowledge. These…

Read More