是否可以从git安装自定义Ansible插件

时间:2019-12-12 10:13:07

标签: ansible

我想在多个剧本和用户之间共享自定义清单插件。

是否可以在git上托管自定义清单插件,并且使用requirements.yml之类的角色执行以下操作:

ansible-galaxy install -r requirements.yml

我尝试使用以下方法将其嵌入角色:

/myrole/library/inventory_plugins/custom_inventory.py
/myrole/plugins/inventory_plugins/custom_inventory.py
/myrole/inventory_plugins/custom_inventory.py

但到目前为止还没有运气。

1 个答案:

答案 0 :(得分:0)

  

Q:“是否可以从git安装自定义Ansible插件?”

A:是的。这是可能的。例如

1)下载并提取插件

  vars:

    ma_src_path: "/usr/local/ansible/src"
    ma_plugins_path: "/usr/local/ansible/plugins"
    map_mitogen_ver: 0.2.8                                                                        
    map_mitogen_sha256: "sha256:1bfca66bcc522346c9167a3a9829feac5ee3b84431e49354fb780e4b9a4b0eee"

    ma_plugins:                                                                                   
      - archive: mitogen-{{ map_mitogen_ver }}.tar.gz                                             
        archive_url: https://networkgenomics.com/try/mitogen-{{ map_mitogen_ver }}.tar.gz         
        checksum: "{{ map_mitogen_sha256 }}"                                                      
        plugins:                                                                                  
          - path: mitogen-{{ map_mitogen_ver }}/ansible_mitogen/plugins/strategy                  
            ini_key: strategy_plugins                                                             
            enable: true                                                                          

  tasks:

    - name: "plugins: Download archives"
      get_url:
        url: "{{ item.archive_url }}"
        dest: "{{ ma_src_path }}"
        checksum: "{{ item.checksum }}"
      loop: "{{ ma_plugins }}"

    - name: "plugins: Extract archives"
      unarchive:
        src: "{{ ma_src_path }}/{{ item.archive }}"
        dest: "{{ ma_plugins_path }}"
      loop: "{{ ma_plugins }}"

2)使用模板ansible-plugins.cfg.j2

配置插件
  vars:

    ma_config:
      - path: "/etc/ansible/ansible.cfg"
        template: "ansible-plugins.cfg.j2"
        owner: "root"
        group: "root"
        mode: "0644"
        config:
         - { section: "defaults", key: "inventory", value: "/etc/ansible/hosts" }
         - { section: "defaults", key: "strategy", value: "mitogen_linear" }

  tasks:

    - name: "configure: Ansible configuration from template"
      template:
        src: "{{ item.template }}"
        dest: "{{ item.path }}"
        owner: "{{ item.owner }}"
        group: "{{ item.group }}"
        mode: "{{ item.mode }}"
        backup: "{{ ma_backup_conf }}"
      loop: "{{ ma_config }}"
      when: ma_config|length > 0

请参阅Ansible Galaxy上的完整角色。