ansibleansible-2.x

copy file and create symlink using ansible


On the source machine where i am running the playbook cmd i have a cert file and i have linked that with the hash value. Now i wanted to copy that to my client machine. What command should i use copy and preserve the symnlink.

Source machine

/home/ca.d
cacert.pem
e2223e235.0 -> cacert.pem

Destination source where i wanted to copy and link

/etc/syslog-ng/ca.d

I am using the below command to copy the cert but i am unable to find the cmmand to link it

- name: copy files
  copy: src=/home/ca.d/{{ item.src_name }} dest=/etc/syslog-ng/ca.d/{{ item.dest_name }}                               
  with_items:                                                                                   
    - { src_name: cacert.pem, dest_name: cacert.pem }   

Solution

  • To create a symlink in Ansible you use the file module with state: link argument. You won’t be able to do it in a single task, so you need to add another one after the copy:

    - file:
        src: /etc/syslog-ng/ca.d/cacert.pem
        dest: /etc/syslog-ng/ca.d/e2223e235.0
        state: link
    

    Your data doesn’t specify how you get the symlink name, so the above example uses hardcoded values, replace them accordingly.