edu.51cto.comedu.51cto.com
自动化运维 --Ansible
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
Ansible Variables
edu.51cto.com
Variables Defined in Inventory
Variables Defined in a Playbook
Variables Defined in Commandline
Registered Variables
Variables defined from included files and roles
Using Variables: About Jinja2
Facts
edu.51cto.com
Variables Defined in Inventory
[webservers]app1_server http_port=80
[webservers:vars]some_server=foo.southeast.example.comhalon_system_timeout=30self_destruct_countdown=60escape_pods=2/etc/ansible/host_vars/app1_server/etc/ansible/group_vars/webservers
edu.51cto.com
Variables Defined in a Playbook
vars
vars_files
vars_prompt
edu.51cto.com
Variables Defined in a Commandline
- hosts: mfs_node user: "{{ uservar }}" tasks: - shell: echo "{{ echovar }}"
ansible-playbook command_vars.yml -e 'uservar="root" echovar="hello world"'ansible-playbook command_vars.yml -e '{"uservar":"root", "echovar":"hello world"}'ansible-playbook command_vars.yml -e '@test.json'
cat test.jsonuservar: rootechovar: hello world
edu.51cto.com
Registered Variables
- hosts: mfs_node tasks: - shell: echo "5" register: result ignore_errors: True
- debug: msg="it failed" when: result|failed
- debug: msg="{{result.stdout}}"
- shell: /usr/bin/bar when: result.rc == 5
edu.51cto.com
roles
edu.51cto.com
Using Variables: About Jinja2
template: src=foo.cfg.j2 dest={{ remote_install_path }}/foo.cfg
{{ some_variable | default(5) }}
edu.51cto.com
Facts
system facts
local facts
edu.51cto.com
System Facts
ansible <hostname> -m setup
edu.51cto.com
Local Facts
- hosts: webservers tasks: - name: create directory for ansible custom facts file: state=directory recurse=yes path=/etc/ansible/facts.d - name: install custom impi fact copy: src=ipmi.fact dest=/etc/ansible/facts.d - name: re-read facts after adding custom fact setup: filter=ansible_local
cat ipmi.fact[general]asdf=1bar=2
edu.51cto.com
Registered Variables
- hosts: web_servers tasks: - shell: /usr/bin/foo register: foo_result ignore_errors: True
- shell: /usr/bin/bar when: foo_result.rc == 5
edu.51cto.com
Omitting Undefined Variables
- name: touch files with an optional mode file: dest={{item.path}} state=touch mode={{item.mode|default(omit)}} with_items: - path: /tmp/foo - path: /tmp/bar - path: /tmp/baz mode: "0444"
edu.51cto.com
A YAML Gotcha
#This won’t work:
- hosts: app_servers vars: app_path: {{ base_path }}/22
#Do it like this and you’ll be fine:
- hosts: app_servers vars: app_path: "{{ base_path }}/22"
edu.51cto.com
Thank You !
edu.51cto.com