Introduction
Quick post; how to configure Ansible playbook to pass command line arguments to variable in the script.
Configuration
Sometimes it is required to create a playbook with dynamic variables, so its structure stays the same, but values are changing. To do this, Ansible created a special function, called prompt which takes input from the user. Here’s the playbook:
---
- hosts: localhost
vars_prompt:
- name: name
prompt: RG name?
private: no
connection: local
tasks:
- name: Create resource group
azure_rm_resourcegroup:
name: '{{ name }}'
location: uksouth
register: rg
- debug:
var: rg
Let’s execute the playbook and test:
[root@centos ansible]# ansible-playbook playbooks/rg.yaml RG name?: prompt_resource_group
PLAY [localhost]
TASK [Gathering Facts]
ok: [localhost]
TASK [Create resource group] **
changed: [localhost]
TASK [debug] ****
ok: [localhost] => {
"rg": {
"changed": true,
"contains_resources": false,
"failed": false,
"state": {
"id": "/subscriptions/6e7488b3-0cbf-45dc-a934-41383eaafc4a/resourceGroups/prompt_resource_group",
"location": "uksouth",
"name": "prompt_resource_group",
"provisioning_state": "Succeeded",
"tags": null
}
}
}
PLAY RECAP
localhost : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0