码迷,mamicode.com
首页 > 其他好文 > 详细

ansible-playbook role 编写示例

时间:2020-04-09 14:15:30      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:rem   http   pwd   role   nbsp   stat   else   配置文件   ORC   

下面是一个安装httpd软件的过程。
tasks / templates / vars 是必须的目录:

├── httpd
│   ├── tasks
│   │   ├── conf.yml
│   │   ├── group.yml
│   │   ├── httpd.yml
│   │   ├── main.yml
│   │   ├── selinux.yml
│   │   ├── service.yml
│   │   └── user.yml
│   ├── templates
│   │   └── httpd.conf.j2
│   └── vars
│       └── main.yml

 
在main.yml中包含各个单独的task:

[root@localhost roles]# cat httpd/tasks/main.yml 
- include: group.yml
- include: user.yml
- include: httpd.yml
- include: conf.yml
- include: service.yml
- include: selinux.yml

 
各个剧本:

[root@localhost roles]# cat httpd/tasks/group.yml 
- name: 创建用户组
  vars_file:
    - /etc/ansible/roles/httpd/vars/main.yml
  group: name={{ groupname }}

[root@localhost roles]# cat httpd/tasks/user.yml 
- name: 创建用户
  vars_file:
    - /etc/ansible/roles/httpd/vars/main.yml
  user: name={{ username }} system=yes group={{ groupname }}

[root@localhost roles]# cat httpd/tasks/httpd.yml 
- name: yum 安装httpd
  yum: name=httpd

[root@localhost roles]# cat httpd/tasks/conf.yml 
- name: 拷贝配置文件
  # 配置文件通过渲染完成后拷贝到被控主机
  template: src=/etc/ansible/roles/httpd/templates/httpd.conf.j2  dest=/etc/httpd/conf/httpd.conf backup=yes

    [root@localhost roles]# cat httpd/tasks/service.yml 
- name: 启动服务
  service: name=httpd state=started

[root@localhost roles]# cat httpd/tasks/selinux.yml 
- name: 关闭selinux
  shell: "sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/g‘ /etc/sysconfig/selinux"

 

[root@localhost roles]# cat httpd/templates/httpd.conf.j2 
...
{% if ansible_distribution_major_version == "7" %}
Listen 77
{% else %}
Listen 8080
{% endif %}
...

 

[root@localhost roles]# cat httpd/vars/main.yml 
username: app
groupname: app

 
在roles同级别目录中定义如下调用角色的剧本,httpd是目录名字

[root@localhost ansible]# pwd
/etc/ansible
[root@localhost ansible]# cat install_apache.yml
- hosts: webs
  remote_user: root
  # 使用角色,角色名是roles下的目录名字
  roles:
    - role: httpd

 
测试剧本,执行角色剧本:

ansible-playbook -C install_apache.yml
ansible-playbook install_apache.yml 

ansible-playbook role 编写示例

标签:rem   http   pwd   role   nbsp   stat   else   配置文件   ORC   

原文地址:https://blog.51cto.com/jackor/2485957

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!