Example 1: Lookups and variables
vars:
motd_value: "{{ lookup('file', '/etc/motd') }}"
tasks:
- debug:
msg: "motd value is {{ motd_value }}"
Example 2: Lookups and loops items
Lookups are an integral part of loops. Wherever you see with_, the part after the underscore is the name of a lookup. This is also the reason most lookups output lists and take lists as input; for example, with_items uses the items lookup:
- name: count to 3
debug: msg={{item}}
with_items: [1, 2, 3]
Example 3: Lookups and file The following example, you could notice that the task1 and task2 are doing the exact same job of copying the public key from local and adding to the authorized_key on the remote server to enable SSH Key based authentication.
---
- name: Ansible Lookup examples
hosts: app
become: true
tasks:
# BOTH TASKS ARE EXACLY DOING THE SAME JOB
# IN TASK1: WE ARE KEEPING THE PUBLIC KEY AS A TEXT IN PLAYBOOK
- name: "Copy the public Key Using the Key directly within Playbook"
authorized_key:
user: vagrant
state: present
key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCmdlM0QV6HxZQ7iqXiboefzMHR/cKX+qlTezRiExW6jmwNaD1a9F3KlMaoi4eTKGtPbM9eTygcBjJOGZhEjZITuNat7teK/evvbiszrT3ORUvotnv8yjVC02CGFsK6fPs10J0rgITPdsnd+oq9WcJ/2rM5wHJPoSfhUzhgDB7mdOIeVM+mG89j+OPV377HRTyC5O9Ja9nX9J5ElHXFWu2CTLMjgYxZ16FLpIdlrL4I12mCucZ8jGaZp8frarwyilHsuUt2hQFi3XEmT3ACKiAtE0kBhclr2gtc2wNoVJVoWB sarav@Saravs-MacBook-Pro.local"
# IN TASK2: WE ARE READING THE PUBLIC KEY FROM THE FILE DIRECTLY USING LOOKUP
- name: "Copy the public Key using Lookup"
authorized_key:
user: vagrant
state: present
key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
# ANSIBLE FILE LOOKUP PLUGIN HELPS TO READ THE FILE CONTENTS WITHIN THE ANSIBLE PLAYBOOK
Example 4: Lookups and env
- debug: msg="{{ lookup('env','HOME') }} is an environment variable"
Example 5: Lookups and ini The ini lookup reads the contents of a file in INI format key1=value1. This plugin retrieves the value on the right side after the equal sign '=' of a given section [section].
- debug: msg="User in integration is {{ lookup('ini', 'user section=integration file=users.ini') }}"
- debug: msg="User in production is {{ lookup('ini', 'user section=production file=users.ini') }}"
**Example 6: Lookups and url ** Returns the content of the URL requested to be used as data in play.
- name: url lookup splits lines by default
debug: msg="{{item}}"
loop: "{{ lookup('url', 'https://github.com/gremlin.keys', wantlist=True) }}"
**Example 7: Lookups and varnames ** Retrieves a list of matching Ansible variable names.
- name: List variables that start with qz_
debug: msg="{{ lookup('varnames', '^qz_.+')}}"
vars:
qz_1: hello
qz_2: world
qa_1: "I won't show"
qz_: "I won't show either"
- name: Show all variables
debug: msg="{{ lookup('varnames', '.+')}}"
- name: Show variables with 'hosts' in their names
debug: msg="{{ lookup('varnames', 'hosts')}}"
- name: Find several related variables that end specific way
debug: msg="{{ lookup('varnames', '.+_zone$', '.+_location$') }}"
**Example 8: Lookups and vars ** Retrieves the value of an Ansible variable.
- name: Show value of 'variablename'
debug: msg="{{ lookup('vars', 'variabl' + myvar)}}"
vars:
variablename: hello
myvar: ename
- name: Show default empty since i dont have 'variablnotename'
debug: msg="{{ lookup('vars', 'variabl' + myvar, default='')}}"
vars:
variablename: hello
myvar: notename