问题原文
原帖链接 · Rino Bino · CC BY-SA 4.0
Situation
I have some network gear that must run some commands within a network namespace in order to reach the internet.
In my scenario, it's the mgmt namespace.
For example, to install a pip package, I would do:
sudo ip netns exec ns-mgmt pip3 install boto3
Or to wget a file, I need to do:
sudo ip netns exec ns-mgmt wget https://example.com
Problem
So, because of this, regular Ansible commands that require reaching the internet do not work:
- name: "download file archive"
get_url:
url: "{{ my_dl_path }}"
dest: "/tmp/downloaded_file.tar.gz"
Solutions / Questions
It seems like I have two options:
- Run the desired commands as a
commandorshelltask, instead of using individual modules (not best practice). - Run the tasks on the localhost (using
delegate_to) and copy the files over. This would be fine for simple things likewgetinstead of get_url module, but would not work for things like the pip module when installing stuff from the internet.
How would you tackle this? Is there some sort of Ansible task override to handle this scenario?
已采纳解答
原帖链接 · U880D · CC BY-SA 4.0
Regarding
Is there some sort of Ansible task override to handle this scenario?
Yes, there is. Depending on how one look on it, it is quite simple. The solution lays within the Become plugins.
Regarding
How would you tackle this?
(1) You could just create your own customized become plugin based on the existing ansible/plugins/become/sudo.py, means just tweak it with an additional 'ip netns exec %s' % (become_network_namespace).
After a custom become plugin is created and provided, in example called as sudo_ns.py, one could use privilege escalation and a new to introduce fact become_network_namespace like
- name: "download file archive"
become: true
become_method: sudo_ns
get_url:
url: "{{ my_dl_path }}"
dest: "/tmp/downloaded_file.tar.gz"
vars:
become_network_namespace: 'ns-mgmt'
(2) You could try and test a shortcut with the existing become plugin sudo and become_flags.
- name: "download file archive"
become: true
become_method: sudo
become_flags: 'ip netns exec {{ BECOME_NETWORK_NAMESPACE }}'
get_url:
url: "{{ my_dl_path }}"
dest: "/tmp/downloaded_file.tar.gz"
Further Documentation
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
