ansible

ansible


真机配好ftp下的ansible的yum源,然后同步yum配置文件夹给虚拟机ansible

注意,传目录一定要加-r
rsync -r /etc/yum.repos.d/ 192.168.3.60:/etc/yum.repos.d/
#######################################################################
虚拟机ansible

配置好相应的虚拟机如下
[root@ansible ansible]# cat /etc/hosts
... ...
192.168.3.60 ansible
192.168.3.61 web1
192.168.3.62 web2
192.168.3.63 db1
192.168.3.64 db2
192.168.3.65 cache


# ansible --version
ansible 2.4.2.0
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]


# yum -y install ansible
# cd /etc/ansible/
# ls
ansible.cfg ansible.cfg.rpmsave hosts hosts.rpmsave roles

# vim ansible.cfg
inventory = /etc/ansible/hosts 打开14行的注释
host_key_checking = False 打开此行的注释,不检查远程连接时的yes和no

# vim hosts
追加
[web]
web1 可以这样写web[1:10],代表连续的虚拟机web1到wbe10
web2

[db]
db1
db2

[other]
cache


[root@ansible ansible]# ansible web --list-host
hosts (2):
web1
web2
[root@ansible ansible]# ansible web1 --list-host
hosts (1):
web1
[root@ansible ansible]# ansible web,db --list-host
hosts (4):
web1
web2
db1
db2
[root@ansible ansible]# ansible all --list-host
hosts (5):
web1
web2
cache
db1
db2
#######################################################################
虚拟机ansible

常见报错:测试是否可以连接,若失败颜色为红色
[root@ansible ansible]# ansible web -m ping 提示密码不对,无权限ssh登陆web1和web2
web1 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Warning: Permanently added 'web1' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n",
"unreachable": true
}
web2 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Warning: Permanently added 'web2' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n",
"unreachable": true
}

 

[root@ansible ansible]# ansible web -m ping -k 用-k来进行交互式操作
SSH password: 输入密码1(虚拟机web1和web2的密码是1)
web1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
web2 | SUCCESS => {
"changed": false,
"ping": "pong"
}


# vim ansible.cfg
[web]
web1 ansible_ssh_pass=1 给每台机器加上远程登陆的密码,就可以不用每次打密码
web2 ansible_ssh_pass=1

[db]
db1 ansible_ssh_pass=1
db2 ansible_ssh_pass=1

[other]
cache ansible_ssh_pass=1


不用密码就能ansible登陆了
[root@ansible ansible]# ansible all -m ping
web2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
db1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
db2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
web1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
cache | SUCCESS => {
"changed": false,
"ping": "pong"
}

 

常见报错:如果主机不存在
[root@ansible ansible]# ansible wb -m ping
[WARNING]: Could not match supplied host pattern, ignoring: wb

[WARNING]: No hosts matched, nothing to do

 

[root@ansible ansible]# cd /root/.ansible/cp/
[root@ansible cp]# ls
123025f244 3eee52bc5e 428c6135df 86427c36cb ae750fdd40
[root@ansible cp]# ll
总用量 0
srw------- 1 root root 0 12月 28 11:52 123025f244
srw------- 1 root root 0 12月 28 11:52 3eee52bc5e
srw------- 1 root root 0 12月 28 11:52 428c6135df
srw------- 1 root root 0 12月 28 11:52 86427c36cb
srw------- 1 root root 0 12月 28 11:52 ae750fdd40


[web]
web1 可以这样写web[1:10],代表连续的虚拟机web1到wbe10
web2

[db]
db1
db2

[other]
cache
#######################################################################
虚拟机ansible


mkdir /abc
cd /abc

vim ansible.cfg
[defaults]
inventory = myhosts
host_key_checking = False


vim myhosts
[app1] app1这个组里包含web1和db1
web1
db1

[app2] app2这个组里包含web2和db2
web2
db2

[app:children] app这组包含了app1和app2这2个子组(用children表示下面的是组而不是单个主机)
app1
app2

[app:vars] 设置远程登陆整个app组的所有主机时的密码
ansible_ssh_pass=1 要求所有主机的密码都是一样的才能使用这个配置项


[root@ansible abc]# ansible all --list-host
hosts (4):
web1
db1
web2
db2


[root@ansible abc]# ansible app -m ping
web2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
db1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
web1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
db2 | SUCCESS => {
"changed": false,
"ping": "pong"
}

 

-m和-a是最重要的2个参数,几乎都会用到
################################################################################
虚拟机ansible
写python文件

[root@ansible abc]# vim ansible.cfg
[defaults]
inventory = myhosts.py 改成python文件
host_key_checking = False


[root@ansible abc]# vim myhosts.py 编写python文件
#!/usr/bin/python

import json

hostlist={}
hostlist["app1"]=["web1","web2"]
hostlist["app2"]=["db1","db2","cache"]

print(json.dumps(hostlist))


[root@ansible abc]# chmod 755 myhosts.py 加执行权限

[root@ansible abc]# ./myhosts.py 试着运行看看
{"app2": ["db1", "db2", "cache"], "app1": ["web1", "web2"]}


[root@ansible abc]# ansible all --list-host 发现ansible也能用那个python文件了
hosts (5):
db1
db2
cache
web1
web2


[root@ansible abc]# ansible all -m command -a 'uptime' -k 交互时输入密码
显示如下:
SSH password: 输入密码1
db1 | SUCCESS | rc=0 >>
14:52:17 up 4:19, 3 users, load average: 0.00, 0.01, 0.03

db2 | SUCCESS | rc=0 >>
14:52:17 up 4:19, 3 users, load average: 0.00, 0.01, 0.03

web1 | SUCCESS | rc=0 >>
14:52:17 up 4:19, 3 users, load average: 0.00, 0.01, 0.04

cache | SUCCESS | rc=0 >>
14:52:17 up 4:19, 3 users, load average: 0.00, 0.01, 0.01

web2 | SUCCESS | rc=0 >>
14:52:17 up 4:19, 2 users, load average: 0.00, 0.01, 0.03
###########################################################################
虚拟机ansible

设置无密码登陆

[root@ansible abc]# cd /root/.ssh/
[root@ansible .ssh]# ls
known_hosts

