2

I write a simple playbook to copy some configuration files on a certain machine. I need to copy this file in a different host too for backup. Is possible to declare different host in the same playbook? I need this cause my "backup host" can be different and I retrieve it from the hostname I use. I tried both copy and raw module and nothing seems to work

here the example of playbook

  - name: find file
     find:
       file_type: directory
       paths: /prd/viv/dat/repository/
       patterns: "{{inventory_hostname}}"
       recurse: yes
     register: find
     delegate_to: localhost


   - name: Copy MASTER
     raw:  echo  xmonit$(echo {{find.files[0].path}} | cut -d "/" -f7 )
     delegate_to: localhost
     register: xmonit


   - debug:
       msg: "{{xmonit.stdout}}"


   - name: Copy MASTER raw
     raw:  sshpass -p "mypass" scp {{find.files[0].path}}/master.cfg  myuser@{{xmonit.stdout}}:/prd
     delegate_to: localhost


     #- name: Copy MASTER
       #copy:
         #src: "{{find.files[0].path}}/master.cfg"
         #dest: /prd/cnf/dat/{{inventory_hostname}}/

edit: if I use the copy module the destination remains that of the main host while the goal is to copy to a third host. I need to declare a different host for this single task

- name: Copy MASTER
  copy:
    src: "{{find.files[0].path}}/master.cfg"
    dest: /prd/cnf/dat/{{inventory_hostname}}/
6
  • 1) Never use raw unless for e.g. installing python on a target to later manage it with "normal" ansible modules 2) nothing seems to work is not an accurate description of your current problems. 3) What is wrong exactly with the copy module which seems the perfect candidate for this job ? - Please update your question with more precise info Commented Jan 13, 2020 at 10:31
  • I can use the modules on the third host but not on the machine declared by the inventory because they have an S.O. outdated which does not support an adequate python version i edited for better understanding Commented Jan 13, 2020 at 10:53
  • 1
    You are copying from localhost to some_other_host. If some_other_host is not be the current host in play, simply delegate_to: some_other_host. Commented Jan 13, 2020 at 11:01
  • acting like that i copy from host1 to "backup host". I'm trying to copy from localhost to "backup host" Commented Jan 13, 2020 at 11:18
  • Your latest assertion is wrong. copy module copies files from localhost (i.e. ansible controller) to the remote target (i.e. current host in play). If you delegate_to, it copies from localhost to the delegated target Commented Jan 13, 2020 at 11:25

1 Answer 1

1

Like Zeitounator told me in the comments copy module are the best way to act. like this it work for me

    - name: Copy MASTER
      copy:
        src: "{{find.files[0].path}}/master.cfg"
        dest: /prd/cnf/dat/{{inventory_hostname}}/
      delegate_to: xmonit.stdout_lines[0]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.