大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
1. ansible-常用模块
创新互联 - 托管服务器,四川服务器租用,成都服务器租用,四川网通托管,绵阳服务器托管,德阳服务器托管,遂宁服务器托管,绵阳服务器托管,四川云主机,成都云主机,西南云主机,托管服务器,西南服务器托管,四川/成都大带宽,服务器机柜,四川老牌IDC服务商根据官方的分类,将模块按功能分类为:云模块、命令模块、数据库模块、文件模块、资产模块、消息模块、监控模块、网络模块、通知模块、包管理模块、源码控制模块、系统模块、单元模块、web设施模块、windows模块
user:配置用户
group:配置用户组
cron:配置计划任务
copy:复制文件到远程主机
file: 用于配置文件属性
yum:用于安装软件包
service:用于管理服务
shell: 用于执行命令可以带 “ |”管道符号等
scripts:在远程主机执行控制端的脚本文件
setup:查看远程主机的基本信息
filesystem:在块设备上创建文件系统
mount:配置挂载点
synchronize:使用rsync同步文件
get_url:该模块主要用于从http、ftp、https服务器上下载文件(类似于wget)
package:使用os包管理器安装,升级和删除包
stat:获取远程主机文件状态信息。
unarchive: 用于解压文件
command:在远程主机上执行命令
raw:类似于shell模块,支持管道
ping:用于检测远程主机是否存活
1)ping模块
测试主机是否通的,用法很简单
[root@test-1 ansible]# ansible test -m ping [DEPRECATION WARNING]: DEFAULT_SUDO_USER option, In favor of Ansible Become, which is a generic framework. See become_user. , use become instead. This feature will be removed in version 2.8. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg. 192.168.3.174 | SUCCESS => { "changed": false, "ping": "pong" } 192.168.3.175 | SUCCESS => { "changed": false, "ping": "pong" }2) file-模块
file模块主要用于远程主机上的文件操作,file模块包含如下选项:
force:需要在两种情况下强制创建软连接。
一种是源文件不存在但之后会创建的情况下;
另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no
group:定义文件/目录的属组
owner:定义文件/目录的属主
mode:定义文件/目录的权限
path:必选项,定义文件/目录的路径
recurse:递归设置文件的属性,只对目录有效
src:要被链接的源文件的路径,只应用于state=link的情况
dest:被链接到的路径,只应用于state=link的情况
state:定义文件状态
directory:如果目录不存在,创建目录
file:即使文件不存在,也不会被创建
link:创建软链接
hard:创建硬链接
touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
absent:删除目录、文件或者取消链接文件
2.1) 案例1-用file创建一个软连接/etc/fstab到/tmp/fstab
[root@test-1 ansible]# ansible test -m file -a " src=/etc/fstab dest=/tmp/fstab state=link" [DEPRECATION WARNING]: DEFAULT_SUDO_USER option, In favor of Ansible Become, which is a generic framework. See become_user. , use become instead. This feature will be removed in version 2.8. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg. 192.168.3.175 | CHANGED => { "changed": true, "dest": "/tmp/fstab", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 10, "src": "/etc/fstab", "state": "link", "uid": 0 } 192.168.3.174 | CHANGED => { "changed": true, "dest": "/tmp/fstab", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 10, "src": "/etc/fstab", "state": "link", "uid": 0 }执行结果
[root@test-2 tmp]# ll total 8 lrwxrwxrwx. 1 root root 10 Nov 19 02:44 fstab -> /etc/fstab -rwx------. 1 root root 836 Oct 24 09:40 ks-script-6aY4Ug drwx------. 3 root root 17 Oct 25 23:39 systemd-private-664f2393bb954d4d812ce589bd921c84-chronyd.service-sclu10 -rw-r--r--. 1 root root 8 Oct 25 22:26 test.txt drwx------. 2 root root 6 Oct 24 09:44 vmware-root -rw-------. 1 root root 0 Oct 24 09:36 yum.log2.2) 案例2-我们需要在远程服务器上/tmp/下创建一个file文件
[root@test-1 ansible]# ansible test -m file -a 'path=/tmp/file state=touch' [DEPRECATION WARNING]: DEFAULT_SUDO_USER option, In favor of Ansible Become, which is a generic framework. See become_user. , use become instead. This feature will be removed in version 2.8. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg. 192.168.3.174 | CHANGED => { "changed": true, "dest": "/tmp/file", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 0, "state": "file", "uid": 0执行结果:
[root@test-2 tmp]# ll total 8 -rw-r--r--. 1 root root 0 Nov 19 03:12 file lrwxrwxrwx. 1 root root 10 Nov 19 02:44 fstab -> /etc/fstab -rwx------. 1 root root 836 Oct 24 09:40 ks-script-6aY4Ug drwx------. 3 root root 17 Oct 25 23:39 systemd-private-664f2393bb954d4d812ce589bd921c84-chronyd.service-sclu10 -rw-r--r--. 1 root root 8 Oct 25 22:26 test.txt drwx------. 2 root root 6 Oct 24 09:44 vmware-root -rw-------. 1 root root 0 Oct 24 09:36 yum.log2.3) 案例3-需要远程test组下修改file的文件权限
[root@test-1 ansible]# ansible test -m file -a "path=/tmp/file mode=755" [DEPRECATION WARNING]: DEFAULT_SUDO_USER option, In favor of Ansible Become, which is a generic framework. See become_user. , use become instead. This feature will be removed in version 2.8. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg. 192.168.3.174 | CHANGED => { "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/tmp/file", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 0, "state": "file", "uid": 0 }执行结果:
[root@test-1 ansible]# ansible test -m command -a 'ls -lh /tmp/' [DEPRECATION WARNING]: DEFAULT_SUDO_USER option, In favor of Ansible Become, which is a generic framework. See become_user. , use become instead. This feature will be removed in version 2.8. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg. 192.168.3.174 | CHANGED | rc=0 >> total 8.0K drwx------. 2 root root 80 Nov 19 03:20 ansible_command_payload_8myj39 -rwxr-xr-x. 1 root root 0 Nov 19 03:12 file lrwxrwxrwx. 1 root root 10 Nov 19 02:44 fstab -> /etc/fstab -rwx------. 1 root root 836 Oct 24 09:40 ks-script-6aY4Ug drwx------. 3 root root 17 Oct 25 23:39 systemd-private-664f2393bb954d4d812ce589bd921c84-chronyd.service-sclu10 -rw-r--r--. 1 root root 8 Oct 25 22:26 test.txt drwx------. 2 root root 6 Oct 24 09:44 vmware-root -rw-------. 1 root root 0 Oct 24 09:36 yum.log3) copy模块
backup:在覆盖之前将原文件备份,备份文件包含时间信息。有两个选项:yes|no
content:用于替代"src",可以直接设定指定文件的值
dest:必选项。要将源文件复制到的远程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目录
directory_mode:递归的设定目录的权限,默认为系统默认权限
force:如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes
others:所有的file模块里的选项都可以在这里使用
src:要复制到远程主机的文件在本地的地址,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用"/"来结尾,则只复制目录里的内容,如果没有使用"/"来结尾,则包含目录在内的整个内容全部复制,类似于rsync。
validate :The validation command to run before copying into place. The path to the file to validate is passed in via '%s' which must be present as in the visudo example bel
3.1) 案例1-从本地拷贝文件到ansibel目标的目录
[root@test-1 ansible]# touch /tmp/aa #本地测试创建的aa文件 [root@test-1 ansible]# ansible test -m copy -a " src=/tmp/aa dest=/tmp/aa" [DEPRECATION WARNING]: DEFAULT_SUDO_USER option, In favor of Ansible Become, which is a generic framework. See become_user. , use become instead. This feature will be removed in version 2.8. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg. 192.168.3.174 | CHANGED => { "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/tmp/aa", "gid": 0, "group": "root", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 0, "src": "/root/.ansible/tmp/ansible-tmp-1542616228.03-86258002572076/source", "state": "file", "uid": 0 }执行结果:
[root@test-2 tmp]# ll total 8 -rw-r--r--. 1 root root 0 Nov 19 03:30 aa -rwxr-xr-x. 1 root root 0 Nov 19 03:12 file lrwxrwxrwx. 1 root root 10 Nov 19 02:44 fstab -> /etc/fstab -rwx------. 1 root root 836 Oct 24 09:40 ks-script-6aY4Ug drwx------. 3 root root 17 Oct 25 23:39 systemd-private-664f2393bb954d4d812ce589bd921c84-chronyd.service-sclu10 -rw-r--r--. 1 root root 8 Oct 25 22:26 test.txt drwx------. 2 root root 6 Oct 24 09:44 vmware-root -rw-------. 1 root root 0 Oct 24 09:36 yum.log3.2) 案例2-ansible使用backup进行备份
[root@test-1 ansible]# vim /tmp/aa [root@test-1 ansible]# ansible test -m copy -a " src=/tmp/aa dest=/tmp/aa backup=yes" [DEPRECATION WARNING]: DEFAULT_SUDO_USER option, In favor of Ansible Become, which is a generic framework. See become_user. , use become instead. This feature will be removed in version 2.8. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg. 192.168.3.174 | CHANGED => { "backup_file": "/tmp/aa.3042.2018-11-19@03:37:06~", "changed": true, "checksum": "5730dd3a58d64a39a7fc704c3c5570d70303d9db", "dest": "/tmp/aa", "gid": 0, "group": "root", "md5sum": "96fdb0b7ddbb489f8636769965584623", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 35, "src": "/root/.ansible/tmp/ansible-tmp-1542616624.56-136259009428901/source", "state": "file", "uid": 0执行结果
[root@test-2 tmp]# ll total 16 -rw-r--r--. 1 root root 35 Nov 19 03:37 aa -rw-r--r--. 1 root root 56 Nov 19 03:36 aa.3042.2018-11-19@03:37:06~ #这里是ansible使用的 -rwxr-xr-x. 1 root root 0 Nov 19 03:12 file lrwxrwxrwx. 1 root root 10 Nov 19 02:44 fstab -> /etc/fstab -rwx------. 1 root root 836 Oct 24 09:40 ks-script-6aY4Ug drwx------. 3 root root 17 Oct 25 23:39 systemd-private-664f2393bb954d4d812ce589bd921c84-chronyd.service-sclu10 -rw-r--r--. 1 root root 8 Oct 25 22:26 test.txt drwx------. 2 root root 6 Oct 24 09:44 vmware-root -rw-------. 1 root root 0 Oct 24 09:36 yum.log另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。