[root@ansible .ssh]# ssh-keygen -t rsa -b 2048 -N '' 生成密钥
显示如下:
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:eT3DYi44Nqyci36uwyMXYqOiSgxBgG7QzqQvfxlj6U0 root@ansible
The key's randomart image is:
+---[RSA 2048]----+
|+o |
|+ o |
|+= |
|.+o . o |
|o. . S + = |
|=oo =.E. + . o |
|oB.+ ** . . |
|= *o=+.o . |
|*++B*. |
+----[SHA256]-----+


[root@ansible .ssh]# ls
id_rsa id_rsa.pub known_hosts 这里的.pub就是密钥文件


[root@ansible .ssh]# ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key='$(< /root/.ssh/id_rsa.pub)'" -k 交互式把密钥文件发到每台主机上
显示如下:
SSH password: 输入密码1
web2 | SUCCESS => {
"changed": true,
"comment": null,
"exclusive": true,
"key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDHD1GeOIpUaLz/a7J33Rvq3aY4eI1qSTX6m/OhghjjTaYy5ar5PonRgAWSqgJg97cU5o0cxnBeDtDei2o+MMJVFgG27ooS0AI3wWwUrquUBjKAkW5l9541A4r4C/rhUqMDFdRj97EJf7n2t4DWRC2aX1UAP9Qp/Pjg3Y0dbzCNPU2wT/8ZGBhIZ+6g85R5E6RzsAOHVCd5E0JQzQ29jbVEBCnNVNetita522qzApEU/z+/8D31w7U2U3niVA/9g/c5pk83zMeqv7CLWoozp7d+LYWcnJjmhr4i0kkfb+CgAP8F+W3ffZ4KEAmXnolGEJGIKE5L5EwwuEF1I7W3TQ41 root@ansible",
"key_options": null,
"keyfile": "/root/.ssh/authorized_keys",
"manage_dir": true,
"path": null,
"state": "present",
"unique": false,
"user": "root",
"validate_certs": true
}
cache | SUCCESS => {
"changed": true,
"comment": null,
"exclusive": true,
"key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDHD1GeOIpUaLz/a7J33Rvq3aY4eI1qSTX6m/OhghjjTaYy5ar5PonRgAWSqgJg97cU5o0cxnBeDtDei2o+MMJVFgG27ooS0AI3wWwUrquUBjKAkW5l9541A4r4C/rhUqMDFdRj97EJf7n2t4DWRC2aX1UAP9Qp/Pjg3Y0dbzCNPU2wT/8ZGBhIZ+6g85R5E6RzsAOHVCd5E0JQzQ29jbVEBCnNVNetita522qzApEU/z+/8D31w7U2U3niVA/9g/c5pk83zMeqv7CLWoozp7d+LYWcnJjmhr4i0kkfb+CgAP8F+W3ffZ4KEAmXnolGEJGIKE5L5EwwuEF1I7W3TQ41 root@ansible",
"key_options": null,
"keyfile": "/root/.ssh/authorized_keys",
"manage_dir": true,
"path": null,
"state": "present",
"unique": false,
"user": "root",
"validate_certs": true
}
db2 | SUCCESS => {
"changed": true,
"comment": null,
"exclusive": true,
"key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDHD1GeOIpUaLz/a7J33Rvq3aY4eI1qSTX6m/OhghjjTaYy5ar5PonRgAWSqgJg97cU5o0cxnBeDtDei2o+MMJVFgG27ooS0AI3wWwUrquUBjKAkW5l9541A4r4C/rhUqMDFdRj97EJf7n2t4DWRC2aX1UAP9Qp/Pjg3Y0dbzCNPU2wT/8ZGBhIZ+6g85R5E6RzsAOHVCd5E0JQzQ29jbVEBCnNVNetita522qzApEU/z+/8D31w7U2U3niVA/9g/c5pk83zMeqv7CLWoozp7d+LYWcnJjmhr4i0kkfb+CgAP8F+W3ffZ4KEAmXnolGEJGIKE5L5EwwuEF1I7W3TQ41 root@ansible",
"key_options": null,
"keyfile": "/root/.ssh/authorized_keys",
"manage_dir": true,
"path": null,
"state": "present",
"unique": false,
"user": "root",
"validate_certs": true
}
db1 | SUCCESS => {
"changed": true,
"comment": null,
"exclusive": true,
"key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDHD1GeOIpUaLz/a7J33Rvq3aY4eI1qSTX6m/OhghjjTaYy5ar5PonRgAWSqgJg97cU5o0cxnBeDtDei2o+MMJVFgG27ooS0AI3wWwUrquUBjKAkW5l9541A4r4C/rhUqMDFdRj97EJf7n2t4DWRC2aX1UAP9Qp/Pjg3Y0dbzCNPU2wT/8ZGBhIZ+6g85R5E6RzsAOHVCd5E0JQzQ29jbVEBCnNVNetita522qzApEU/z+/8D31w7U2U3niVA/9g/c5pk83zMeqv7CLWoozp7d+LYWcnJjmhr4i0kkfb+CgAP8F+W3ffZ4KEAmXnolGEJGIKE5L5EwwuEF1I7W3TQ41 root@ansible",
"key_options": null,
"keyfile": "/root/.ssh/authorized_keys",
"manage_dir": true,
"path": null,
"state": "present",
"unique": false,
"user": "root",
"validate_certs": true
}
web1 | SUCCESS => {
"changed": true,
"comment": null,
"exclusive": true,
"key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDHD1GeOIpUaLz/a7J33Rvq3aY4eI1qSTX6m/OhghjjTaYy5ar5PonRgAWSqgJg97cU5o0cxnBeDtDei2o+MMJVFgG27ooS0AI3wWwUrquUBjKAkW5l9541A4r4C/rhUqMDFdRj97EJf7n2t4DWRC2aX1UAP9Qp/Pjg3Y0dbzCNPU2wT/8ZGBhIZ+6g85R5E6RzsAOHVCd5E0JQzQ29jbVEBCnNVNetita522qzApEU/z+/8D31w7U2U3niVA/9g/c5pk83zMeqv7CLWoozp7d+LYWcnJjmhr4i0kkfb+CgAP8F+W3ffZ4KEAmXnolGEJGIKE5L5EwwuEF1I7W3TQ41 root@ansible",
"key_options": null,
"keyfile": "/root/.ssh/authorized_keys",
"manage_dir": true,
"path": null,
"state": "present",
"unique": false,
"user": "root",
"validate_certs": true
}


