设为首页 收藏本站
查看: 996|回复: 0

[经验分享] 33-5 ansible playbook组件:任务列表、handlers、案例

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2018-7-30 08:00:07 | 显示全部楼层 |阅读模式
  管理端:192.168.1.131Centos7.2
  node1:1.121Centos6.7
  node2:1.122Centos6.7
  node3:1.123Centos6.7
  [root@server ~]# yum -y install ansible#需要安装EPEL源
  [root@server ~]# ssh-keygen -t rsa -P ''
  [root@server ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.1.131#管理本机
  [root@server ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.1.121
  [root@server ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.1.122
  [root@server ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.1.123
  [root@server ~]# cd /etc/ansible/
  [root@server ansible]# cp hosts{,.bak}
  [root@server ansible]# vim hosts
  添加
  [websrvs]
  192.168.1.121
  192.168.1.122
  [dbsrvs]
  192.168.1.123
  测试
  1、在指定主机组上:创建nginx组、创建nginx用户、复制文件
  [root@server ~]# vim nginx.yml
  - hosts: websrvs
  remote_user: root
  tasks:
  - name: create ninx group
  group: name=nginx system=yes gid=208
  - name: create nginx
  user: name=nginx uid=208 group=nginx system=yes
  - hosts: dbsrvs
  remote_user: root
  tasks:
  - name: copy file to dbsrvs
  copy: src=/etc/inittab dest=/tmp/inittab.ansible
  [root@server ~]# ansible-playbook nginx.yml
  
  2、在指定主机组上:安装apahce、修改配置文件、启动apache服务
  [root@server ~]# mkdir conf
  [root@server ~]# cp /etc/httpd/conf/httpd.conf conf/
  [root@server ~]# vim conf/httpd.conf
  修改
  Listen 80
  
  Listen 8080
  [root@server ~]# vim apache.yml
  - hosts: websrvs
  remote_user: root
  tasks:
  - name: install httpd package
  yum: name=httpd state=latest
  - name: install configuration file for httpd
  copy: src=/root/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
  - name: start httpd service
  service: enabled=true name=httpd state=started
  [root@server ~]# ansible-playbook apache.yml
  
  3、执行上面操作后,将配置文件作了更改
  [root@server ~]# vim apache.yml
  - hosts: websrvs
  remote_user: root
  tasks:
  - name: install httpd package
  yum: name=httpd state=latest
  - name: install configuration file for httpd
  copy: src=/root/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
  notify:
  - restart httpd
  - name: start httpd service
  service: enabled=true name=httpd state=started
  handlers:
  - name: restart httpd
  service: name=httpd state=restarted
  [root@server ~]# ansible-playbook apache.yml
  
  4、引入变量(功能同上)
  [root@server ~]# vim apache.yml
  - hosts: websrvs
  remote_user: root
  vars:
  - package: httpd
  - service: httpd
  tasks:
  - name: install httpd package
  yum: name=` package ` state=latest
  - name: install configuration file for httpd
  copy: src=/root/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
  notify:
  - restart httpd
  - name: start httpd service
  service: enabled=true name=` service ` state=started
  handlers:
  - name: restart httpd
  service: name=httpd state=restarted
  [root@server ~]# ansible-playbook apache.yml
  
  5、使用ansible内置变量
  [root@server ~]# vim test.yml
  - hosts: websrvs
  remote_user: root
  tasks:
  - name: copy file
  copy: content="` ansible_all_ipv4_addresses `" dest=/tmp/vars.ansi
  [root@server ~]# ansible-playbook test.yml
  
  6、自定义变量(主机内部变量)
  [root@server ~]# vim /etc/ansible/hosts
  修改后内容为:
  [websrvs]
  192.168.1.121 testvar="1.121"
  192.168.1.122 testvar="1.122"
  192.168.1.131
  [dbsrvs]
  192.168.1.123
  [root@server ~]# vim test.yml
  - hosts: websrvs
  remote_user: root
  tasks:
  - name: copy file
  copy: content="` ansible_all_ipv4_addresses `, ` testvar `" dest=/tmp/vars.ansi
  [root@server ~]# ansible-playbook test.yml
  
  7、条件测试(向符合条件的主机添加用户)
  [root@server ~]# vim cond.yml
  - hosts: all
  remote_user: root
  vars:
  - username: user10
  tasks:
  - name: create ` username ` user
  user: name=` username `
  when: ansible_fqdn == "node1"
  
  [root@server ~]# ansible-playbook cond.yml
  
  8、templates示例
  [root@server ~]# mkdir templates
  [root@server ~]# cp conf/httpd.conf templates/
  [root@server ~]# mv templates/httpd.conf templates/httpd.conf.j2
  [root@server ~]# vim templates/httpd.conf.j2
  修改
  Listen 80
  
  Listen ` http_port `
  修改
  MaxClients       256
  
  MaxClients       ` maxClients `
  修改
  #ServerName www.example.com:80
  
  ServerName ` ansible_fqdn `
  [root@server ~]# vim /etc/ansible/hosts
  添加以下内容
  [websrvs]
  192.168.1.121 http_port=80 maxClients=100
  192.168.1.122 http_port=8080 maxClients=100
  
  [root@server ~]# vim apache.yml
  - hosts: websrvs
  remote_user: root
  vars:
  - package: httpd
  - service: httpd
  tasks:
  - name: install httpd package
  yum: name=` package ` state=latest
  - name: install configuration file for httpd
  template: src=/root/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
  notify:
  - restart httpd
  - name: start httpd service
  service: enabled=true name=` service ` state=started
  handlers:
  - name: restart httpd
  service: name=httpd state=restarted
  [root@server ~]# ansible-playbook apache.yml
  9、tags示例
  [root@server ~]# vim apache.yml
  添加tags标签
  - hosts: websrvs
  remote_user: root
  vars:
  - package: httpd
  - service: httpd
  tasks:
  - name: install httpd package
  yum: name=` package ` state=latest
  - name: install configuration file for httpd
  template: src=/root/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
  tags:
  - conf
  notify:
  - restart httpd
  - name: start httpd service
  service: enabled=true name=` service ` state=started
  handlers:
  - name: restart httpd
  service: name=httpd state=restarted
  [root@server ~]# vim /etc/ansible/hosts
  修改主机配置文件内容
  [websrvs]
  192.168.1.121 http_port=80 maxClients=150
  192.168.1.122 http_port=8080 maxClients=180
  [root@server ~]# ansible-playbook apache.yml --tags="conf"
  
  10、roles示例
  [root@server ~]# mkdir -pv ansible_playbooks/roles/{websrvs,dbsrvs}/{tasks,files,templates,meta,handlers,vars}
  [root@server ~]# tree ansible_playbooks/
  ansible_playbooks/
  └── roles
  ├── dbsrvs
  │?? ├── files
  │?? ├── handlers
  │?? ├── meta
  │?? ├── tasks
  │?? ├── templates
  │?? └── vars
  └── websrvs
  ├── files
  ├── handlers
  ├── meta
  ├── tasks
  ├── templates
  └── vars
  15 directories, 0 files
  [root@server ~]# cd ansible_playbooks/
  [root@server ansible_playbooks]# cd roles/websrvs/
  [root@server websrvs]# cp /etc/httpd/conf/httpd.conf files/
  [root@server websrvs]# vim tasks/main.yml
  - name: install httpd package
  yum: name=httpd
  - name: install configuration file
  copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
  tags:
  - conf
  notify:
  - restart httpd
  - name: start httpd
  service: name=httpd state=started
  [root@server websrvs]# vim handlers/main.yml
  - name: restart httpd
  service: name=httpd state=restarted
  [root@server websrvs]# cd ../..
  [root@server ansible_playbooks]# vim site.yml
  - hosts: 192.168.1.121
  remote_user: root
  roles:
  - websrvs
  - hosts: 192.168.1.122
  remote_user: root
  roles:
  - dbsrvs
  - hosts: 192.168.1.123
  remote_user: root
  roles:
  - websrvs
  - dbsrvs
  [root@server ~]# cd ansible_playbooks/roles/dbsrvs/
  [root@server dbsrvs]# cp /etc/my.cnf files/
  [root@server dbsrvs]# vim tasks/main.yml
  - name: install mysql-server package
  yum: name=mysql-server state=latest
  - name: install configuration file
  copy: src=my.cnf dest=/etc/my.cnf
  tags:
  - myconf
  notify:
  - restart mysqld
  - name: start mysqld service
  service: name=mysqld enabled=true state=started
  [root@server dbsrvs]# vim handlers/main.yml
  - name: restart mysqld
  service: name=mysqld state=restarted  
  [root@server ansible_playbooks]# ansible-playbook site.yml   

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-543292-1-1.html 上篇帖子: 改进版重启java 程序,及配合ansible yml 批量执行 下篇帖子: 配置管理系统-学习之路
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表