****
ok: [10.0.0.30]
TASK [shell] ******************************************************************************************************************************
changed: [10.0.0.30]
TASK [Get System Status] ******************************************************************************************************************
ok: [10.0.0.30] => {
"msg": [
"tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 925/sshd ",
"tcp6 0 0 :::22 :::* LISTEN 925/sshd "
]
}
PLAY RECAP ********************************************************************************************************************************
10.0.0.30 : ok=3 changed=1 unreachable=0 failed=0
4.Playbook条件语句
playbook中的条件判断语句使用when
[root@manager ~]# cat f6.yml
- hosts: all
remote_user: root
tasks:
- name: Create File
file: path=/tmp/this_is_{{ ansible_hostname }}_file state=touch
when: (ansible_hostname == "nfs") or (ansible_hostname == "backup")
#系统为centos的主机才会执行
- name: Centos Install httpd
yum: name=httpd state=present
when: (ansible_distribution == "CentOS")
#系统为ubuntu的主机才会执行
- name: Ubuntu Install httpd
yum: name=httpd2 state=present
when: (ansible_distribution == "Ubuntu")
#playbook执行结果:
[root@manager ~]# vim f6.yml
[root@manager ~]# ansible-playbook f6.yml
PLAY [all] ********************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************
ok: [10.0.0.30]
TASK [Create File] ************************************************************************************************************************
skipping: [10.0.0.30] #主机名不匹配则跳过, 匹配则会进行创建文件
PLAY RECAP ********************************************************************************************************************************
10.0.0.30 : ok=1 changed=0 unreachable=0 failed=0
5.Playbook循环语句
1、标准循环使用场景-批量安装软件
[root@manager ~]# cat f7.yml
---
- hosts: all
remote_user: root
tasks:
- name: Installed Pkg
yum: name={{ item }} state=present
with_items:
- wget
- tree
- lrzsz
#palybook执行结果
[root@manager ~]# ansible-playbook f7.yml
PLAY [all] ********************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************
ok: [10.0.0.30]
TASK [Installed Pkg] **********************************************************************************************************************
ok: [10.0.0.30] => (item=[u'wget', u'tree', u'lrzsz'])
PLAY RECAP ********************************************************************************************************************************
10.0.0.30 : ok=2 changed=0 unreachable=0 failed=0
2、标准循环使用场景-批量创建用户
[root@manager ~]# cat f7.yml
- hosts: all
remote_user: root
tasks:
- name: Add Users
user: name={{ item.name }} groups={{ item.groups }} state=present
with_items:
- { name: 'testuser1', groups: 'bin' }
- { name: 'testuser2', groups: 'r