[root@ansible .ssh]# ansible all -m command -a 'uptime' 能无密码使用ansible了
显示如下:
web2 | SUCCESS | rc=0 >>
15:08:39 up 4:35, 2 users, load average: 0.00, 0.01, 0.03

db1 | SUCCESS | rc=0 >>
15:08:39 up 4:35, 3 users, load average: 0.00, 0.01, 0.04

cache | SUCCESS | rc=0 >>
15:08:39 up 4:35, 3 users, load average: 0.00, 0.01, 0.02

web1 | SUCCESS | rc=0 >>
15:08:39 up 4:35, 3 users, load average: 0.00, 0.01, 0.04

db2 | SUCCESS | rc=0 >>
15:08:39 up 4:35, 3 users, load average: 0.00, 0.01, 0.03
#########################################################################3

[root@ansible abc]# userdel --help
用法:userdel [选项] 登录

选项:
-f, --force force some actions that would fail otherwise
e.g. removal of user still logged in
or files, even if not owned by the user
-h, --help 显示此帮助信息并推出
-r, --remove 删除主目录和邮件池
-R, --root CHROOT_DIR chroot 到的目录
-Z, --selinux-user 为用户删除所有的 SELinux 用户映射

[root@ansible abc]# ansible web1,db2 -m shell -a 'userdel -r z3'
db2 | SUCCESS | rc=0 >>


web1 | SUCCESS | rc=0 >>


[root@ansible abc]# ansible web1,db2 -m shell -a 'useradd z3;echo 123 | passwd --stdin z3'
web1 | SUCCESS | rc=0 >>
更改用户 z3 的密码 。
passwd:所有的身份验证令牌已经成功更新。

db2 | SUCCESS | rc=0 >>
更改用户 z3 的密码 。
passwd:所有的身份验证令牌已经成功更新。


[root@ansible abc]# ansible web1,db2 -m shell -a 'chage -d 0 z3'
db2 | SUCCESS | rc=0 >>


web1 | SUCCESS | rc=0 >>
#######################################################################

[root@ansible ansible]# cat useradd.sh
#!/bin/bash
id z3
if [ $? != 0 ];then
useradd li4
echo 321 | passwd --stdin li4
fi
[root@ansible ansible]# pwd
/etc/ansible


[root@ansible ansible]# ./useradd.sh
id: z3: no such user
更改用户 li4 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@ansible ansible]# ansible web -m script -a './useradd.sh'
web1 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to web1 closed.\r\n",
"stdout": "uid=1000(z3) gid=1000(z3) 组=1000(z3)\r\n",
"stdout_lines": [
"uid=1000(z3) gid=1000(z3) 组=1000(z3)"
]
}
web2 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to web2 closed.\r\n",
"stdout": "id: z3: no such user\r\n更改用户 li4 的密码 。\r\npasswd:所有的身份验证令牌已经成功更新。\r\n",
"stdout_lines": [
"id: z3: no such user",
"更改用户 li4 的密码 。",
"passwd:所有的身份验证令牌已经成功更新。"
]
}


主机web1
[root@web1 home]# id w5
uid=1001(w5) gid=1001(w5) 组=1001(w5)

 

[root@ansible ansible]# vim /etc/resolv.conf
显示如下:
; generated by /usr/sbin/dhclient-script
nameserver 114.114.114.114
search localdomain
[root@ansible ansible]# ansible all -m copy -a 'src=/etc/resolv.conf dest=/etc/resolv.con'
db1 | SUCCESS => {
"changed": true,
"checksum": "ff7933bc09d04d348269ff8ee5f1b8e23b40baf3",
"dest": "/etc/resolv.con",
"gid": 0,
"group": "root",
"md5sum": "755490aa03bbd8d1b68121618dfc392d",
"mode": "0644",
"owner": "root",
"size": 87,
"src": "/root/.ansible/tmp/ansible-tmp-1545986508.74-173215833497992/source",
"state": "file",
"uid": 0
}
web1 | SUCCESS => {
"changed": true,
"checksum": "ff7933bc09d04d348269ff8ee5f1b8e23b40baf3",
"dest": "/etc/resolv.con",
"gid": 0,
"group": "root",
"md5sum": "755490aa03bbd8d1b68121618dfc392d",
"mode": "0644",
"owner": "root",
"size": 87,
"src": "/root/.ansible/tmp/ansible-tmp-1545986508.7-246227410718560/source",
"state": "file",
"uid": 0
}
cache | SUCCESS => {
"changed": true,
"checksum": "ff7933bc09d04d348269ff8ee5f1b8e23b40baf3",
"dest": "/etc/resolv.con",
"gid": 0,
"group": "root",
"md5sum": "755490aa03bbd8d1b68121618dfc392d",
"mode": "0644",
"owner": "root",
"size": 87,
"src": "/root/.ansible/tmp/ansible-tmp-1545986508.75-69181702923641/source",
"state": "file",
"uid": 0
}
db2 | SUCCESS => {
"changed": true,
"checksum": "ff7933bc09d04d348269ff8ee5f1b8e23b40baf3",
"dest": "/etc/resolv.con",
"gid": 0,
"group": "root",
"md5sum": "755490aa03bbd8d1b68121618dfc392d",
"mode": "0644",
"owner": "root",
"size": 87,
"src": "/root/.ansible/tmp/ansible-tmp-1545986508.75-201646722005862/source",
"state": "file",
"uid": 0
}
web2 | SUCCESS => {
"changed": true,
"checksum": "ff7933bc09d04d348269ff8ee5f1b8e23b40baf3",
"dest": "/etc/resolv.con",
"gid": 0,
"group": "root",
"md5sum": "755490aa03bbd8d1b68121618dfc392d",
"mode": "0644",
"owner": "root",
"size": 87,
"src": "/root/.ansible/tmp/ansible-tmp-1545986508.72-93713161627468/source",
"state": "file",
"uid": 0
}

 

[root@ansible ansible]# ansible all -m shell -a 'cat /etc/resolv.conf'
显示如下:
web2 | SUCCESS | rc=0 >>
; generated by /usr/sbin/dhclient-script
nameserver 192.168.3.254
search localdomain

db2 | SUCCESS | rc=0 >>
; generated by /usr/sbin/dhclient-script
nameserver 192.168.3.254
search localdomain

cache | SUCCESS | rc=0 >>
; generated by /usr/sbin/dhclient-script
nameserver 192.168.3.254
search localdomain

