ansible 查找的替代方法,因为它总是在本地主机上查找文件
Alternative for ansible lookups as it always looks up files on localhost
我是 运行 terraform local-exec
provisioner 中的一个 ansible playbook,具有远程实例 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}}"
ansible 剧本应该克隆一个 git 存储库并将其文件的内容复制到另一个文件。
但是当使用 ansible lookups 读取文件的内容(在远程主机中克隆)时,它总是在本地主机中查找文件。
Like all templating, lookups execute and are evaluated on the Ansible
control machine.
因此上面给定的剧本失败并出现错误:
No such file or directory found
将 with_fileglob
和 loop
与 fileglob 查找一起使用以遍历文件时会出现类似的问题,因为它们也在内部进行查找。我将其替换为 find
模块以列出文件名,register
它在一个变量中,然后在下一步中使用 with_items
.
对其进行迭代
有没有这样的替代方法来读取文件内容?
首先将它们取回 ansible 控制节点。请注意,ansible 有一个 authorized_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
它像我使用 cat
那样工作。
- name: Install git
become_user: root
apt:
name: git
state: present
update_cache: yes
- name: Clone the git repository
git:
repo: "{{ REPO_URL }}"
dest: "{{ SRC_DIR }}"
- name : Find file names with .pub extension
find:
paths: "{{ SRC_DIR }}"
patterns: '*.pub'
register: pub_files
- name: Get contents of all those .pub files
shell: cat {{ item.path }}
register: file_content
with_items: "{{ pub_files.files }}"
- name: Print file_content to console
debug:
var: item.stdout
with_items:
- "{{ file_content.results }}"
- name: Append the content of all public key files to authorized_keys file.
lineinfile:
path: "{{ DEST_FILE }}"
line: "{{ item.stdout }}"
insertafter: EOF
create: "yes"
state: present
with_items:
- "{{ file_content.results }}"
- name: Display destinationFile contents
command: cat "{{ DEST_FILE }}"
register: command_output
- name: Print to console
debug:
msg: "{{command_output.stdout}}"
我是 运行 terraform local-exec
provisioner 中的一个 ansible playbook,具有远程实例 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}}"
ansible 剧本应该克隆一个 git 存储库并将其文件的内容复制到另一个文件。 但是当使用 ansible lookups 读取文件的内容(在远程主机中克隆)时,它总是在本地主机中查找文件。
Like all templating, lookups execute and are evaluated on the Ansible control machine.
因此上面给定的剧本失败并出现错误:
No such file or directory found
将 with_fileglob
和 loop
与 fileglob 查找一起使用以遍历文件时会出现类似的问题,因为它们也在内部进行查找。我将其替换为 find
模块以列出文件名,register
它在一个变量中,然后在下一步中使用 with_items
.
有没有这样的替代方法来读取文件内容?
首先将它们取回 ansible 控制节点。请注意,ansible 有一个 authorized_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
它像我使用 cat
那样工作。
- name: Install git
become_user: root
apt:
name: git
state: present
update_cache: yes
- name: Clone the git repository
git:
repo: "{{ REPO_URL }}"
dest: "{{ SRC_DIR }}"
- name : Find file names with .pub extension
find:
paths: "{{ SRC_DIR }}"
patterns: '*.pub'
register: pub_files
- name: Get contents of all those .pub files
shell: cat {{ item.path }}
register: file_content
with_items: "{{ pub_files.files }}"
- name: Print file_content to console
debug:
var: item.stdout
with_items:
- "{{ file_content.results }}"
- name: Append the content of all public key files to authorized_keys file.
lineinfile:
path: "{{ DEST_FILE }}"
line: "{{ item.stdout }}"
insertafter: EOF
create: "yes"
state: present
with_items:
- "{{ file_content.results }}"
- name: Display destinationFile contents
command: cat "{{ DEST_FILE }}"
register: command_output
- name: Print to console
debug:
msg: "{{command_output.stdout}}"