-1

What specific changes need to be made to the code below so that the Ansible Playbook will know how to pull testFile.txt from ServerB into ServerA?

These are both Amazon Linux servers.

Note that ServerA and ServerB are both remote servers being acted upon by a central Ansible server.

A file created on ServerB by a Playbook from the central Ansible server needs to be copied/pulled directly into ServerA from ServerB at runtime instead of coming back to the Ansible Server.

Also note that the inventory for ServerA and ServerB (one IP for each) is defined on the central Ansible server, but not on the remote ServerA or the remote ServerB.

Therefore, at runtime, the playbook is throwing the following error when it gets to the line `` below:

fatal: [ip.of.server.b]: FAILED! => {
    "msg": "'ServerA' is undefined"
}  

So what specific changes need to be made to the below code in order for ServerA to be defined at runtime?

Here is the playbook:

# Then create a file on ServerB and copy/pull that file into ServerA
- name: File transfer example 
  hosts: ServerB
  remote_user: server-b-user
  become: true
  become_method: sudo
  become_user: root

  tasks:  

     - name: Create a test file to copy
       shell:
         cmd: |
           cat <<EOF > /path/on/ServerB/testFile.txt
           this is a test file.
           EOF
       sudo: true
       sudo_user: server-b-user

     - name: Transfer conf file from master to admin
       synchronize:
         src: /path/on/ServerB/testFile.txt
         dest: /path/on/ServerA/
         mode: pull
       delegate_to: "{{ ServerA }}"
  • 1
    Since you did not care for my answer explaining that ServerA was a variable that you had not defined, I'm at a loss how to resolve this. Given that you mentioned using the regular IP throws other errors (info that would be helpful in the question), I suggest you look for other modules that might copy the file. synchronize with pull seems a bit esoteric; is it necessary in your case for reasons we don't know? – 0xSheepdog Jun 5 at 0:29
  • @0xSheepdog A module that abstracts out the nuances is exactly the need. But my google research indicated that this is the module to use. So my guess is that it must have to do with the way that this module is configured. Maybe some arguments to add along with it. I figured someone on this site would already have experience with this. – CodeMed Jun 5 at 2:14
  • 1
    What indicates that synchronize is the module to use? Why not simply copy? "The copy module copies a file from the local or remote machine to a location on the remote machine." docs.ansible.com/ansible/latest/modules/copy_module.html – Panki Jun 5 at 6:40
  • "a module that abstracts the nuances" means nothing. You are trying to overcome a technical problem. If there are requirements and restrictions that factor into the situation, they should be described in the question. If you want to discuss the "best practices" maybe try the Ansible subreddit? – 0xSheepdog Jun 5 at 14:33
0

So what specific changes need to be made to the below code in order for ServerA to be defined at runtime?

- hosts: ServerB
  vars:
    ServerA: 192.168.0.0

You must address the error message indicating the variable ServerA is undefined.

Your code is calling variable ServerA. You have not defined the variable, and Ansible does not have a 'fact' registered to that name; therefore it is undefined and the controller doesn't know what you are referring to.

Ansible documentation shows that delegate_to: supports directly calling an IP address. If you want to use a variable to do so, you need to declare it yourself in the code, or use one of the dynamic inventory facts.

This may also be doable with the magic variable inventory_hostname to pull facts. YMMV.

  • This does not answer the question yet, because the OP asks how to load the inventory group names of the inventory into the Playbook. Simply pointing out that it may be necessary to populate a variable does not address the OP's scope of the specific case of the inventory group names mapped as the variables (retaining association with ips that are abstracted into the inventory group names. What is more, when I pasted the raw IP address for testing, there were other low level errors such as the standard warning against man-in-the-middle-attacks and then a rejection even on the second try. – CodeMed Jun 5 at 0:06
  • Copying from one remote server directly to another remote server is a common requirement for which there must be some best practice. I am asking for that best practice. I have read other postings about this general problem, but none of the other postings address this specific error. – CodeMed Jun 5 at 0:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.