web1 | SUCCESS | rc=0 >>
; generated by /usr/sbin/dhclient-script
nameserver 192.168.3.254
search localdomain

db1 | SUCCESS | rc=0 >>
; generated by /usr/sbin/dhclient-script
nameserver 192.168.3.254
search localdomain

 

[root@ansible ansible]# ansible cache -m lineinfile -a 'path="/etc/sysconfig/network-scripts/ifcfg-eth0" regexp="^ONBOOT" line="ONBOOT=\"no\"" '
cache | SUCCESS => {
"backup": "",
"changed": true,
"msg": "line replaced"
}

[root@ansible ansible]# ansible cache -m shell -a 'grep -P "^ONBOOT" /etc/sysconfig/network-scripts/ifcfg-eth0'
cache | SUCCESS | rc=0 >>
ONBOOT="no"


[root@ansible ansible]# ansible cache -m replace -a 'path="/etc/sysconfig/network-scripts/ifcfg-eth0" regexp="(ONBOOT).*" replace="\1=\"yes\""'
cache | SUCCESS => {
"changed": true,
"msg": "1 replacements made"
}

[root@ansible ansible]# ansible cache -m shell -a 'grep -P "^ONBOOT" /etc/sysconfig/network-scripts/ifcfg-eth0'cache | SUCCESS | rc=0 >>
ONBOOT="yes"


打错模块或路径时,会报错
[root@ansible ansible]# ansible cache -m lineninfile -a 'path="/etc/sysconfig/network-scripts/ifcfg-eth0" regexp="^ONBOOT" line="ONBOOT=\"no\"" '
cache | FAILED! => {
"msg": "The module lineninfile was not found in configured module paths. Additionally, core modules are missing. If this is a checkout, run 'git pull --rebase' to correct this problem."
}

说模块lineninfile没有找到,认真看会发现中间是打错了,多了一个n


[root@ansible ansible]# ansible cache -m replace -a 'path="/etc/sysconfig/network-scripts/ifcfg-eht0" regexp="(ONBOOT).*" replace="\1=\"yes\""'
cache | FAILED! => {
"changed": false,
"msg": "Path /etc/sysconfig/network-scripts/ifcfg-eht0 does not exist !",
"rc": 257
}

说文件不存在,原来路径写错成eht0了,应该是eth0
############################################################################

[root@ansible ansible]# ansible db -m yum -a 'name=telnet state=installed'
db2 | SUCCESS => {
"changed": true,
"msg": "Repository centos is listed more than once in the configuration\n",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\nResolving Dependencies\n--> Running transaction check\n---> Package telnet.x86_64 1:0.17-64.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n telnet x86_64 1:0.17-64.el7 centos 64 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 64 k\nInstalled size: 113 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : 1:telnet-0.17-64.el7.x86_64 1/1 \n Verifying : 1:telnet-0.17-64.el7.x86_64 1/1 \n\nInstalled:\n telnet.x86_64 1:0.17-64.el7 \n\nComplete!\n"
]
}
db1 | SUCCESS => {
"changed": true,
"msg": "Repository centos is listed more than once in the configuration\n",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\nResolving Dependencies\n--> Running transaction check\n---> Package telnet.x86_64 1:0.17-64.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n telnet x86_64 1:0.17-64.el7 centos 64 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 64 k\nInstalled size: 113 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : 1:telnet-0.17-64.el7.x86_64 1/1 \n Verifying : 1:telnet-0.17-64.el7.x86_64 1/1 \n\nInstalled:\n telnet.x86_64 1:0.17-64.el7 \n\nComplete!\n"
]
}

 

db1或db1,验证已经安装成功
[root@db1 ~]# telnet
telnet> help
Commands may be abbreviated. Commands are:

close close current connection
logout forcibly logout remote user and close the connection
display display operating parameters
mode try to enter line or character mode ('mode ?' for more)
open connect to a site
quit exit telnet
send transmit special characters ('send ?' for more)
set set operating parameters ('set ?' for more)
unset unset operating parameters ('unset ?' for more)
status print status information
toggle toggle operating parameters ('toggle ?' for more)
slc change state of special charaters ('slc ?' for more)
z suspend telnet
! invoke a subshell
environ change environment variables ('environ ?' for more)
? print help information
telnet> close
?Need to be connected first.
telnet> quit
####################################################################
yum和service模块


yum
name 包1,包2,包3
state 动作
install ==> installed 后面要变成ed结尾
remove ==> removed


service 启动或关闭服务,设置开机自启
name 服务名
state 动作
enable yes|no

 

[root@cache ~]# ss -tunlp 发现时间同步服务端口323开启了
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
udp UNCONN 0 0 127.0.0.1:323 *:* users:(("chronyd",pid=470,fd=1))
udp UNCONN 0 0 ::1:323 :::* users:(("chronyd",pid=470,fd=2))
... ...


[root@cache ~]# systemctl is-enabled chronyd 查看状态是开启自启
enabled


