暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Ansible:模块(Modules)

Nephilim 2024-09-26
237

Tips:一些记录,一些笔记



2024/09/25

WEDNESDAY

If you wish to succeed, as perseverance, for friend, experience as your reference, prudence as your brother and hope as your sentry.

如果你希望成功,当以恒心为良友,以经验为参谋,以谨慎为兄弟,以希望为哨兵。



01

Ansible Modules


Ansible是做自动化运维非常方便的一个工具,它也是Redhat(红帽)IT自动化解决方案的核心技术之一。


Red Hat Enterprise IT automation:

https://www.redhat.com/en/solutions/it-automation



Ansible是如何为大家提供便捷的呢?


Ansible 官方网站:

https://www.redhat.com/en/technologies/management/ansible


就像Python给我们提供了非常丰富的类库,也有非常多的开发者参与到Python的生态中,开发了很多可以拿来即用的工具一样。


Ansible也提供了非常丰富的模块(Modules)以及将不同的模块组合在一起去解决某个特定任务的剧本(Playbooks)


这些资源可以公开的被访问与检索:

https://galaxy.ansible.com/ui/



Ansible的模块分为几种:

  • 核心模块(ansible-core)

  • 扩展模块(ansible-extra)

  • 自定义模块(当你学会了以上两种模块后,你是可以通过自定义模块的方式,去实现Ansible不能满足你的功能)


Ansible的官方GITHUB仓库:

核心模块:

https://github.com/ansible/ansible-modules-core


扩展模块:

https://github.com/ansible/ansible-modules-extras


最新的模块仓库:

https://github.com/ansible/ansible/tree/devel/lib/ansible/modules


来看看具体的一段Ansible代码中,模块是怎么体现的:

可以看到,编写Ansible的代码,其实就是在调用各种预先开发好的Modules,进而实现我们需要的功能。


02

「模块」有时候可能找不到


