Table of Contents
Sometimes it is better to be honest with people who read my blog. As you look there is an huge title that says what article is about. Sometimes even I didn’t invent to passing variables to Ansible, I wrote like that pages.
Main reason why i wrote like that pages, there are not enough examples about topic so I spend my precious time to make the world better again.☮ 1
TLDR
All examples are at below. Copy and Paste them wisely.
Passing variable to Ansible
1 2 3 4 |
ansible-playbook playbook_passing_variable -e / "my_plain_variable=ImPlain" or ansible-playbook playbook_passing_variable -e '{"my_plain_variable":"ImPlain"}' |
Passing list to Ansible
1 |
ansible-playbook playbook_passing_variable -e '{ "my_list":[1,2,3] }' |
Passing dictionary to Ansible
1 |
ansible-playbook playbook_passing_variable -e '{"my_plain_variable":"ImPlain","my_list":[1,2,3], "my_dictionary":{ "key1": "value1", "key2":"value2" }}' |
Pass more variables
1 |
ansible-playbook playbook_passing_variable -e '{"my_plain_variable":"ImPlain","my_list":[1,2,3], "my_dictionary":{ "key1": "value1", "key2":"value2" }}'<code class="src src-s"> |
Example playbook
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
--- - hosts: localhost gather_facts: no tasks: - name: Pass plain variable to ansible debug: msg: "{{ my_plain_variable }}" - name: Pass list to ansible debug: msg: "{{ my_list }}" - name: Pass dictionary to ansible debug: msg: "{{ my_dictionary }}" |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
PLAY [localhost] TASK [Show plain] ok: [localhost] => { "msg": "ImPlain"f } TASK [Pass list to ansible] ok: [localhost] => { "msg": [ 1, 2, 3 ] } TASK [Pass dictionary to ansible] ok: [localhost] => { "msg": { "key1": "value1", "key2": "value2" } } |