[root@ansible ansible]# ansible cache -m service -a 'name=chronyd state=stopped enabled=no'
显示如下:
cache | SUCCESS => {
"changed": true,
"enabled": false,
"name": "chronyd",
"state": "stopped",
"status": {
"ActiveEnterTimestamp": "五 2018-12-28 23:39:07 CST",
"ActiveEnterTimestampMonotonic": "25290604",
"ActiveExitTimestampMonotonic": "0",
"ActiveState": "active",
"After": "sntp.service basic.target system.slice tmp.mount -.mount ntpdate.service systemd-journald.socket ntpd.service",
"AllowIsolate": "no",
"AmbientCapabilities": "0",
"AssertResult": "yes",
"AssertTimestamp": "五 2018-12-28 23:39:06 CST",
"AssertTimestampMonotonic": "24735564",
"Before": "shutdown.target multi-user.target",
"BlockIOAccounting": "no",
"BlockIOWeight": "18446744073709551615",
"CPUAccounting": "no",
"CPUQuotaPerSecUSec": "infinity",
"CPUSchedulingPolicy": "0",
"CPUSchedulingPriority": "0",
"CPUSchedulingResetOnFork": "no",
"CPUShares": "18446744073709551615",
"CanIsolate": "no",
"CanReload": "no",
"CanStart": "yes",
"CanStop": "yes",
"CapabilityBoundingSet": "18446744073709551615",
"ConditionResult": "yes",
"ConditionTimestamp": "五 2018-12-28 23:39:06 CST",
"ConditionTimestampMonotonic": "24735458",
"Conflicts": "systemd-timesyncd.service shutdown.target ntpd.service",
"ControlGroup": "/system.slice/chronyd.service",
"ControlPID": "0",
"DefaultDependencies": "yes",
"Delegate": "no",
"Description": "NTP client/server",
"DevicePolicy": "auto",
"Documentation": "man:chronyd(8) man:chrony.conf(5)",
"EnvironmentFile": "/etc/sysconfig/chronyd (ignore_errors=yes)",
"ExecMainCode": "0",
"ExecMainExitTimestampMonotonic": "0",
"ExecMainPID": "470",
"ExecMainStartTimestamp": "五 2018-12-28 23:39:07 CST",
"ExecMainStartTimestampMonotonic": "25084612",
"ExecMainStatus": "0",
"ExecStart": "{ path=/usr/sbin/chronyd ; argv[]=/usr/sbin/chronyd $OPTIONS ; ignore_errors=no ; start_time=[五 2018-12-28 23:39:06 CST] ; stop_time=[五 2018-12-28 23:39:07 CST] ; pid=466 ; code=exited ; status=0 }",
"ExecStartPost": "{ path=/usr/libexec/chrony-helper ; argv[]=/usr/libexec/chrony-helper update-daemon ; ignore_errors=no ; start_time=[五 2018-12-28 23:39:07 CST] ; stop_time=[五 2018-12-28 23:39:07 CST] ; pid=478 ; code=exited ; status=0 }",
"FailureAction": "none",
"FileDescriptorStoreMax": "0",
"FragmentPath": "/usr/lib/systemd/system/chronyd.service",
"GuessMainPID": "yes",
"IOScheduling": "0",
"Id": "chronyd.service",
"IgnoreOnIsolate": "no",
"IgnoreOnSnapshot": "no",
"IgnoreSIGPIPE": "yes",
"InactiveEnterTimestampMonotonic": "0",
"InactiveExitTimestamp": "五 2018-12-28 23:39:06 CST",
"InactiveExitTimestampMonotonic": "24850599",
"JobTimeoutAction": "none",
"JobTimeoutUSec": "0",
"KillMode": "control-group",
"KillSignal": "15",
"LimitAS": "18446744073709551615",
"LimitCORE": "18446744073709551615",
"LimitCPU": "18446744073709551615",
"LimitDATA": "18446744073709551615",
"LimitFSIZE": "18446744073709551615",
"LimitLOCKS": "18446744073709551615",
"LimitMEMLOCK": "65536",
"LimitMSGQUEUE": "819200",
"LimitNICE": "0",
"LimitNOFILE": "4096",
"LimitNPROC": "7371",
"LimitRSS": "18446744073709551615",
"LimitRTPRIO": "0",
"LimitRTTIME": "18446744073709551615",
"LimitSIGPENDING": "7371",
"LimitSTACK": "18446744073709551615",
"LoadState": "loaded",
"MainPID": "470",
"MemoryAccounting": "no",
"MemoryCurrent": "18446744073709551615",
"MemoryLimit": "18446744073709551615",
"MountFlags": "0",
"Names": "chronyd.service",
"NeedDaemonReload": "no",
"Nice": "0",
"NoNewPrivileges": "no",
"NonBlocking": "no",
"NotifyAccess": "none",
"OOMScoreAdjust": "0",
"OnFailureJobMode": "replace",
"PIDFile": "/var/run/chronyd.pid",
"PermissionsStartOnly": "no",
"PrivateDevices": "no",
"PrivateNetwork": "no",
"PrivateTmp": "yes",
"ProtectHome": "yes",
"ProtectSystem": "full",
"RefuseManualStart": "no",
"RefuseManualStop": "no",
"RemainAfterExit": "no",
"Requires": "-.mount basic.target",
"RequiresMountsFor": "/var/tmp",
"Restart": "no",
"RestartUSec": "100ms",
"Result": "success",
"RootDirectoryStartOnly": "no",
"RuntimeDirectoryMode": "0755",
"SameProcessGroup": "no",
"SecureBits": "0",
"SendSIGHUP": "no",
"SendSIGKILL": "yes",
"Slice": "system.slice",
"StandardError": "inherit",
"StandardInput": "null",
"StandardOutput": "journal",
"StartLimitAction": "none",
"StartLimitBurst": "5",
"StartLimitInterval": "10000000",
"StartupBlockIOWeight": "18446744073709551615",
"StartupCPUShares": "18446744073709551615",
"StatusErrno": "0",
"StopWhenUnneeded": "no",
"SubState": "running",
"SyslogLevelPrefix": "yes",
"SyslogPriority": "30",
"SystemCallErrorNumber": "0",
"TTYReset": "no",
"TTYVHangup": "no",
"TTYVTDisallocate": "no",
"TasksAccounting": "no",
"TasksCurrent": "18446744073709551615",
"TasksMax": "18446744073709551615",
"TimeoutStartUSec": "1min 30s",
"TimeoutStopUSec": "1min 30s",
"TimerSlackNSec": "50000",
"Transient": "no",
"Type": "forking",
"UMask": "0022",
"UnitFilePreset": "enabled",
"UnitFileState": "enabled",
"WantedBy": "multi-user.target",
"Wants": "system.slice",
"WatchdogTimestamp": "五 2018-12-28 23:39:07 CST",
"WatchdogTimestampMonotonic": "25084636",
"WatchdogUSec": "0"
}
}


[root@cache ~]# systemctl is-enabled chronyd 查看状态是开启不启动
disabled

[root@cache ~]# ss -tunlp 没有时间服务323端口了
##############################################################################
安装apache,设置默认端口8080,启动服务,开机自启,默认首页hello world


[root@ansible ansible]# ansible web --list-host
hosts (2):
web1
web2


