Linux 第五本书 第六单元--------Ansible中的角色使用

roles

vim ansible.cfg
[
roles_path= ~/.ansible/roles   添加roles
]
mkdir roles
cd roles/
ansible-galaxy init apache 创建目录结构

在.ansible下

 


vim http.yml

[
- name: create web
  hosts: westos
  roles:
    - role: apache
]
在 roles/apache下

 


vim tasks/main.yml
[
- name: install
  dnf:
     name: httpd
     state: present
- name: start apache
  service:
     name: httpd
     state: started
     enabled: yes
- name: firewalld set
  firewalld:
     service: http
     permanent: yes
     state: enabled
     immediate: yes
- name: create documentroot
  file:
     path: "{{item.doc}}"
     state: directory
  loop:
     "{{webs}}"
- name: create index.html
  copy:
     dest: "{{item.doc}}/index.html"
     content: "{{item.index}}"
  loop:
     "{{webs}}"
- name: create vhost.conf
  template:
     src: ./httpd.j2
     dest: /etc/httpd/conf.d/vhosts.conf
  notify: restart apache

]


vim handlers/main.yml

 


[
 - name: restart apache
   service:
       name: httpd
       state: restarted

]


vim vars/main.yml
[
webs:
 - doc: /var/www/html
   index: www.westos.org
 - name: linux.westos.org
   doc: /var/www/virtual/westos.org/linux
   index: linux.westos.org
 - name: java.westos.org
   doc: /var/www/virtual/westos.org/java
   index: java.westos.org
]

 

=================================================
控制任务执行顺序

 

vim http.yml

[
- name: create web
  hosts: westos
  roles:
    - role: apache
  pre_tasks:               -------在play前执行
    - name: remove apache
      dnf:
         name: httpd
         state: absent
  post_tasks:             ---------在play后执行
    - name: sucessful
      debug:
        msg: web configur sucessfully
  tasks:                  -------普通任务
    - name: install lftp
      dnf:
        name: lftp
        state: present
      notify: install message

  handlers:               ------选择执行触发器
    - name: install message
      debug:
        msg: message is ok

]

====================================================
ansible-galaxy命令工具


tar zcf /tmp/apache.tar.gz  apache/  -----把.ansible/roles的apche打包到tmp

vim install_apache.yml
[
- src: file:///tmp/apache.tar.gz
  name: westos
]

 ansible-galaxy  install -r install_apache.yml

查看解压的westos

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值