大网数据广告活动
服务器运维问答

Running Ansible tasks (ex: get_url) within a network namespace

· 52CE 我爱测

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 e…

问题原文

原帖链接 · 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 command or shell task, 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 like wget instead 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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-08-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。