# Ansible.builtin.copy not doing what I expect, what am I doing wrong?

**URL:** <https://forum.ansible.com/t/ansible-builtin-copy-not-doing-what-i-expect-what-am-i-doing-wrong/4272>\
**Category:** Get Help\
**Tags:** playbook, ubuntu\
**Created:** [March 8, 2024, 2:50pm UTC](https://forum.ansible.com/t/ansible-builtin-copy-not-doing-what-i-expect-what-am-i-doing-wrong/4272 "2024-03-08T14:50:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nesbit7](https://avatars.discourse-cdn.com/v4/letter/n/53a042/32.png) [@Nesbit7](https://forum.ansible.com/u/Nesbit7)\
**Post date:** [March 8, 2024, 2:50pm UTC](https://forum.ansible.com/t/ansible-builtin-copy-not-doing-what-i-expect-what-am-i-doing-wrong/4272/1 "2024-03-08T14:50:01Z")

</div>

I have tried to configure a simple task to:-  
1/ install postfix  
2/ save a copy of the default configuration file as ‘asinstalled’  
3/ download a preconfigured configuration file from our configuration server.

what I am seeing is the downloaded configuration file is being saved as the asinstalled, and the original configuration file is remaining untouched, i.e remains as postfix/main.cf.

ansible version installed is: ansible-7.7.0-1.el9.noarch

I have tried splitting the stanzas into separate playbooks, but the end result is the same whatever.

I have separate sections for RedHat/Ubuntu as we use a monolithic Linux playbook.

Here are the postfix install/confugre sections of my playbooks:-

```auto
   - name: Install the latest version of postfix on RedHat
     ansible.builtin.yum:
       name: postfix
       state: latest
     when: ansible_distribution == 'RedHat'

   - name: Install the latest version of postfix on Ubuntu
     ansible.builtin.apt:
       name: postfix
       state: latest
     when: ansible_distribution == 'Ubuntu'

   - name: ansible copy main.cf to asinstalled RedHat
     ansible.builtin.copy:
       src: /etc/postfix/main.cf
       dest: /etc/postfix/main.cf.asinstalled
       force: false
     when: ansible_distribution == 'RedHat'

   - name: ansible copy main.cf.debian to postfix Ubuntu
     ansible.builtin.copy:
       src: /usr/share/postfix/main.cf.debian
       dest: /etc/postfix/main.cf.asinstalled
       force: false
       remote_src: true
     when: ansible_distribution == 'Ubuntu'

   - name: ansible copy master.cf to asinstalled
     ansible.builtin.copy:
       src: /etc/postfix/master.cf
       dest: /etc/postfix/master.cf.asinstalled
       force: false

   - name: Download main.cf RedHat
     ansible.builtin.get_url:
       url: http://servername.domain/ks/etc/postfix/main.cf.redhat8
       dest: /etc/postfix/main.cf
       validate_certs: false
     when: ansible_distribution == 'RedHat'

   - name: Download main.cf Ubuntu
     ansible.builtin.get_url:
       url: http://servername.domain/ks/etc/postfix/main.cf.ubuntu22
       dest: /etc/postfix/main.cf
       validate_certs: false
     when: ansible_distribution == 'Ubuntu'

```

where am I going wrong ?

thanks in advance…

---

<div class="post-metadata">

**Author:** ![kpfleming](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kpfleming](https://forum.ansible.com/u/kpfleming)\
**Post date:** [March 8, 2024, 2:59pm UTC](https://forum.ansible.com/t/ansible-builtin-copy-not-doing-what-i-expect-what-am-i-doing-wrong/4272/2 "2024-03-08T14:59:32Z")

</div>

Copies of files on the remote system need to have `remote_src: true`, otherwise `ansible.builtin.copy` assumes the source file is on the Ansible controller.

---

<div class="post-metadata">

**Author:** ![Nesbit7](https://avatars.discourse-cdn.com/v4/letter/n/53a042/32.png) [@Nesbit7](https://forum.ansible.com/u/Nesbit7)\
**Post date:** [March 8, 2024, 4:57pm UTC](https://forum.ansible.com/t/ansible-builtin-copy-not-doing-what-i-expect-what-am-i-doing-wrong/4272/3 "2024-03-08T16:57:25Z")

</div>

thanks, Kevin, that’s got me further, the main.cf.asinstalled is now being created as desired.

however the file copy from configuration server now isn’t working, the task reports ‘ok:’ but isn’t downloading.

---

<div class="post-metadata">

**Author:** ![kpfleming](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kpfleming](https://forum.ansible.com/u/kpfleming)\
**Post date:** [March 8, 2024, 6:47pm UTC](https://forum.ansible.com/t/ansible-builtin-copy-not-doing-what-i-expect-what-am-i-doing-wrong/4272/4 "2024-03-08T18:47:24Z")

</div>

I don’t see any ‘configuration server’ in the snippet you pasted; do you mean the Ansible controller (where the playbook is being executed)?

---

<div class="post-metadata">

**Author:** ![Denney-tech](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/denney-tech/32/1556_2.png) [@Denney-tech](https://forum.ansible.com/u/Denney-tech)\
**Post date:** [March 8, 2024, 7:05pm UTC](https://forum.ansible.com/t/ansible-builtin-copy-not-doing-what-i-expect-what-am-i-doing-wrong/4272/5 "2024-03-08T19:05:06Z")

</div>

From what it looks like you’re trying to do, you want to backup the original config files “as installed” by the postfix package. Then you want to download your own config file from a server and use that instead.

If that’s the case, you appear to have some typos in some of your sources. Not to mention that you would overwrite the originally installed main.cf.asinstalled file with your modified main.cf on subsequent runs.

That said, I would try this more agnostic approach:

```yaml
# Use 'state: latest' for patching playbooks
# Use 'state: present' for configuration playbooks
  - name: Install the postfix
    ansible.builtin.package: # use this when package names are the same across distros
      name: postfix
      state: present

   - name: Backup original main.cf
     ansible.builtin.command:
       cmd: cp /etc/postfix/main.cf /etc/postfix/main.cf.asinstalled
       # cmd: cp /etc/postfix/main.cf /etc/postfix/main.cf.orig # < this is my preference for backing up original config files
       creates: /etc/postfix/main.cf.asinstalled # only do this if the backup doesn't exist

# Use ansible vars in the url to dynamically grab the right file for the OS
# Enforcing permissions on modified files is also good practice
   - name: Download main.cf
     ansible.builtin.get_url:
       url: "http://servername.domain/ks/etc/postfix/main.cf.{{ ansible_distribution | lower }}{{ ansible_distribution_major_version }}"
       dest: /etc/postfix/main.cf
       validate_certs: false
       mode: "644"
       owner: root
       group: root

```

Similarly, you could also use a jinja2 template file of main.cf and make it work for both RedHat/Ubuntu hosts, and copy that from your control node instead of downloading it from a server.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [March 8, 2024, 7:06pm UTC](https://forum.ansible.com/t/ansible-builtin-copy-not-doing-what-i-expect-what-am-i-doing-wrong/4272/6 "2024-03-08T19:06:10Z")

</div>

> [@Nesbit7](#):
>
> the task reports ‘ok:’ but isn’t downloading

That would indicate the `dest:` already contains the correct content. You’ll have to provide more information to convince me otherwise. I’m easily swayed, though.🙂

---

<div class="post-metadata">

**Author:** ![Nesbit7](https://avatars.discourse-cdn.com/v4/letter/n/53a042/32.png) [@Nesbit7](https://forum.ansible.com/u/Nesbit7)\
**Post date:** [March 15, 2024, 3:11pm UTC](https://forum.ansible.com/t/ansible-builtin-copy-not-doing-what-i-expect-what-am-i-doing-wrong/4272/7 "2024-03-15T15:11:13Z")

</div>

Kevin - the configuration server is denoted by ‘servername.domain’ in my code sample.

Denney-tech - great, I have confirmed your code does exactly what I want, beautiful/aspirational bit of coding, nice one, Thanks.

Todd, thanks, I over assumed there would be some file comparison going on, but looks like it is satisfied with a not exist only.

---

<div class="post-metadata">

**Author:** ![Nesbit7](https://avatars.discourse-cdn.com/v4/letter/n/53a042/32.png) [@Nesbit7](https://forum.ansible.com/u/Nesbit7)\
**Post date:** [March 15, 2024, 3:14pm UTC](https://forum.ansible.com/t/ansible-builtin-copy-not-doing-what-i-expect-what-am-i-doing-wrong/4272/8 "2024-03-15T15:14:41Z")

</div>

weird the ‘ansible.builtin.get\_url’ downloaded the file to .asinstalled though, definitely not desired state on that.  
maybe a bug, but as my code was wrong in the first place, probs not worth flagging it

---

<div class="post-metadata">

**Author:** ![Denney-tech](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/denney-tech/32/1556_2.png) [@Denney-tech](https://forum.ansible.com/u/Denney-tech)\
**Post date:** [March 15, 2024, 4:12pm UTC](https://forum.ansible.com/t/ansible-builtin-copy-not-doing-what-i-expect-what-am-i-doing-wrong/4272/9 "2024-03-15T16:12:05Z")

</div>

I don’t think that get\_url: was saving to .asinstalled, it would be copy: rewriting it on subsequent runs.

Edit: you also had a copy step that is specifically copying the .debian file over .asinstalled, so there’s that too.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 14, 2024, 4:12pm UTC](https://forum.ansible.com/t/ansible-builtin-copy-not-doing-what-i-expect-what-am-i-doing-wrong/4272/10 "2024-04-14T16:12:13Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
