ddlddx0000 发表于 2018-7-30 08:00:07

33-5 ansible playbook组件:任务列表、handlers、案例

  管理端:192.168.1.131Centos7.2
  node1:1.121Centos6.7
  node2:1.122Centos6.7
  node3:1.123Centos6.7
  # yum -y install ansible#需要安装EPEL源
  # ssh-keygen -t rsa -P ''
  # ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.1.131#管理本机
  # ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.1.121
  # ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.1.122
  # ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.1.123
  # cd /etc/ansible/
  # cp hosts{,.bak}
  # vim hosts
  添加
  
  192.168.1.121
  192.168.1.122
  
  192.168.1.123
  测试
  1、在指定主机组上:创建nginx组、创建nginx用户、复制文件
  # 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
  # ansible-playbook nginx.yml
  
  2、在指定主机组上:安装apahce、修改配置文件、启动apache服务
  # mkdir conf
  # cp /etc/httpd/conf/httpd.conf conf/
  # vim conf/httpd.conf
  修改
  Listen 80
  为
  Listen 8080
  # 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
  # ansible-playbook apache.yml
  
  3、执行上面操作后,将配置文件作了更改
  # 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
  # ansible-playbook apache.yml
  
  4、引入变量(功能同上)
  # 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
  # ansible-playbook apache.yml
  
  5、使用ansible内置变量
  # vim test.yml
  - hosts: websrvs
  remote_user: root
  tasks:
  - name: copy file
  copy: content="` ansible_all_ipv4_addresses `" dest=/tmp/vars.ansi
  # ansible-playbook test.yml
  
  6、自定义变量(主机内部变量)
  # vim /etc/ansible/hosts
  修改后内容为:
  
  192.168.1.121 testvar="1.121"
  192.168.1.122 testvar="1.122"
  192.168.1.131
  
  192.168.1.123
  # vim test.yml
  - hosts: websrvs
  remote_user: root
  tasks:
  - name: copy file
  copy: content="` ansible_all_ipv4_addresses `, ` testvar `" dest=/tmp/vars.ansi
  # ansible-playbook test.yml
  
  7、条件测试(向符合条件的主机添加用户)
  # vim cond.yml
  - hosts: all
  remote_user: root
  vars:
  - username: user10
  tasks:
  - name: create ` username ` user
  user: name=` username `
  when: ansible_fqdn == "node1"
  
  # ansible-playbook cond.yml
  
  8、templates示例
  # mkdir templates
  # cp conf/httpd.conf templates/
  # mv templates/httpd.conf templates/httpd.conf.j2
  # vim templates/httpd.conf.j2
  修改
  Listen 80
  为
  Listen ` http_port `
  修改
  MaxClients       256
  为
  MaxClients       ` maxClients `
  修改
  #ServerName www.example.com:80
  为
  ServerName ` ansible_fqdn `
  # vim /etc/ansible/hosts
  添加以下内容
  
  192.168.1.121 http_port=80 maxClients=100
  192.168.1.122 http_port=8080 maxClients=100
  
  # 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
  # ansible-playbook apache.yml
  9、tags示例
  # 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
  # vim /etc/ansible/hosts
  修改主机配置文件内容
  
  192.168.1.121 http_port=80 maxClients=150
  192.168.1.122 http_port=8080 maxClients=180
  # ansible-playbook apache.yml --tags="conf"
  
  10、roles示例
  # mkdir -pv ansible_playbooks/roles/{websrvs,dbsrvs}/{tasks,files,templates,meta,handlers,vars}
  # tree ansible_playbooks/
  ansible_playbooks/
  └── roles
  ├── dbsrvs
  │?? ├── files
  │?? ├── handlers
  │?? ├── meta
  │?? ├── tasks
  │?? ├── templates
  │?? └── vars
  └── websrvs
  ├── files
  ├── handlers
  ├── meta
  ├── tasks
  ├── templates
  └── vars
  15 directories, 0 files
  # cd ansible_playbooks/
  # cd roles/websrvs/
  # cp /etc/httpd/conf/httpd.conf files/
  # 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
  # vim handlers/main.yml
  - name: restart httpd
  service: name=httpd state=restarted
  # cd ../..
  # 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
  # cd ansible_playbooks/roles/dbsrvs/
  # cp /etc/my.cnf files/
  # 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
  # vim handlers/main.yml
  - name: restart mysqld
  service: name=mysqld state=restarted
  # ansible-playbook site.yml   
页: [1]
查看完整版本: 33-5 ansible playbook组件:任务列表、handlers、案例