How to pass variable while running playbook on command line

Let's take an example of below playbook which has variables 
defined. You can see "vars" section of playbook. It has
multiple variables like pkg, myuser etc. Now user is looking
to change one variable during run time and rest can go same.
You need to just pass value if variable while running playbook
using option "-e".
# cat plabook-var.yml
# Playbooks for defining varibles
# vars should be defined before tasks

- name: Playbook for Learning Variables
  hosts: all
  vars:
    pkg: vsftpd
    myuser: testing
    mypass: testing
    webroot: /var/www/html
    myurl: https://www.w3schools.com/jsref/prop_option_index.asp

  tasks:
  - name: Install package
    yum:
      name: "{{ pkg }}"
      state: installed

  - name: Create User
    user:
        name: "{{ myuser }}"
        password: "{{ mypass | password_hash('sha512') }}"

  - name: Create folder
    file:
      path: "{{ webroot }}/vijay"
      state: directory


  - name: Download index file
    get_url:
      url: "{{ myurl }}"
      dest: /tmp



# ansible-playbook playbooks2/p1-varibale.yml -e pkg=httpd
You can also change multiple variable values as per your 
requirement.
# ansible-playbook playbooks2/p1-varibale.yml -e pkg=wget -e mypass=qwerty

Leave a Reply

Your email address will not be published. Required fields are marked *