ansible的基本用法如下。
ansible 机器名 -m 模块x -a "模块的参数"
对被管理机器执行不同的操作,只需要调用不同的模块就可以了。ansible中内置了很多的模块,可以通过ansible-doc -l查看系统中所有的模块。
[tom@RedHat ~]$ ansible-doc -l
按【Enter】键会一行一行地往下显示,按空格键会一页一页地往下显示,按【q】键退出。
不同的模块有不同的参数,如果要查看某个模块的参数,可以通过以下语法来查看。
ansible-doc 模块名
ansible中有很多模块,每个模块也有很多参数,我们是不可能把所有的模块、每个模块的所有参数都掌握的。所以,下面我们只讲解最常见的模块及这些模块中最常见的参数的使用方法。
shell模块可以在远端执行操作系统命令,具体用法如下。
ansible 主机组 -m shell -a "系统命令"
(1)在RedHat2上执行hostname命令,命令如下。
[tom@RedHat ~]$ ansible RedHat2 -m shell -a "hostname"
RedHat2 | CHANGED | rc=0 >>
RedHat2
[tom@RedHat ~]$
这里rc=0的意思是执行此命令之后的返回值为0,rc的意思是returm code(返回值),为0说明正确执行了,非零说明没有正确执行。
(2) 在RedHat2上执行一个错误的命令,命令如下。
[tom@RedHat ~]$ ansible RedHat2 -m shell -a "hostnamexx"
RedHat2 | FAILED | rc=127 >>
/bin/sh: hostnamexx: 未找到命令non-zero return code
[tom@RedHat ~]$
这里rc=127的意思是执行此命令之后的返回值为127,非零说明没有正确执行。
file模块用于创建和删除文件或目录,修改文件或目录属性,其常见的参数包括以下几个。
(1)在RedHat2上创建一个文件/opt/hosts,并设置所有者为root,所属组为tom,权限为444,命令如下。
[tom@RedHat ~]$ ansible RedHat2 -m file -a "path=/opt/hosts owner=root group=tom mode=444 state=touch"
RedHat2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/opt/hosts",
"gid": 1000,
"group": "tom",
"mode": "0444",
"owner": "root",
"secontext": "unconfined_u:object_r:usr_t:s0",
"size": 0,
"state": "file",
"uid": 0
}
[tom@RedHat ~]$
需要注意的是,此处用path指定的文件,替换成name也是可以的,即name=/opt/hosts。查看文件的属性,命令如下。
[tom@RedHat ~]$ ansible RedHat2 -m shell -a "ls -l /opt/hosts"
RedHat2 | CHANGED | rc=0 >>
-r--r--r--. 1 root tom 0 12月 21 10:52 /opt/hosts
[tom@RedHat ~]$
(2)为/opt/hosts创建一个软链接/opt/hosts123,命令如下。
[tom@RedHat ~]$ ansible RedHat2 -m file -a "src=/opt/hosts dest=/opt/hosts123 state=link"
RedHat2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/opt/hosts123",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"secontext": "unconfined_u:object_r:usr_t:s0",
"size": 10,
"src": "/opt/hosts",
"state": "link",
"uid": 0
}
[tom@RedHat ~]$
验证,命令如下。
[tom@RedHat ~]$ ansible RedHat2 -m shell -a "ls -l /opt/"
RedHat2 | CHANGED | rc=0 >>
总用量 0
-r--r--r--. 1 root tom 0 12月 21 10:52 hosts
lrwxrwxrwx. 1 root root 10 12月 21 11:01 hosts123 -> /opt/hosts
[tom@RedHat ~]$
(3)删除/opt/hosts123,命令如下。
[tom@RedHat ~]$ ansible RedHat2 -m file -a "name=/opt/hosts123 state=absent"
RedHat2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"path": "/opt/hosts123",
"state": "absent"
}
[tom@RedHat ~]$
(4)创建目录/opt/xx,上下文设置为default_t,命令如下。
[tom@RedHat ~]$ ansible RedHat2 -m file -a 'name=/opt/xx state=directory setype=default_t'
RedHat2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/opt/xx",
"secontext": "unconfined_u:object_r:default_t:s0",
"size": 6,
"state": "directory",
"uid": 0
}
[tom@RedHat ~]$
(5)把/opt/hosts的权限改成000,所有者改成tom,所属组改成users,命令如下。
[tom@RedHat ~]$ ansible RedHat2 -m file -a "name=/opt/hosts owner=tom group=users mode=000"
RedHat2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 100,
"group": "users",
"mode": "0000",
"owner": "tom",
"path": "/opt/hosts",
"secontext": "unconfined_u:object_r:usr_t:s0",
"size": 0,
"state": "file",
"uid": 1000
}
[tom@RedHat ~]$
验证,命令如下。
[tom@RedHat ~]$ ansible RedHat2 -m shell -a "ls -l /opt/hosts"
RedHat2 | CHANGED | rc=0 >>
----------. 1 tom users 0 12月 21 10:52 /opt/hosts
[tom@RedHat ~]$
注意:指定文件时用name或path都是可以的。
清空RedHat2上/opt中所有的内容,命令如下。
[tom@RedHat ~]$ ansible RedHat2 -m shell -a "rm -rf /opt/*"
[WARNING]: Consider using the file module with state=absent rather than running 'rm'. If you
need to use command because file is insufficient you can add 'warn: false' to this command
task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
RedHat2 | CHANGED | rc=0 >>
[tom@RedHat ~]$
上面的WARNING可以忽略不管,如果不想显示此消息,则在ansible.cfg的[defaults]字段下添加 command warnings=False即可。
copy用于把本地的文件拷贝到被管理器,语法如下。
ansible 主机组 -m copy -a "src=/path1/file1 dest=path2"
作用是把本地的/path1/file1拷贝到目的主机的/path2中。
copy模块常见的几个参数包括以下几个。
(1)把本地的文件/etc/ansible/hosts拷贝到目标机器的/opt目录中,并设置权限为000,所有者为tom,命令如下。
[tom@RedHat ~]$ ansible RedHat2 -m copy -a "src=/etc/ansible/hosts mode=000 owner=tom dest=/opt"
RedHat2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "da996f1a52dbae3b6b43a6c50d761e4ed5ec9a9f",
"dest": "/opt/hosts",
"gid": 0,
"group": "root",
"md5sum": "1564b951dc7c8511c6f9ee842653c541",
"mode": "0000",
"owner": "tom",
"secontext": "system_u:object_r:usr_t:s0",
"size": 1016,
"src": "/home/tom/.ansible/tmp/ansible-tmp-1703130227.8995965-3786-209287909553812/source",
"state": "file",
"uid": 1000
}
[tom@RedHat ~]$
验证,命令如下。
[tom@RedHat ~]$ ansible RedHat2 -m shell -a "ls -l /opt/hosts"
RedHat2 | CHANGED | rc=0 >>
----------. 1 tom root 1016 12月 21 11:43 /opt/hosts
[tom@RedHat ~]$
copy模块也可以利用content参数往某个文件中写内容,如果此文件不存在则会创建出来。
(2)在被管理机器的/opt目录中创建11.txt,内容为123456,命令如下。
[tom@RedHat ~]$ ansible RedHat2 -m copy -a 'content="123456" dest=/opt/11.txt'
RedHat2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "7c4a8d09ca3762af61e59520943dc26494f8941b",
"dest": "/opt/11.txt",
"gid": 0,
"group": "root",
"md5sum": "e10adc3949ba59abbe56e057f20f883e",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:usr_t:s0",
"size": 6,
"src": "/home/tom/.ansible/tmp/ansible-tmp-1703130763.039135-3915-118078635332073/source",
"state": "file",
"uid": 0
}
[tom@RedHat ~]$
验证/opt/11.txt的内容,命令如下。
[tom@RedHat ~]$ ansible RedHat2 -m shell -a "cat /opt/11.txt"
RedHat2 | CHANGED | rc=0 >>
123456
[tom@RedHat ~]$
fetch用于把文件从被管理机器拷贝到本机当前目录中,命令如下。
[tom@RedHat ~]$ ansible RedHat2 -m fetch -a "src=/opt/hosts dest=."
RedHat2 | CHANGED => {
"changed": true,
"checksum": "da996f1a52dbae3b6b43a6c50d761e4ed5ec9a9f",
"dest": "/home/tom/RedHat2/opt/hosts",
"md5sum": "1564b951dc7c8511c6f9ee842653c541",
"remote_checksum": "da996f1a52dbae3b6b43a6c50d761e4ed5ec9a9f",
"remote_md5sum": null
}
[tom@RedHat ~]$
查看命令如下。
[tom@RedHat ~]$ tree RedHat2
RedHat2
└── opt
└── hosts
1 directory, 1 file
[tom@RedHat ~]$
利用yum_repository设置yum源,一个标准的repo配置文件,如下所示。
[tom@RedHat ~]$ cat /etc/yum.repos.d/aa.repo
[aa]
name=aa
baseurl=file:///mnt/AppStream
enabled=1
gpgcheck=0
gpgkey=ftp://192.168.26.101/dvd/RPM‐GPG‐KEY‐redhat‐release
[bb]
name=bb
baseurl=file:///mnt/BaseOS
enabled=1
gpgcheck=0
[tom@RedHat ~]$
其中门中的名称用于区分不同的yum源,这里参数的含义如下。
对于yum_repository模块来说,常见的参数包括以下几个。
(1)给RedHat2配置yum源,地址是ftp://192.168.56.11/dvd/AppStream,所需要的密钥文件为ftp://192.168.56.11/dvd/RPM-GPG-KEY-redhat-release,命令如下。
[tom@RedHat ~]$ ansible RedHat2 -m yum_repository -a "name=app description='this is appstream' baseurl=ftp://192.168.56.11/dvd/AppStream gpgcheck=yes gpgkey=ftp://192.168.56.11/dvd/RPM-GPG-KEY-redhat-release"
RedHat2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"repo": "app",
"state": "present"
}
[tom@RedHat ~]$
执行之后的效果如下。
[tom@RedHat ~]$ ansible RedHat2 -m shell -a "ls /etc/yum.repos.d/"
RedHat2 | CHANGED | rc=0 >>
app.repo
redhat.repo
[tom@RedHat ~]$ ansible RedHat2 -m shell -a "cat /etc/yum.repos.d/app.repo"
RedHat2 | CHANGED | rc=0 >>
[app]
baseurl = ftp://192.168.56.11/dvd/AppStream
gpgcheck = 1
gpgkey = ftp://192.168.56.11/dvd/RPM-GPG-KEY-redhat-release
name = this is appstream
[tom@RedHat ~]$
(2)给RedHat2配置第二个yum源,地址是 ftp://192.168.56.11/dvd/BaseOS,所需要的密钥
文件为ftp://192.168.56.11/dvd/RPM-GPG-KEY-redhat-release,命令如下。
[tom@RedHat ~]$ ansible RedHat2 -m yum_repository -a "name=baseos description='this is baseos' baseurl=ftp://192.168.56.11/dvd/BaseOS gpgcheck=yes gpgkey=ftp://192.168.56.11/dvd/RPM-GPG-KEY-redhat-release"
RedHat2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"repo": "baseos",
"state": "present"
}
[tom@RedHat ~]$
验证,命令如下。
[tom@RedHat ~]$ ansible RedHat2 -m shell -a "ls /etc/yum.repos.d/"
RedHat2 | CHANGED | rc=0 >>
app.repo
baseos.repo
redhat.repo
[tom@RedHat ~]$ ansible RedHat2 -m shell -a "cat /etc/yum.repos.d/baseos.repo"
RedHat2 | CHANGED | rc=0 >>
[baseos]
baseurl = ftp://192.168.56.11/dvd/BaseOS
gpgcheck = 1
gpgkey = ftp://192.168.56.11/dvd/RPM-GPG-KEY-redhat-release
name = this is baseos
[tom@RedHat ~]$