Ansible配置复用

1. include

利用include可以将一个Playbook根据具体的操作拆分成多个文件,更容易组织和管理Playbook。

  • 定义一个安装nginx的yaml文件
# cat install_nginx.yml
- name: install nginx
  package:
    name: nginx
    state: present

- name: ensure nginx enabled and started
  systemd:
    name: nginx
    state: started
    enabled: yes

- name: config nginx        
  copy:
    src: /etc/nginx/nginx.conf   # 可以维护一个自己的配置,这里只是为了测试,用template配置更灵活
    dest: /etc/nginx/nginx.conf
  notify: restart nginx
  • 定义一个重启nginx的yaml文件
# cat restart_nginx.yml
- name: restart nginx
  systemd:
    name: nginx
    state: restarted
  • 定义一个入口文件,可在这个文件执行ansible-playbook -i hosts nginx.yml
# cat nginx.yml         # 相当于一个入口文件
---
- hosts: proxy

  tasks:
    - include: install_nginx.yml

  handlers:
    - include: restart_nginx.yml

2. roles

这个是ansible提供的又一个非常灵活且强大的功能,就是强!可以基于ansible-galaxy初始化配置目录,也可以基于ansible galaxy网站找一些比较不错的roles下载下来使用。

  • 初始化roles目录结构
# ansible-galaxy init deploy
- Role deploy was created successfully

# tree deploy
deploy
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml
  • 查看其它命令
# ansible-galaxy role -h
usage: ansible-galaxy role [-h] ROLE_ACTION ...

positional arguments:
  ROLE_ACTION
    init       Initialize new role with the base structure of a role.
    remove     Delete roles from roles_path.
    delete     Removes the role from Galaxy. It does not remove or alter the
               actual GitHub repository.
    list       Show the name and version of each role installed in the
               roles_path.
    search     Search the Galaxy database by tags, platforms, author and
               multiple keywords.
    import     Import a role
    setup      Manage the integration between Galaxy and the given source.
    login      Login to api.github.com server in order to use ansible-galaxy
               role sub command such as 'import', 'delete', 'publish', and
               'setup'
    info       View more details about a specific role.
    install    Install role(s) from file(s), URL(s) or Ansible Galaxy
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值