ansible read file as variable code example

Example: ansible use files contents to a variable

# 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') }}"

# SAME AS
- 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 username@server"