gekmihesg.openwrt

Ansible 角色:openwrt

使用 Ansible 管理 OpenWRT 及其衍生版本,而无需 Python。

将主机放入库存组 openwrt 中时,某些模块会被替换为在标准 OpenWRT 安装上运行的 Shell 版本,尽量保留大部分原始功能。不在此组中的主机不受影响。这使得可以将 OpenWRT 和其他平台的任务混合使用。 还包含一些新的 OpenWRT 特定模块(如 uci)。 并不是所有的参数组合都经过测试! 某些情况仅为完整性将其从 Python 转换过来。

目前,已实现以下模块:

  • command
  • copy
  • fetch(隐式)
  • file
  • lineinfile
  • nohup(新)
  • opkg
  • ping
  • service
  • setup
  • shell(隐式)
  • slurp
  • stat
  • sysctl
  • template(隐式)
  • uci(新)
  • wait_for_connection(隐式)

为了实现这些功能,涉及一些猴子补丁(如果你想知道 vars_plugins)。

兼容性

此角色已在以下环境中成功测试:

  • LEDE 17.01(手动)
  • OpenWRT 18.06
  • OpenWRT 19.07
  • OpenWRT 21.02
  • OpenWRT 22.03

要求

某些模块可选地需要生成 SHA1 哈希或进行 Base64 编码。对于 Base64,包含一个非常慢的 hexdump | awk 实现。对于 SHA1,则没有替代方案。 这些模块会在需要时尝试查找可用的系统命令以生成 SHA1(sha1sumopenssl)和 Base64(base64openssl、替代方案)。如果没有找到可用的命令,大多数功能仍然可以使用,但例如,fetch 模块必须以 validate_checksum: no 运行,始终会下载该文件并返回 changed: yes。 因此,建议安装 coreutils-sha1sumcoreutils-base64,如果这些命令没有由 busybox 提供。该角色默认会自动处理这个问题(见下文)。

角色变量

openwrt_install_recommended_packages:
    检查一些命令,并在缺失时安装相应的包。见上面的要求。(默认:是)

openwrt_scp_if_ssh:
    是否在 OpenWRT 系统中使用 scp 而不是 sftp。值可以是 `yes`、`no` 或 `smart`。Ansible 默认使用 `smart`,但此角色默认使用 `yes`,因为 OpenWRT 默认不提供 sftp。(默认:是)

openwrt_remote_tmp:
    Ansible 在 OpenWRT 系统中的 remote_tmp 设置。默认为 /tmp,以避免目标设备的闪存磨损。(默认:/tmp)

openwrt_wait_for_connection, openwrt_wait_for_connection_timeout:
    是否等待主机(默认:是)以及在网络或 WiFi 重启后等待多长时间(300秒,见 handlers)。

openwrt_ssh, openwrt_scp, openwrt_ssh_host, openwrt_ssh_user, openwrt_user_host:
    辅助快捷方式,用于执行类似于
    "command: {{ openwrt_scp }} {{ openwrt_user_host|quote }}:/etc/rc.local /tmp" 的操作。

示例剧本

库存:

[aps]
ap1.example.com
ap2.example.com
ap3.example.com

[routers]
router1.example.com

[openwrt:children]
aps
routers

剧本:

- hosts: openwrt
  roles:
    - gekmihesg.openwrt
  tasks:
    - name: 复制 openwrt 镜像
      command: "{{ openwrt_scp }} image.bin {{ openwrt_user_host|quote }}:/tmp/sysupgrade.bin"
      delegate_to: localhost
    - name: 启动 sysupgrade
      nohup:
        command: sysupgrade -q /tmp/sysupgrade.bin
    - name: 等待重启
      wait_for_connection:
        timeout: 300
        delay: 60
    - name: 安装 mdns
      opkg:
        name: mdns
        state: present
    - name: 启用并启动 mdns
      service:
        name: mdns
        state: started
        enabled: yes
    - name: 复制授权密钥
      copy:
        src: authorized_keys
        dest: /etc/dropbear/authorized_keys
    - name: 撤销待处理的更改
      uci:
        command: revert
    - name: 配置 WiFi 设备 radio0
      uci:
        command: set
        key: wireless.radio0
        value:
          phy: phy0
          type: mac80211
          hwmode: 11g
          channel: auto
    - name: 配置 WiFi 接口
      uci:
        command: section
        config: wireless
        type: wifi-iface
        find_by:
          device: radio0
          mode: ap
        value:
          ssid: MySSID
          encryption: psk2+ccmp
          key: very secret
    - name: 提交更改
      uci:
        command: commit
      notify: 重新加载 WiFi

在剧本外部运行模块可以这样进行:

$ export ANSIBLE_LIBRARY=~/.ansible/roles/gekmihesg.openwrt/library
$ export ANSIBLE_VARS_PLUGINS=~/.ansible/roles/gekmihesg.openwrt/vars_plugins
$ ansible -i openwrt-hosts -m setup all

许可证

GNU 通用公共许可证 v3.0(见 https://www.gnu.org/licenses/gpl-3.0.txt)

开发

为此框架编写自定义模块并不困难。模块被封装在一个包装脚本中,该脚本提供了一些通用函数,用于参数解析、JSON 处理、响应生成等。 所有模块必须符合 openwrt_<module_name>.sh 的命名规则。如果 module_name 不是 Ansible 核心模块之一,则必须还有一个 <module_name>.py。这个文件不需要具备任何功能(可以针对非 OpenWRT 系统具有某些功能),并且可以包含文档。

确保在虚拟环境中安装 requirements.txt 中的包,并激活 venv 后,运行:

$ molecule test

在提交和提交你的 PR 之前。

强烈建议为你的新模块编写测试。

关于项目

Manage OpenWRT without python

安装
ansible-galaxy install gekmihesg.openwrt
许可证
gpl-3.0
下载
35.5k
拥有者