自动化运维--Ansible

Download Report

Transcript 自动化运维--Ansible

自动化运维--Ansible
edu.51cto.com
edu.51cto.com
讲师: Breeze Yan
自动化运维QQ群: 22346584
edu.51cto.com
课程目录
Course Contents
一、Ansible简介与Hosts Inventory
二、Ansible常用模块
三、Ansible Playbook简单介绍
四、Playbook的角色及包含
五、Playbook的变量
六、Playbook的条件判断
七、Playbook的循环
八、Playbook常用模块
九、自定义模块
十、Ansible生产案例
edu.51cto.com
Playbook Conditionals
edu.51cto.com
内置变量
hostvars
groups
group_names
inventory_hostname
inventory_hostname_short
inventory_dir
inventory_file
edu.51cto.com
hostvars
- name: Get the masters IP
set_fact: dns_master="{{ hostvars.ns1.ansible_default_ipv4.address }}"
- name: Configure BIND
template: dest=/etc/named.conf src=templates/named.conf.j2
edu.51cto.com
groups
- name: Create a user for all app servers
with_items: groups.appservers
mysql_user: name=kate password=test host={{
hostvars.[item].ansible_eth0.ipv4.address }} state=present
{% for host in groups['all'] %}
{{ hostvars[host]['ansible_hostname'] }}
{{hostvars[host]['ansible_ssh_host_key_rsa_public'] }}
{% endfor %}
edu.51cto.com
groups_names
- name: For secure machines
set_fact: sshconfig=files/ssh/sshd_config_secure
when: "'secure' in group_names"
- name: For non-secure machines
set_fact: sshconfig=files/ssh/sshd_config_default
when: "'secure' not in group_names"
edu.51cto.com
inventory_hostname
inventory_hostname变量保存了在设备配置清单中服务器的主机名
edu.51cto.com
inventory_hostname_short
inventory_hostname_short变量跟inventory_hostname一样,只是去掉域名,
比如inventory_hostname 是host.example 那么inventory_hostname_short就是host
edu.51cto.com
inventory_dir
inventory_dir是设备清单文件的路径
edu.51cto.com
inventory_file
inventory_file是设备清单文件的文件名
edu.51cto.com
when
tasks:
- name: "shutdown Debian flavored systems"
command: /sbin/shutdown -t now
when: ansible_os_family == "Debian"
edu.51cto.com
when
tasks:
- command: /bin/false
register: result
ignore_errors: True
- command: /bin/something
when: result|failed
- command: /bin/something_else
when: result|success
- command: /bin/still/something_else
when: result|skipped
edu.51cto.com
when
vars:
epic: true
tasks:
- shell: echo "This certainly is epic!"
when: epic
edu.51cto.com
when
tasks:
- shell: echo "I've got '{{ foo }}' and am not afraid to use it!"
when: foo is defined
- fail: msg="Bailing out. this play requires 'bar'"
when: bar is not defined
edu.51cto.com
when
tasks:
- command: echo {{ item }}
with_items: [ 0, 2, 4, 6, 8, 10 ]
when: item > 5
edu.51cto.com
when
- hosts: webservers
roles:
- { role: debian_stock_config, when: ansible_os_family == 'Debian' }
edu.51cto.com
when
- name: test play
hosts: mfs
tasks:
- shell: cat /etc/motd
register: motd_contents
- shell: echo "motd contains the word hi" > /tmp/test2
when: motd_contents.stdout.find('hi') != -1
edu.51cto.com
changed_when
tasks:
- shell: /usr/bin/billybass --mode="take me to the river"
register: bass_result
changed_when: "bass_result.rc != 2"
# this will never report 'changed' status
- shell: wall 'beep'
changed_when: False
edu.51cto.com
failed_when
- name: this command prints FAILED when it fails
command: /usr/bin/example-command -x -y -z
register: command_result
failed_when: "'FAILED' in command_result.stderr"
- name: this command prints FAILED when it fails
command: /usr/bin/example-command -x -y -z
register: command_result
ignore_errors: True
- name: fail the play if the previous command did not succeed
fail: msg="the command failed"
when: "'FAILED' in command_result.stderr"
edu.51cto.com
Thank You !
edu.51cto.com
edu.51cto.com