Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

ubuntu - Alternative for ansible lookups as it always looks up files on localhost

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Fetching them back to the ansible control node first works. And note that ansible has an authorized_keys module that simplifies the task of adding the keys.

  tasks:
  - name: find all the .pub files
    find: 
      paths: "/path/remote"
      recurse: no
      patterns: "*.pub"
    register: files_to_fetch
  - debug:
      var: files_to_fetch.files

  - name: "fetch .pub files from remote host"
    fetch: 
      flat: yes
      src:  "{{ item.path }}"
      dest: ./local/
    with_items: "{{ files_to_fetch.files }}"

  - name: update SSH keys
    authorized_key:
     user: user1
     key: "{{ lookup('file', item) }}"
     state: present
     #exclusive: yes
    with_fileglob:
      - local/*.pub


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...