我正在尝试将dist目录的内容复制到nginx目录。
我写下一个:
- name: copy html file
copy: src=/home/vagrant/dist/ dest=/usr/share/nginx/html/ remote_src=yes directory_mode=yes
但是当我执行剧本时,它会抛出一个错误:
TASK [NGINX : copy html file] **************************************************
fatal: [172.16.8.200]: FAILED! => {"changed": false, "failed": true, "msg": "attempted to take checksum of directory:/home/vagrant/dist/"}
复制目录有错误吗?如何复制另一个目录和文件中的目录内容?
有任何帮助吗? 感谢
答案 0 :(得分:30)
您可以使用synchronize模块。文档中的示例:
# Synchronize two directories on one remote host.
- synchronize:
src: /first/absolute/path
dest: /second/absolute/path
delegate_to: "{{ inventory_hostname }}"
这具有额外的好处,即对于大/多个文件来说它会更有效。
答案 1 :(得分:24)
已解决的答案: 要将目录的内容复制到另一个目录,我使用下一个目录:
$AuthorTitle = mysqli_real_escape_string($con, ($_POST['AuthorTitle']));
$AuthorTitle = mysqli_real_escape_string($con, ($_POST['AuthorTitle_ID2']));
$F_Name = mysqli_real_escape_string($con, ($_POST['first_name']));
$F_Name = mysqli_real_escape_string($con, ($_POST['first_name_ID2']));
$L_Name = mysqli_real_escape_string($con, ($_POST['last_name']));
$L_Name = mysqli_real_escape_string($con, ($_POST['last_name_ID2']));
$Email = mysqli_real_escape_string($con, ($_POST['email']));
$Email = mysqli_real_escape_string($con, ($_POST['email_ID2']));
$Contactauthor = mysqli_real_escape_string($con, ($_POST['contact']));
$Contactauthor = mysqli_real_escape_string($con, ($_POST['contact_ID2']));
$Title = mysqli_real_escape_string($con, ($_POST['Title']));
$Summary = mysqli_real_escape_string($con, ($_POST['Summary']));
mysqli_multi_query($con,"
START TRANSACTION;
INSERT INTO author(AuthorTitle, F_Name, L_Name, Email, Contact)
VALUES('$AuthorTitle','$F_Name','$L_Name','$Email','$Contactauthor'), ('$AuthorTitle_ID2','$F_Name_ID2','$L_Name_ID2','$Email_ID2','$Contactauthor_ID2');
SET @AuthorId = LAST_INSERT_ID();
INSERT INTO study(Title, Summary) VALUES('$Title','$Summary');
SET @StudyId = LAST_INSERT_ID();
INSERT INTO casestudy(AuthorId, StudyId, Submitted) VALUES(@AuthorId, @StudyId, NOW());
COMMIT;
");
它将两个项目复制到另一个目录。在该示例中,其中一个项目是目录而另一个项目不是。它运作得很好。
答案 2 :(得分:14)
Ansible Copy模块将文件/目录从控制计算机复制到远程计算机。如果要在远程计算机中复制文件/目录,并且如果您有 Ansible 2.0 ,请将remote_src
设置为yes
- name: copy html file
copy: src=/home/vagrant/dist/ dest=/usr/share/nginx/html/ remote_src=yes directory_mode=yes
答案 3 :(得分:7)
我发现复制文件夹的内容而不复制文件夹本身的最简单的解决方案是使用以下内容:
- name: Move directory contents
command: cp -r /<source_path>/. /<dest_path>/
这解决了@ surfer190的后续问题:
嗯,如果要复制整个内容怎么办?我注意到*不起作用 - surfer190 2016年7月23日7:29
*
是一个shell glob,因为在运行cp
之前它依赖你的shell来枚举文件夹中的所有文件,而.
直接指示cp
获取目录内容(参见https://askubuntu.com/questions/86822/how-can-i-copy-the-contents-of-a-folder-to-another-folder-in-a-different-directo)
答案 4 :(得分:2)
我也参与了整整一天!最后在shell
命令中找到了解决方案而不是 copy:或命令:,如下所示:
- hosts: remote-server-name
gather_facts: no
vars:
src_path: "/path/to/source/"
des_path: "/path/to/dest/"
tasks:
- name: Ansible copy files remote to remote
shell: 'cp -r {{ src_path }}/. {{ des_path }}'
严格注意:
1. src_path和des_path以/
符号结尾
2.在shell命令中,src_path以.
结尾,显示目录的所有内容
3.我在主机中使用了 remote-server-name 并执行shell
jenkins的一部分,而不是playbook中的remote_src:
说明符。
我想在jenkins的执行Shell部分中运行以下命令是一个很好的建议:
ansible-playbook copy-payment.yml -i remote-server-name
答案 5 :(得分:2)
要将目录的内容复制到另一个目录,可以使用ansibles copy
模块:
- name: Copy content of directory 'files'
copy:
src: files/ # note the '/' <-- !!!
dest: /tmp/files/
从docs about the src
parameter:
如果(src!)路径是目录,则将其以递归方式复制...
... 如果路径以“ /”结尾,则只会将该目录的内部内容复制到目标位置。
... 如果不以“ /”结尾,则目录本身将被复制。
答案 6 :(得分:1)
我找到了从远程到远程递归复制的解决方法:
- name: List files in /usr/share/easy-rsa
find:
path: /usr/share/easy-rsa
recurse: yes
file_type: any
register: find_result
- name: Create the directories
file:
path: "{{ item.path | regex_replace('/usr/share/easy-rsa','/etc/easy-rsa') }}"
state: directory
mode: "{{ item.mode }}"
with_items:
- "{{ find_result.files }}"
when:
- item.isdir
- name: Copy the files
copy:
src: "{{ item.path }}"
dest: "{{ item.path | regex_replace('/usr/share/easy-rsa','/etc/easy-rsa') }}"
remote_src: yes
mode: "{{ item.mode }}"
with_items:
- "{{ find_result.files }}"
when:
- item.isdir == False
答案 7 :(得分:1)
对于参数src,Ansible文档非常清楚https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html,它表示以下内容:
Local path to a file to copy to the remote server.
This can be absolute or relative.
If path is a directory, it is copied recursively. In this case, if path ends with "/",
only inside contents of that directory are copied to destination. Otherwise, if it
does not end with "/", the directory itself with all contents is copied. This behavior
is similar to the rsync command line tool.
因此,您需要跳过src路径末尾的/。
- name: copy html file
copy: src=/home/vagrant/dist dest=/usr/share/nginx/html/
答案 8 :(得分:0)
以下为我工作,
-name: Upload html app directory to Deployment host
copy: src=/var/lib/jenkins/workspace/Demoapp/html dest=/var/www/ directory_mode=yes
答案 9 :(得分:0)
我找到了将文件从Ansible服务器复制到远程服务器的理想解决方案。
复制yaml文件
- hosts: localhost
user: {{ user }}
connection: ssh
become: yes
gather_facts: no
tasks:
- name: Creation of directory on remote server
file:
path: /var/lib/jenkins/.aws
state: directory
mode: 0755
register: result
- debug:
var: result
- name: get file names to copy
command: "find conf/.aws -type f"
register: files_to_copy
- name: copy files
copy:
src: "{{ item }}"
dest: "/var/lib/jenkins/.aws"
owner: {{ user }}
group: {{ group }}
remote_src: True
mode: 0644
with_items:
- "{{ files_to_copy.stdout_lines }}"
答案 10 :(得分:0)
如何将目录和子目录以及文件从Ansible服务器复制到远程主机
- name: copy nmonchart39 directory to {{ inventory_hostname }}
copy:
src: /home/ansib.usr.srv/automation/monitoring/nmonchart39
dest: /var/nmon/data
Where:
copy entire directory: src: /automation/monitoring/nmonchart39
copy directory contents src: nmonchart39/