Ansible Galaxy使用方法

2019-08-06  本文已影响0人  张都尉

安装模块

$ ansible-galaxy install username.rolename
 # roles.txt
    user1.roles1,v1.0
    user2.roles2,v1.2
    user3.roles3,v1.3

配置规则格式为 username.rolesname[,version]的形式

$ ansible-galaxy install -r roles.txt

方式一 不指定安装路径,也不指定安装名称,即ansible提供了高级的配置格式:YML格式

# install_roles.yml

# from galaxy
- src: yatesr.timezone

# from github
- src: https://github.com/bennojoy/nginx

# from github installing to a relative path
- src: https://github.com/bennojoy/nginx
  path: vagrant/roles/

# from github, overriding the name and specifying a specific tag
- src: https://github.com/bennojoy/nginx
  version: master
  name: nginx_role

# from a webserver, where the role is packaged in a tar.gz
- src: https://some.webserver.example.com/files/master.tar.gz
  name: http-role

# from bitbucket, if bitbucket happens to be operational right now :)
- src: git+http://bitbucket.org/willthames/git-ansible-galaxy
  version: v1.4

# from bitbucket, alternative syntax and caveats
- src: http://bitbucket.org/willthames/hg-ansible-galaxy
  scm: hg

安装配置好的role模块

$ ansible-galaxy install -r install_roles.yml

可以在项目里创建ansible.cfg配置文件,配置role模块下载位置

ansible.cfg
[defaults]
hostfile = inventory
roles_path = roles

创建模块

举例比如创建一个acme模块

$ ansible-galaxy init acme --force

文件结构为:

.
  ├── README.md
  ├── defaults
  │   └── main.yml
  ├── files
  ├── handlers
  │   └── main.yml
  ├── meta
  │   └── main.yml
  ├── tasks
  │   └── main.yml
  ├── templates
  ├── tests
  │   ├── inventory
  │   └── test.yml
  └── vars
      └── main.yml

ansible-galaxy init 创建项目生成的模板如下

---
galaxy_info:
 author: John Doe
 description: Quick and easy acme web installer.
 company: Acme
 license: MIT
 min_ansible_version: 1.9
 platforms:
 - name: EL
   versions:
   - all
 galaxy_tags:
   - acme
   - installer
   - web
dependencies:
 - { role: username.common, some_parameter: 3 }
 - { role: username.iptables, open_port: 80 }
dependencies:
  - { role: stouts.elasticsearch }

当然也可以传参,比如安装elasticsearch:

dependencies:
  - { role: stouts.elasticsearch, elasticsearch_home: "{{ install_path_variable }}", elasticsearch_http_port: 8900 }
- name: copy source.list.{{ansible_distribution_release}} file
  copy: src=sources.list.{{ansible_distribution_release}} dest=/etc/apt/sources.list backup=yes
---
# this might be in a file like handlers/handlers.yml
- name: restart apache
 service: name=apache state=restarted
上一篇 下一篇

猜你喜欢

热点阅读