[root@ansible ansible]# ansible web -m yum -a 'name=httpd state=installed'
web2 | SUCCESS => {
"changed": true,
"msg": "Repository centos is listed more than once in the configuration\n",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-67.el7.centos will be installed\n--> Processing Dependency: httpd-tools = 2.4.6-67.el7.centos for package: httpd-2.4.6-67.el7.centos.x86_64\n--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-67.el7.centos.x86_64\n--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-67.el7.centos.x86_64\n--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-67.el7.centos.x86_64\n--> Running transaction check\n---> Package apr.x86_64 0:1.4.8-3.el7 will be installed\n---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed\n---> Package httpd-tools.x86_64 0:2.4.6-67.el7.centos will be installed\n---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.4.6-67.el7.centos centos 2.7 M\nInstalling for dependencies:\n apr x86_64 1.4.8-3.el7 centos 103 k\n apr-util x86_64 1.5.2-6.el7 centos 92 k\n httpd-tools x86_64 2.4.6-67.el7.centos centos 87 k\n mailcap noarch 2.1.41-2.el7 centos 31 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package (+4 Dependent packages)\n\nTotal download size: 3.0 M\nInstalled size: 10 M\nDownloading packages:\n--------------------------------------------------------------------------------\nTotal 18 MB/s | 3.0 MB 00:00 \nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : apr-1.4.8-3.el7.x86_64 1/5 \n Installing : apr-util-1.5.2-6.el7.x86_64 2/5 \n Installing : httpd-tools-2.4.6-67.el7.centos.x86_64 3/5 \n Installing : mailcap-2.1.41-2.el7.noarch 4/5 \n Installing : httpd-2.4.6-67.el7.centos.x86_64 5/5 \n Verifying : httpd-2.4.6-67.el7.centos.x86_64 1/5 \n Verifying : mailcap-2.1.41-2.el7.noarch 2/5 \n Verifying : apr-1.4.8-3.el7.x86_64 3/5 \n Verifying : httpd-tools-2.4.6-67.el7.centos.x86_64 4/5 \n Verifying : apr-util-1.5.2-6.el7.x86_64 5/5 \n\nInstalled:\n httpd.x86_64 0:2.4.6-67.el7.centos \n\nDependency Installed:\n apr.x86_64 0:1.4.8-3.el7 apr-util.x86_64 0:1.5.2-6.el7 \n httpd-tools.x86_64 0:2.4.6-67.el7.centos mailcap.noarch 0:2.1.41-2.el7 \n\nComplete!\n"
]
}
web1 | SUCCESS => {
"changed": true,
"msg": "Repository centos is listed more than once in the configuration\n",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-67.el7.centos will be installed\n--> Processing Dependency: httpd-tools = 2.4.6-67.el7.centos for package: httpd-2.4.6-67.el7.centos.x86_64\n--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-67.el7.centos.x86_64\n--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-67.el7.centos.x86_64\n--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-67.el7.centos.x86_64\n--> Running transaction check\n---> Package apr.x86_64 0:1.4.8-3.el7 will be installed\n---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed\n---> Package httpd-tools.x86_64 0:2.4.6-67.el7.centos will be installed\n---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.4.6-67.el7.centos centos 2.7 M\nInstalling for dependencies:\n apr x86_64 1.4.8-3.el7 centos 103 k\n apr-util x86_64 1.5.2-6.el7 centos 92 k\n httpd-tools x86_64 2.4.6-67.el7.centos centos 87 k\n mailcap noarch 2.1.41-2.el7 centos 31 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package (+4 Dependent packages)\n\nTotal download size: 3.0 M\nInstalled size: 10 M\nDownloading packages:\n--------------------------------------------------------------------------------\nTotal 25 MB/s | 3.0 MB 00:00 \nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : apr-1.4.8-3.el7.x86_64 1/5 \n Installing : apr-util-1.5.2-6.el7.x86_64 2/5 \n Installing : httpd-tools-2.4.6-67.el7.centos.x86_64 3/5 \n Installing : mailcap-2.1.41-2.el7.noarch 4/5 \n Installing : httpd-2.4.6-67.el7.centos.x86_64 5/5 \n Verifying : httpd-2.4.6-67.el7.centos.x86_64 1/5 \n Verifying : mailcap-2.1.41-2.el7.noarch 2/5 \n Verifying : apr-1.4.8-3.el7.x86_64 3/5 \n Verifying : httpd-tools-2.4.6-67.el7.centos.x86_64 4/5 \n Verifying : apr-util-1.5.2-6.el7.x86_64 5/5 \n\nInstalled:\n httpd.x86_64 0:2.4.6-67.el7.centos \n\nDependency Installed:\n apr.x86_64 0:1.4.8-3.el7 apr-util.x86_64 0:1.5.2-6.el7 \n httpd-tools.x86_64 0:2.4.6-67.el7.centos mailcap.noarch 0:2.1.41-2.el7 \n\nComplete!\n"
]
}

 

[root@ansible ansible]# ansible web -m lineinfile -a 'path="/etc/httpd/conf/httpd.conf" regexp="^Listen" line="Listen 8080"'
web1 | SUCCESS => {
"backup": "",
"changed": true,
"msg": "line replaced"
}
web2 | SUCCESS => {
"backup": "",
"changed": true,
"msg": "line replaced"
}
[root@ansible ansible]# ansible web -m shell -a 'grep -P "^Listen" /etc/httpd/conf/httpd.conf'
web2 | SUCCESS | rc=0 >>
Listen 8080

web1 | SUCCESS | rc=0 >>
Listen 8080

