美文网首页
Ansible Galaxy使用方法

Ansible Galaxy使用方法

作者: 张都尉 | 来源:发表于2019-08-06 18:55 被阅读0次

安装模块

  • 直接使用ansible-galaxy安装一些模块
$ ansible-galaxy install username.rolename
  • 也可以批量安装,ansible提供了两钟方式配置依赖模块,可以批量安装模块
    • 方式一 使用 roles.txt
 # 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格式

  • 方式二 使用YML格式的配置文件
    创建 install_roles.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
  • Meta Data - meta/main.yml
    meta/main.yml 用来配置模块一些元信息,比如支持的平台,Ansible最小依赖版本,
    这个模块的标签,依赖,作者的信息等等。

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 用于配置依赖的其它模块 比如
dependencies:
  - { role: stouts.elasticsearch }

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

dependencies:
  - { role: stouts.elasticsearch, elasticsearch_home: "{{ install_path_variable }}", elasticsearch_http_port: 8900 }
  • task/main.yml 是这个role的入口文件,命令从main.yml开始执行
  • files files目录是放一些文件用的,比如可以用copy命令,把files目录下的文件复制到服务器上。这块遵从ansible合集模块,比如:
- name: copy source.list.{{ansible_distribution_release}} file
  copy: src=sources.list.{{ansible_distribution_release}} dest=/etc/apt/sources.list backup=yes
  • templates 用户jinjia2模块文件 Jinja2是基于[python](https://baike.baidu.com/item/python/407313)的[模板引擎](https://baike.baidu.com/item/%E6%A8%A1%E6%9D%BF%E5%BC%95%E6%93%8E/907667),功能比较类似于于[PHP](https://baike.baidu.com/item/PHP/9337)的[smarty](https://baike.baidu.com/item/smarty/7270424),[J2ee](https://baike.baidu.com/item/J2ee)的[Freemarker](https://baike.baidu.com/item/Freemarker)和[velocity](https://baike.baidu.com/item/velocity/1398152)。 它能完全支持[unicode](https://baike.baidu.com/item/unicode/750500),并具有集成的沙箱执行环境,应用广泛。jinja2使用[BSD](https://baike.baidu.com/item/BSD)授权。
  • defaults/main.yml 默认的配置变量写在defaults/main.yml里
  • handlers/main.yml 一般用来重启服务,比如
---
# this might be in a file like handlers/handlers.yml
- name: restart apache
 service: name=apache state=restarted

相关文章

  • Ansible Galaxy使用方法

    ansible也有类似rubygems.org 的网站, 让ansible用户分享ansible功能模块网址 ht...

  • DevOps 之 Ansible入门实践(一)

    本文将介绍在Ansible 以及 Galaxy 的基本使用。 本文默认操作环境为 Mac. 安装Ansible[^...

  • Ansible Galaxy SSL 报错

    [root@localhost roles]# ansible-galaxy install dj-wasabi....

  • 第5章 role和Ansible Galaxy

    5.1 role 和 Ansible Galaxy 的简要介绍 5.1.1 role role 是高级版本的inc...

  • ansible

    ansible-doc -s ping 简单列出ping模块的使用方法ansible +要控制的ip +模块名...

  • 任务中心之Ansible进阶篇

    在 任务中心之Ansible基础篇 已经介绍了Ansible的技术架构以及功能和基本配置、使用方法,如果看完的同学...

  • 随笔

    最近要做有关自动化部署的事情,主要使用ansible来实现自动化部署,最近会分享一些有关ansible的使用方法

  • Ansible-8 Roles以及Galaxy

    一个数据中心有可能存在好多类型的服务器。比如WEB类型、DB类型、开发人员使用的开发类型、QA使用的测试类型等等。...

  • 用 Flask 实现的在 Web 页面执行 shell 命令的小

    1、项目结构 2、项目文件内容 3、使用方法运行在 Python 2.7.x 环境下,依赖 ansible,fla...

  • docker-compose

    docker-compose一个单机版的容器编排工具,使用方法类似于ansible的playbook.可直接使用命...

网友评论

      本文标题:Ansible Galaxy使用方法

      本文链接:https://www.haomeiwen.com/subject/eviwdctx.html