---
- hosts: webservers
vars:
http_port: 80
max_clients: 200
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum:
name: httpd
state: latest
- name: write the apache config file
template:
src: /srv/httpd.j2
dest: /etc/httpd.conf
notify:
- restart apache
- name: ensure apache is running
service:
name: httpd
state: started
---
- hosts: webservers
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum:
name: httpd
state: latest
- name: write the apache config file
template:
src: /srv/httpd.j2
dest: /etc/httpd.conf
- hosts: databases
remote_user: root
tasks:
- name: ensure postgresql is at the latest version
yum:
name: postgresql
state: latest
- name: ensure that postgresql is started
service:
name: postgresql
state: started
- import_playbook: webservers.yml
- import_playbook: databases.yml
---
- hosts: webservers
tasks:
- debug:
msg: "before we run our role"
- import_role:
name: example
- include_role:
name: example
- debug:
msg: "after we ran our role"
---
- hosts: webservers
roles:
- common
- role: foo_app_instance
vars:
dir: '/opt/a'
app_port: 5000
- role: foo_app_instance
vars:
dir: '/opt/b'
app_port: 5001
---
- hosts: webservers
tasks:
- import_role:
name: foo
tags:
- bar
- baz
---
- hosts: webservers
tasks:
- include_role:
name: some_role
when: "ansible_os_family == 'RedHat'"
---
- hosts: webservers
roles:
- common
- webservers
###OR
- hosts: webservers
roles:
- role: '/path/to/my/roles/common'
---
vars:
field1: one
---
foo:
field1: one
field2: two
---
- hosts: webservers
vars:
http_port: 80
---
- hosts: node1
gather_facts: false
tasks:
- name: Use var debug
vars:
- name: weimeng
- age: 26
debug:
var: name,age
- hosts: node1
gather_facts: false
tasks:
- include_role:
name: role_A
vars:
age: 24
tasks:
- import_tasks: wordpress.yml
vars:
wp_user: timmy
- import_tasks: wordpress.yml
vars:
wp_user: alice
- import_tasks: wordpress.yml
vars:
wp_user: bob
---
- hosts: webservers
roles:
- role: bar
tags: ["foo"]
# using YAML shorthand, this is equivalent to the above
- { role: foo, tags: ["bar", "baz"] }
---
- hosts: webservers
roles:
- role: bar
tags: ["foo"]
# using YAML shorthand, this is equivalent to the above
- { role: foo, tags: ["bar", "baz"] }
ansible-playbook release.yml --extra-vars "version=1.23.45 other_variable=foo”
---
- hosts: all
remote_user: root
vars:
favcolor: blue
vars_files:
- /vars/external_vars.yml
tasks:
- name: this is just a placeholder
command: /bin/echo foo
ansible hostname -m setup
---
- hosts: whatever
gather_facts: no
- hosts: app_servers
vars:
app_path: "{{ base_path }}/22"
{{ foo[0] }}
{{ansible_eth0["ipv4"]["address"] }}
{{ansible_eth0.ipv4.address }}
---
- hosts: node1
gather_facts: false
vars:
- name: weimeng
- age: 26
tasks:
- name: Use var debug
debug:
var: name,age
- name: Use msg debug
debug:
msg: "my name is {{ name }},and my age is {{ age }}"
➜ lab-ansible ansible-playbook playbooks/task_vars.yml
[WARNING]: Found variable using reserved name: name
PLAY [node1] *******************************************************************
TASK [Use var debug] ***********************************************************
ok: [node1] => {
"name,age": "(u'weimeng', 26)"
}
TASK [Use msg debug] ***********************************************************
ok: [node1] => {
"msg": "my name is weimeng,and my age is 26"
}
PLAY RECAP *********************************************************************
node1 : ok=2 changed=0 unreachable=0 failed=0
tasks:
- name: "shut down Debian flavored systems"
command: /sbin/shutdown -t now
when: ansible_os_family == "Debian"
# note that Ansible facts and vars like ansible_os_family can be used
# directly in conditionals without double curly braces
tasks:
- name: "shut down CentOS 6 and Debian 7 systems"
command: /sbin/shutdown -t now
when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6") or
(ansible_distribution == "Debian" and ansible_distribution_major_version == "7")
tasks:
- name: "shut down CentOS 6 systems"
command: /sbin/shutdown -t now
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "6"
tasks:
- command: /bin/false
register: result
ignore_errors: True
- command: /bin/something
when: result is failed
# In older versions of ansible use ``success``, now both are valid but succeeded uses the correct tense.
- command: /bin/something_else
when: result is succeeded
- command: /bin/still/something_else
when: result is skipped
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 undefined
---
tasks:
- command: echo { item }
loop: [ 0, 2, 4, 6, 8, 10 ]
---
tasks:
- command: echo { item }
loop: [ 0, 2, 4, 6, 8, 10 ]
when: item > 5
- hosts: localhost
gather_facts: no
vars:
- ff: 1
- gg: 2
tasks:
- debug:
var: ff
roles:
- role: role_B
➜ lab-ansible ansible-playbook playbooks/roles_vars.yml
PLAY [localhost] ***************************************************************
TASK [role_B : debug] **********************************************************
ok: [localhost] => {
"a": 2
}
TASK [debug] *******************************************************************
ok: [localhost] => {
"ff": 1
}
PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
---
- hosts: localhost
gather_facts: no
vars:
- ff: 1
- gg: 2
pre_tasks:
- import_role:
name: role_A
vars:
age: 23
roles:
- role: role_B
tasks:
- debug:
var: ff
post_tasks:
- debug:
var: gg
- name: template configuration file
template:
src: template.j2
dest: /etc/foo.conf
notify:
- restart memcached
- restart apache
文章转载自IT运维那点事儿,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