[root@ansible ansible]# ansible web -m service -a 'name=httpd state=started enabled=yes'
web1 | SUCCESS => {
"changed": true,
"enabled": true,
"name": "httpd",
"state": "started",
"status": {
"ActiveEnterTimestampMonotonic": "0",
"ActiveExitTimestampMonotonic": "0",
"ActiveState": "inactive",
"After": "systemd-journald.socket basic.target system.slice tmp.mount -.mount network.target remote-fs.target nss-lookup.target",
"AllowIsolate": "no",
"AmbientCapabilities": "0",
"AssertResult": "no",
"AssertTimestampMonotonic": "0",
"Before": "shutdown.target",
"BlockIOAccounting": "no",
"BlockIOWeight": "18446744073709551615",
"CPUAccounting": "no",
"CPUQuotaPerSecUSec": "infinity",
"CPUSchedulingPolicy": "0",
"CPUSchedulingPriority": "0",
"CPUSchedulingResetOnFork": "no",
"CPUShares": "18446744073709551615",
"CanIsolate": "no",
"CanReload": "yes",
"CanStart": "yes",
"CanStop": "yes",
"CapabilityBoundingSet": "18446744073709551615",
"ConditionResult": "no",
"ConditionTimestampMonotonic": "0",
"Conflicts": "shutdown.target",
"ControlPID": "0",
"DefaultDependencies": "yes",
"Delegate": "no",
"Description": "The Apache HTTP Server",
"DevicePolicy": "auto",
"Documentation": "man:httpd(8) man:apachectl(8)",
"EnvironmentFile": "/etc/sysconfig/httpd (ignore_errors=no)",
"ExecMainCode": "0",
"ExecMainExitTimestampMonotonic": "0",
"ExecMainPID": "0",
"ExecMainStartTimestampMonotonic": "0",
"ExecMainStatus": "0",
"ExecReload": "{ path=/usr/sbin/httpd ; argv[]=/usr/sbin/httpd $OPTIONS -k graceful ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
"ExecStart": "{ path=/usr/sbin/httpd ; argv[]=/usr/sbin/httpd $OPTIONS -DFOREGROUND ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
"ExecStop": "{ path=/bin/kill ; argv[]=/bin/kill -WINCH ${MAINPID} ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
"FailureAction": "none",
"FileDescriptorStoreMax": "0",
"FragmentPath": "/usr/lib/systemd/system/httpd.service",
"GuessMainPID": "yes",
"IOScheduling": "0",
"Id": "httpd.service",
"IgnoreOnIsolate": "no",
"IgnoreOnSnapshot": "no",
"IgnoreSIGPIPE": "yes",
"InactiveEnterTimestampMonotonic": "0",
"InactiveExitTimestampMonotonic": "0",
"JobTimeoutAction": "none",
"JobTimeoutUSec": "0",
"KillMode": "control-group",
"KillSignal": "18",
"LimitAS": "18446744073709551615",
"LimitCORE": "18446744073709551615",
"LimitCPU": "18446744073709551615",
"LimitDATA": "18446744073709551615",
"LimitFSIZE": "18446744073709551615",
"LimitLOCKS": "18446744073709551615",
"LimitMEMLOCK": "65536",
"LimitMSGQUEUE": "819200",
"LimitNICE": "0",
"LimitNOFILE": "4096",
"LimitNPROC": "7371",
"LimitRSS": "18446744073709551615",
"LimitRTPRIO": "0",
"LimitRTTIME": "18446744073709551615",
"LimitSIGPENDING": "7371",
"LimitSTACK": "18446744073709551615",
"LoadState": "loaded",
"MainPID": "0",
"MemoryAccounting": "no",
"MemoryCurrent": "18446744073709551615",
"MemoryLimit": "18446744073709551615",
"MountFlags": "0",
"Names": "httpd.service",
"NeedDaemonReload": "no",
"Nice": "0",
"NoNewPrivileges": "no",
"NonBlocking": "no",
"NotifyAccess": "main",
"OOMScoreAdjust": "0",
"OnFailureJobMode": "replace",
"PermissionsStartOnly": "no",
"PrivateDevices": "no",
"PrivateNetwork": "no",
"PrivateTmp": "yes",
"ProtectHome": "no",
"ProtectSystem": "no",
"RefuseManualStart": "no",
"RefuseManualStop": "no",
"RemainAfterExit": "no",
"Requires": "basic.target -.mount",
"RequiresMountsFor": "/var/tmp",
"Restart": "no",
"RestartUSec": "100ms",
"Result": "success",
"RootDirectoryStartOnly": "no",
"RuntimeDirectoryMode": "0755",
"SameProcessGroup": "no",
"SecureBits": "0",
"SendSIGHUP": "no",
"SendSIGKILL": "yes",
"Slice": "system.slice",
"StandardError": "inherit",
"StandardInput": "null",
"StandardOutput": "journal",
"StartLimitAction": "none",
"StartLimitBurst": "5",
"StartLimitInterval": "10000000",
"StartupBlockIOWeight": "18446744073709551615",
"StartupCPUShares": "18446744073709551615",
"StatusErrno": "0",
"StopWhenUnneeded": "no",
"SubState": "dead",
"SyslogLevelPrefix": "yes",
"SyslogPriority": "30",
"SystemCallErrorNumber": "0",
"TTYReset": "no",
"TTYVHangup": "no",
"TTYVTDisallocate": "no",
"TasksAccounting": "no",
"TasksCurrent": "18446744073709551615",
"TasksMax": "18446744073709551615",
"TimeoutStartUSec": "1min 30s",
"TimeoutStopUSec": "1min 30s",
"TimerSlackNSec": "50000",
"Transient": "no",
"Type": "notify",
"UMask": "0022",
"UnitFilePreset": "disabled",
"UnitFileState": "disabled",
"Wants": "system.slice",
"WatchdogTimestampMonotonic": "0",
"WatchdogUSec": "0"
}
}
web2 | SUCCESS => {
"changed": true,
"enabled": true,
"name": "httpd",
"state": "started",
"status": {
"ActiveEnterTimestampMonotonic": "0",
"ActiveExitTimestampMonotonic": "0",
"ActiveState": "inactive",
"After": "system.slice -.mount remote-fs.target basic.target network.target systemd-journald.socket tmp.mount nss-lookup.target",
"AllowIsolate": "no",
"AmbientCapabilities": "0",
"AssertResult": "no",
"AssertTimestampMonotonic": "0",
"Before": "shutdown.target",
"BlockIOAccounting": "no",
"BlockIOWeight": "18446744073709551615",
"CPUAccounting": "no",
"CPUQuotaPerSecUSec": "infinity",
"CPUSchedulingPolicy": "0",
"CPUSchedulingPriority": "0",
"CPUSchedulingResetOnFork": "no",
"CPUShares": "18446744073709551615",
"CanIsolate": "no",
"CanReload": "yes",
"CanStart": "yes",
"CanStop": "yes",
"CapabilityBoundingSet": "18446744073709551615",
"ConditionResult": "no",
"ConditionTimestampMonotonic": "0",
"Conflicts": "shutdown.target",
"ControlPID": "0",
"DefaultDependencies": "yes",
"Delegate": "no",
"Description": "The Apache HTTP Server",
"DevicePolicy": "auto",
"Documentation": "man:httpd(8) man:apachectl(8)",
"EnvironmentFile": "/etc/sysconfig/httpd (ignore_errors=no)",
"ExecMainCode": "0",
"ExecMainExitTimestampMonotonic": "0",
"ExecMainPID": "0",
"ExecMainStartTimestampMonotonic": "0",
"ExecMainStatus": "0",
"ExecReload": "{ path=/usr/sbin/httpd ; argv[]=/usr/sbin/httpd $OPTIONS -k graceful ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
"ExecStart": "{ path=/usr/sbin/httpd ; argv[]=/usr/sbin/httpd $OPTIONS -DFOREGROUND ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
"ExecStop": "{ path=/bin/kill ; argv[]=/bin/kill -WINCH ${MAINPID} ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
"FailureAction": "none",
"FileDescriptorStoreMax": "0",
"FragmentPath": "/usr/lib/systemd/system/httpd.service",
"GuessMainPID": "yes",
"IOScheduling": "0",
"Id": "httpd.service",
"IgnoreOnIsolate": "no",
"IgnoreOnSnapshot": "no",
"IgnoreSIGPIPE": "yes",
"InactiveEnterTimestampMonotonic": "0",
"InactiveExitTimestampMonotonic": "0",
"JobTimeoutAction": "none",
"JobTimeoutUSec": "0",
"KillMode": "control-group",
"KillSignal": "18",
"LimitAS": "18446744073709551615",
"LimitCORE": "18446744073709551615",
"LimitCPU": "18446744073709551615",
"LimitDATA": "18446744073709551615",
"LimitFSIZE": "18446744073709551615",
"LimitLOCKS": "18446744073709551615",
"LimitMEMLOCK": "65536",
"LimitMSGQUEUE": "819200",
"LimitNICE": "0",
"LimitNOFILE": "4096",
"LimitNPROC": "7371",
"LimitRSS": "18446744073709551615",
"LimitRTPRIO": "0",
"LimitRTTIME": "18446744073709551615",
"LimitSIGPENDING": "7371",
"LimitSTACK": "18446744073709551615",
"LoadState": "loaded",
"MainPID": "0",
"MemoryAccounting": "no",
"MemoryCurrent": "18446744073709551615",
"MemoryLimit": "18446744073709551615",
"MountFlags": "0",
"Names": "httpd.service",
"NeedDaemonReload": "no",
"Nice": "0",
"NoNewPrivileges": "no",
"NonBlocking": "no",
"NotifyAccess": "main",
"OOMScoreAdjust": "0",
"OnFailureJobMode": "replace",
"PermissionsStartOnly": "no",
"PrivateDevices": "no",
"PrivateNetwork": "no",
"PrivateTmp": "yes",
"ProtectHome": "no",
"ProtectSystem": "no",
"RefuseManualStart": "no",
"RefuseManualStop": "no",
"RemainAfterExit": "no",
"Requires": "basic.target -.mount",
"RequiresMountsFor": "/var/tmp",
"Restart": "no",
"RestartUSec": "100ms",
"Result": "success",
"RootDirectoryStartOnly": "no",
"RuntimeDirectoryMode": "0755",
"SameProcessGroup": "no",
"SecureBits": "0",
"SendSIGHUP": "no",
"SendSIGKILL": "yes",
"Slice": "system.slice",
"StandardError": "inherit",
"StandardInput": "null",
"StandardOutput": "journal",
"StartLimitAction": "none",
"StartLimitBurst": "5",
"StartLimitInterval": "10000000",
"StartupBlockIOWeight": "18446744073709551615",
"StartupCPUShares": "18446744073709551615",
"StatusErrno": "0",
"StopWhenUnneeded": "no",
"SubState": "dead",
"SyslogLevelPrefix": "yes",
"SyslogPriority": "30",
"SystemCallErrorNumber": "0",
"TTYReset": "no",
"TTYVHangup": "no",
"TTYVTDisallocate": "no",
"TasksAccounting": "no",
"TasksCurrent": "18446744073709551615",
"TasksMax": "18446744073709551615",
"TimeoutStartUSec": "1min 30s",
"TimeoutStopUSec": "1min 30s",
"TimerSlackNSec": "50000",
"Transient": "no",
"Type": "notify",
"UMask": "0022",
"UnitFilePreset": "disabled",
"UnitFileState": "disabled",
"Wants": "system.slice",
"WatchdogTimestampMonotonic": "0",
"WatchdogUSec": "0"
}
}
[root@ansible ansible]# ansible web -m shell -a 'netstat -antunlp | grep httpd'
web1 | SUCCESS | rc=0 >>
tcp6 0 0 :::8080 :::* LISTEN 2346/httpd