Ansible的很多常用模块可能并不一定是默认安装的,如果找不到对应模块,则会出现如下图所示的错误:


    couldn't resolve module/action 'pam_limits'.


    03

    解决方法

    首先你需要知道,你当前的操作系统中,Ansible的模块是在哪里存放的,Ansible去哪里找模块?


    比方说,我的环境里有三个版本的Ansible,你就通过三个版本的命令反馈看到它们的差异:

      [root@ansible ~]# ansible --version
      ansible 2.9.27
      config file = etc/ansible/ansible.cfg
      configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
      ansible python module location = usr/lib/python2.7/site-packages/ansible
      executable location = usr/bin/ansible
      python version = 2.7.5 (default, Nov 14 2023, 16:14:06) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
      [root@ansible ~]#
      [root@ansible ~]# ansible-3 --version
      /usr/lib/python3.6/site-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography. The next release of cryptography will remove support for Python 3.6.
      from cryptography.exceptions import InvalidSignature
      ansible-3 2.9.27
      config file = etc/ansible/ansible.cfg
      configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
      ansible python module location = usr/lib/python3.6/site-packages/ansible
      executable location = usr/bin/ansible-3
      python version = 3.6.8 (default, Nov 14 2023, 16:29:52) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
      [root@ansible ~]#
      [root@ansible ~]# ansible-3.6 --version
      /usr/lib/python3.6/site-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography. The next release of cryptography will remove support for Python 3.6.
      from cryptography.exceptions import InvalidSignature
      ansible-3.6 2.9.27
      config file = etc/ansible/ansible.cfg
      configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
      ansible python module location = usr/lib/python3.6/site-packages/ansible
      executable location = usr/bin/ansible-3.6
      python version = 3.6.8 (default, Nov 14 2023, 16:29:52) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
      [root@ansible ~]#


      如上所示,存放在「ansible python module location


      进入这个目录看看:

        [root@ansible ~]# cd usr/lib/python3.6/site-packages/ansible
        [root@ansible ansible]# ls -ltr
        total 52
        -rw-r--r--. 1 root root 1616 Oct 11 2021 __init__.py
        -rw-r--r--. 1 root root 2049 Oct 11 2021 context.py
        -rw-r--r--. 1 root root 9173 Oct 11 2021 constants.py
        -rw-r--r--. 1 root root 918 Oct 11 2021 release.py
        drwxr-xr-x. 2 root root 276 Aug 29 16:29 __pycache__
        drwxr-xr-x. 5 root root 228 Aug 29 16:29 cli
        drwxr-xr-x. 4 root root 61 Aug 29 16:29 compat
        drwxr-xr-x. 3 root root 120 Aug 29 16:29 config
        drwxr-xr-x. 3 root root 67 Aug 29 16:29 errors
        drwxr-xr-x. 6 root root 4096 Aug 29 16:29 executor
        drwxr-xr-x. 4 root root 143 Aug 29 16:29 galaxy
        drwxr-xr-x. 3 root root 126 Aug 29 16:29 inventory
        drwxr-xr-x. 23 root root 4096 Aug 29 16:29 module_utils
        drwxr-xr-x. 24 root root 4096 Aug 29 16:30 modules
        drwxr-xr-x. 6 root root 216 Aug 29 16:30 parsing
        drwxr-xr-x. 4 root root 4096 Aug 29 16:30 playbook
        drwxr-xr-x. 20 root root 4096 Aug 29 16:30 plugins
        drwxr-xr-x. 3 root root 123 Aug 29 16:30 template
        drwxr-xr-x. 3 root root 4096 Aug 29 16:30 utils
        drwxr-xr-x. 3 root root 137 Aug 29 16:30 vars
        [root@ansible ansible]#
        [root@ansible ansible]# ls -ltr modules/
        total 56
        -rw-r--r--. 1 root root 0 Oct 11 2021 __init__.py
        drwxr-xr-x. 2 root root 74 Aug 29 16:29 __pycache__
        drwxr-xr-x. 44 root root 4096 Aug 29 16:29 cloud
        drwxr-xr-x. 5 root root 215 Aug 29 16:29 clustering
        drwxr-xr-x. 3 root root 160 Aug 29 16:29 commands
        drwxr-xr-x. 5 root root 4096 Aug 29 16:29 crypto
        drwxr-xr-x. 12 root root 179 Aug 29 16:30 database
        drwxr-xr-x. 3 root root 4096 Aug 29 16:30 files
        drwxr-xr-x. 7 root root 157 Aug 29 16:30 identity
        drwxr-xr-x. 3 root root 82 Aug 29 16:30 inventory
        drwxr-xr-x. 4 root root 60 Aug 29 16:30 messaging
        drwxr-xr-x. 4 root root 4096 Aug 29 16:30 monitoring
        drwxr-xr-x. 9 root root 4096 Aug 29 16:30 net_tools
        drwxr-xr-x. 70 root root 4096 Aug 29 16:30 network
        drwxr-xr-x. 3 root root 4096 Aug 29 16:30 notification
        drwxr-xr-x. 5 root root 70 Aug 29 16:30 packaging
        drwxr-xr-x. 17 root root 257 Aug 29 16:30 remote_management
        drwxr-xr-x. 4 root root 4096 Aug 29 16:30 source_control
        drwxr-xr-x. 12 root root 173 Aug 29 16:30 storage
        drwxr-xr-x. 3 root root 4096 Aug 29 16:30 system
        drwxr-xr-x. 5 root root 71 Aug 29 16:30 utilities
        drwxr-xr-x. 5 root root 4096 Aug 29 16:30 web_infrastructure
        drwxr-xr-x. 3 root root 8192 Aug 29 16:30 windows
        [root@ansible ansible]#
        [root@ansible ansible]# ls -ltr modules/system/
        total 1172
        -rw-r--r--. 1 root root 12876 Oct 11 2021 xfs_quota.py
        -rw-r--r--. 1 root root 7103 Oct 11 2021 xfconf.py
        -rw-r--r--. 1 root root 37008 Oct 11 2021 vdo.py
        -rw-r--r--. 1 root root 109357 Oct 11 2021 user.py
        -rw-r--r--. 1 root root 20921 Oct 11 2021 ufw.py
        -rw-r--r--. 1 root root 36525 Oct 11 2021 timezone.py
        -rw-r--r--. 1 root root 13499 Oct 11 2021 sysvinit.py
        -rw-r--r--. 1 root root 23104 Oct 11 2021 systemd.py
        -rw-r--r--. 1 root root 3971 Oct 11 2021 syspatch.py
        -rw-r--r--. 1 root root 15312 Oct 11 2021 sysctl.py
        -rw-r--r--. 1 root root 9312 Oct 11 2021 svc.py
        -rw-r--r--. 1 root root 16999 Oct 11 2021 solaris_zone.py
        -rw-r--r--. 1 root root 8084 Oct 11 2021 setup.py
        -rw-r--r--. 1 root root 63506 Oct 11 2021 service.py
        -rw-r--r--. 1 root root 10790 Oct 11 2021 service_facts.py
        -rw-r--r--. 1 root root 8513 Oct 11 2021 seport.py
        -rw-r--r--. 1 root root 7779 Oct 11 2021 selogin.py
        -rw-r--r--. 1 root root 8292 Oct 11 2021 selinux.py
        -rw-r--r--. 1 root root 4165 Oct 11 2021 selinux_permissive.py
        -rw-r--r--. 1 root root 9800 Oct 11 2021 sefcontext.py
        -rw-r--r--. 1 root root 10825 Oct 11 2021 seboolean.py
        -rw-r--r--. 1 root root 8633 Oct 11 2021 runit.py
        -rw-r--r--. 1 root root 3293 Oct 11 2021 reboot.py
        -rw-r--r--. 1 root root 5203 Oct 11 2021 python_requirements_info.py
        -rw-r--r--. 1 root root 10605 Oct 11 2021 puppet.py
        -rw-r--r--. 1 root root 2221 Oct 11 2021 ping.py
        -rw-r--r--. 1 root root 2095 Oct 11 2021 pids.py
        -rw-r--r--. 1 root root 21902 Oct 11 2021 parted.py
        -rw-r--r--. 1 root root 9385 Oct 11 2021 pam_limits.py
        -rw-r--r--. 1 root root 31749 Oct 11 2021 pamd.py
        -rw-r--r--. 1 root root 13703 Oct 11 2021 osx_defaults.py
        -rw-r--r--. 1 root root 6313 Oct 11 2021 openwrt_init.py
        -rw-r--r--. 1 root root 11412 Oct 11 2021 open_iscsi.py
        -rw-r--r--. 1 root root 1375 Oct 11 2021 ohai.py
        -rw-r--r--. 1 root root 15986 Oct 11 2021 nosh.py
        -rw-r--r--. 1 root root 22409 Oct 11 2021 mount.py
        -rw-r--r--. 1 root root 3876 Oct 11 2021 modprobe.py
        -rw-r--r--. 1 root root 5410 Oct 11 2021 mksysb.py
        -rw-r--r--. 1 root root 4911 Oct 11 2021 make.py
        -rw-r--r--. 1 root root 19759 Oct 11 2021 lvol.py
        -rw-r--r--. 1 root root 10618 Oct 11 2021 lvg.py
        -rw-r--r--. 1 root root 7254 Oct 11 2021 locale_gen.py
        -rw-r--r--. 1 root root 7483 Oct 11 2021 listen_ports_facts.py
        -rw-r--r--. 1 root root 12950 Oct 11 2021 known_hosts.py
        -rw-r--r--. 1 root root 4009 Oct 11 2021 kernel_blacklist.py
        -rw-r--r--. 1 root root 10891 Oct 11 2021 java_keystore.py
        -rw-r--r--. 1 root root 13988 Oct 11 2021 java_cert.py
        -rw-r--r--. 1 root root 27295 Oct 11 2021 iptables.py
        -rw-r--r--. 1 root root 14894 Oct 11 2021 interfaces_file.py
        -rw-r--r--. 1 root root 0 Oct 11 2021 __init__.py
        -rw-r--r--. 1 root root 19256 Oct 11 2021 group.py
        -rw-r--r--. 1 root root 4284 Oct 11 2021 getent.py
        -rw-r--r--. 1 root root 8238 Oct 11 2021 gconftool2.py
        -rw-r--r--. 1 root root 1904 Oct 11 2021 gather_facts.py
        -rw-r--r--. 1 root root 29655 Oct 11 2021 firewalld.py
        -rw-r--r--. 1 root root 13383 Oct 11 2021 filesystem.py
        -rw-r--r--. 1 root root 1321 Oct 11 2021 facter.py
        -rw-r--r--. 1 root root 6078 Oct 11 2021 debconf.py
        -rw-r--r--. 1 root root 13500 Oct 11 2021 dconf.py
        -rw-r--r--. 1 root root 11164 Oct 11 2021 crypttab.py
        -rw-r--r--. 1 root root 13827 Oct 11 2021 cronvar.py
        -rw-r--r--. 1 root root 26204 Oct 11 2021 cron.py
        -rw-r--r--. 1 root root 6609 Oct 11 2021 capabilities.py
        -rw-r--r--. 1 root root 11899 Oct 11 2021 beadm.py
        -rw-r--r--. 1 root root 4437 Oct 11 2021 awall.py
        -rw-r--r--. 1 root root 22204 Oct 11 2021 authorized_key.py
        -rw-r--r--. 1 root root 5749 Oct 11 2021 at.py
        -rw-r--r--. 1 root root 5280 Oct 11 2021 alternatives.py
        -rw-r--r--. 1 root root 10578 Oct 11 2021 aix_lvol.py
        -rw-r--r--. 1 root root 11203 Oct 11 2021 aix_lvg.py
        -rw-r--r--. 1 root root 7431 Oct 11 2021 aix_inittab.py
        -rw-r--r--. 1 root root 17740 Oct 11 2021 aix_filesystem.py
        -rw-r--r--. 1 root root 9870 Oct 11 2021 aix_devices.py
        -rw-r--r--. 1 root root 25900 Jan 16 2022 hostname.py
        lrwxrwxrwx. 1 root root 27 Aug 29 16:30 _python_requirements_facts.py -> python_requirements_info.py
        drwxr-xr-x. 2 root root 8192 Aug 29 16:30 __pycache__
        [root@ansible ansible]#
        [root@ansible ansible]# ls -ltr modules/system/ | grep pam_limits
        -rw-r--r--. 1 root root 9385 Oct 11 2021 pam_limits.py
        [root@ansible ansible]#
        [root@ansible ansible]# ls -ltr modules/system/ | grep sysctl
        -rw-r--r--. 1 root root 15312 Oct 11 2021 sysctl.py
        [root@ansible ansible]#


        可以在这里找到很多之前常用的一些模块。


        如果Ansible在这里找不到对应的模块,或者对应的模块的Python文件出现了错误,则会如上所示,在运行「ansible ansible-playbook」的时候,出现错误。


        因此,解决方法也很简单:

        • 去官方网站找到需要的模块包替换本地错误的模块包

        • 如果是在新的环境中运行Ansible缺包,则需要从之前的环境中,将缺少的包直接添加到Ansible的模块目录中,即可






        END




        温馨提示



        如果你喜欢本文,请分享到朋友圈,想要获得更多信息,请关注我。


        文章转载自Nephilim,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

        评论