I am running an ansible playbook inside a terraform local-exec
provisioner with inline inventory of the remote instance IP.
- name: Install git
apt:
name: git
state: present
update_cache: yes
- name: Clone the git repository
become_user: "{{ SSH_USER }}"
git:
repo: "{{ REPO_URL }}"
dest: "{{ SRC_DIR }}"
- name : Find files with .pub extension
become_user: "{{ SSH_USER }}"
find:
paths: "{{ SRC_DIR }}"
patterns: '*.pub'
register: pub_files
- name: Append the content of all public key files to authorized_keys file.
become_user: "{{ SSH_USER }}"
lineinfile:
path: "{{ DEST_FILE }}"
line: "{{ lookup('file', '{{ item.path }}') }}"
insertafter: EOF
create: "yes"
state: present
# loop: "{{ lookup('fileglob', "{{ SRC_DIR }}/*.pub", wantlist=True) }}"
# with_fileglob: "{{ SRC_DIR }}/*.pub"
with_items: "{{ pub_files.files }}"
- name: Display destinationFile contents
become_user: "{{ SSH_USER }}"
command: cat "{{ DEST_FILE }}"
register: command_output
- name: Print to console
become_user: "{{ SSH_USER }}"
debug:
msg: "{{command_output.stdout}}"
The ansible playbook should clone a git repo and copies the content of it's files to another file.
But when using ansible lookups to read the content of the files (which are cloned in remote host), it always looks for the file in localhost.
Like all templating, lookups execute and are evaluated on the Ansible
control machine.
Thus the above given playbook fails with error:
No such file or directory found
The similar issue occurred when used with_fileglob
and loop
with fileglob lookup to iterate over the files, as they also does a lookup inside. I replaced that with find
module to list files names, register
it in a variable and then iterate over it in next step using with_items
.
Is there any such alternative to read content of files?
question from:
https://stackoverflow.com/questions/65952847/alternative-for-ansible-lookups-as-it-always-looks-up-files-on-localhost 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…