web2 | SUCCESS | rc=0 >>
tcp6 0 0 :::8080 :::* LISTEN 1654/httpd

 

[root@ansible ansible]# ansible web -m shell -a 'echo "hello world" > /var/www/html/index.html'
web1 | SUCCESS | rc=0 >>


web2 | SUCCESS | rc=0 >>


[root@ansible ansible]# ansible web -m shell -a 'cat /var/www/html/index.html'
web2 | SUCCESS | rc=0 >>
hello world

web1 | SUCCESS | rc=0 >>
hello world


[root@ansible ansible]# ansible web -m shell -a 'curl 127.0.0.1:8080'
[WARNING]: Consider using get_url or uri module rather than running curl

web2 | SUCCESS | rc=0 >>
hello world % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 12 100 12 0 0 18897 0 --:--:-- --:--:-- --:--:-- 12000

web1 | SUCCESS | rc=0 >>
hello world % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 12 100 12 0 0 18633 0 --:--:-- --:--:-- --:--:-- 12000


在web1上验证一下
[root@web1 home]# curl 192.168.3.61:8080
hello world
####################################################################################
setup模块

[root@ansible ansible]# ansible cache -m setup | grep cache
cache | SUCCESS => {
"tx_nocache_copy": "off",
"ansible_fqdn": "cache",
"ansible_hostname": "cache",
"tx_nocache_copy": "off [fixed]",
"nocache": {
"cached": 0,
"ansible_nodename": "cache",


[root@ansible ansible]# ansible cache -m setup -a 'filter="ansible_hostname"' filter的作用有点像grep
cache | SUCCESS => {
"ansible_facts": {
"ansible_hostname": "cache"
},
"changed": false
}

颜色:
绿色代表成功
黄色代表内容发生改变了
红色代表失败
##############################################################################
[root@ansible ansible]# ansible-doc -l 查看所有的模块

 

posted @ 2019-04-30 22:57  安于夏  阅读(389)  评论(0编辑  收藏  举报