Skip to content

Latest commit

 

History

History
72 lines (68 loc) · 1.86 KB

20171008_01.md

File metadata and controls

72 lines (68 loc) · 1.86 KB

ansible入门5:ansible条件判断及角色roles和邮件通知

作者

jwh5566

日期

2017-10-07

标签

python,ansible 2


条件选择

---
- hosts: all
  remote_user: ansible
  tasks:
  - name: print the ansible_os_family value
    debug:
      msg: '{{  ansible_os_family }}'
  - name: ensure httpd is updated
    yum:
      name: httpd
      state: latest
    become: True
    when: ansible_os_family == 'RedHat'
  - name: ensure  apache2 is updated
    apt:
      name: apache2
      state: latest
    become: True
    when: ansible_os_family == 'Debian'

roles

├── ansible.cfg			# 帮助文件,指导ansible去哪找hosts和roles
├── hosts
├── master.yaml			# 入口文件
├── playbooks
│   ├── firstrun.yaml		# 一次性playbook
│   └── groups
│       ├── database.yaml	# 这里表示tasks需要在那些group上面执行
│       └── webserver.yaml
└── roles
    ├── common
    │   ├── files		# 包含需要复制到远程的文件	
	│	├── handlers	# 包含tasks的触发器
	│	├── tasks
    │   │   └── main.yaml   	# 这里是common角色需要执行的task
    │   └── templates
    │       └── motd		# 这里是tasks需要的模板文件
    ├── database
    └── webserver  

playbook执行: ansible-playbook master.yaml

ansible邮件通知

- hosts: localhost
  tasks:
  - name: Read the machine uptime
    command: uptime -p
    register: uptime
  - name: Send the uptime via e-mail
    mail:
      host: mail.fale.io
      username: [email protected]
      password: PASSWORD
      to: [email protected]
      subject: Ansible-report
      body: 'Local system uptime is {{ uptime.stdout }}.'