Ansible copy module requires writable parent directory?

久未见 提交于 2020-06-01 04:28:13

问题


Need to set /proc/sys/net/ipv4/conf/all/forwarding to 1

That's can be easily done via command

- name: Enable IPv4 traffic forwarding
  command: echo 1 > /proc/sys/net/ipv4/conf/all/forwarding

But that's bad practice - it will be always "changed" task.

So I tried the following:

- name: Enable IPv4 traffic forwarding
  copy: content=1 dest="/proc/sys/net/ipv4/conf/all/forwarding" force=yes

Which failed with msg: "Destination /proc/sys/net/ipv4/conf/all not writable"

According to sources seems like copy always requires parent directory will be writable. But 1) I don't understand why 2) Any other "idiomatic" way to set destination file to required value?


回答1:


While I still do not understand why copy needs to check parent directory permissions, thanks to @larsks:

sysctl module changes both sysctl.conf and /proc values

and this solves my task




回答2:


- name: Enable IPv4 traffic forwarding
  copy: content=1 dest="/proc/sys/net/ipv4/conf/all/forwarding" unsafe_writes=true

will disable Ansible's atomic write functionality, and instead write 1 to the file directly.

Atomic writes are good and useful because they mean you will never get a corrupted file that has the output of multiple processes interleaved, but /proc is a special magic thing. The classic Unix dance of writing to a temporary file until you're done, and then renaming it to the final filename you want breaks because /proc doesn't let you create random temporary files.




回答3:


I found a workaround for this problem:

  - name: Create temp copy of mongod.conf
    copy:
      src : /etc/mongod.conf
      dest: /tmp/mongod.conf
      remote_src: yes
    diff: no
    check_mode: no
    changed_when: false

  - name: Copy config file mongod.conf
    copy:
      src : "/source/of/your/mongod.conf" 
      dest: "/tmp/mongod.conf"
    register: result

  - name: Copy temp mongod.conf to /etc/mongod.conf
    shell: "cp --force /tmp/mongod.conf /etc/mongod.conf"
    when: result.changed == true


来源:https://stackoverflow.com/questions/36140661/ansible-copy-module-requires-writable-parent-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!