一、下载ansible软件
[root@m01 ~]# yum install -y ansible
二、创建目录
[root@m01 ~]# mkdir ansible_ad-hoc
[root@m01 ~]# cd ansible_ad-hoc/
三、拷贝ansible配置文件
[root@m01 ansible_ad-hoc]# cp /etc/ansible/ansible.cfg .
四、编写主机清单
[root@m01 ansible_ad-hoc]# cat hosts
[web]
172.16.1.7
172.16.1.8
[root@m01 ansible_ad-hoc]# ls
ansible.cfg hosts
五、ansible的command模块
1、查看磁盘信息
[root@m01 ansible_ad-hoc]# ansible web -a "df -h "
172.16.1.7 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
devtmpfs 476M 0 476M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda3 18G 3.2G 15G 18% /
/dev/sda1 1014M 127M 888M 13% /boot
tmpfs 98M 0 98M 0% /run/user/0
172.16.1.8 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
devtmpfs 476M 0 476M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda3 18G 3.6G 15G 20% /
/dev/sda1 1014M 127M 888M 13% /boot
tmpfs 98M 0 98M 0% /run/user/0
2、切换目录
[root@m01 ansible_ad-hoc]# ansible web -m command -a "chdir=/var/log pwd"
172.16.1.8 | CHANGED | rc=0 >>
/var/log
172.16.1.7 | CHANGED | rc=0 >>
/var/log
3、创建文件,如果1.txt存在就不创建,如果不存在就创建2.txt
[root@m01 ansible_ad-hoc]# ansible web -m command -a "creates=/root/1.txt touch /root/2.txt"
[WARNING]: Consider using the file module with state=touch rather than running 'touch'. 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.
172.16.1.7 | CHANGED | rc=0 >>
172.16.1.8 | CHANGED | rc=0 >>
六、ansible的shell模块(可以识别特殊字符)
[root@m01 ansible_ad-hoc]# ansible web -m shell -a "cd /tmp ; who"
172.16.1.8 | CHANGED | rc=0 >>
root pts/0 2020-07-01 22:48 (10.0.0.1)
root pts/1 2020-07-01 23:13 (172.16.1.61)
172.16.1.7 | CHANGED | rc=0 >>
root pts/0 2020-07-01 22:48 (10.0.0.1)
root pts/1 2020-07-01 23:13 (172.16.1.61)
七、ansible的script模块
[root@m01 ansible_ad-hoc]# ansible web -m script -a "/server/1.sh"
172.16.1.7 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 172.16.1.7 closed.\r\n",
"stderr_lines": [
"Shared connection to 172.16.1.7 closed."
],
"stdout": "",
"stdout_lines": []
}
172.16.1.8 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 172.16.1.8 closed.\r\n",
"stderr_lines": [
"Shared connection to 172.16.1.8 closed."
],
"stdout": "",
"stdout_lines": []
}
八、ansible的copy模块
src #源路径
content #内容
dest #目录路径
owner #属主
group #属组
mode #权限
backup #备份
[root@m01 ansible_ad-hoc]# ansible web -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ owner=root group=root mode=600 backup=yes"
172.16.1.8 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "1f31c0e82b36e84604af41733e88821d541ee9ed",
"dest": "/tmp/ansible.cfg",
"gid": 0,
"group": "root",
"md5sum": "9b86926c0c703847d5e32f31d92a93bd",
"mode": "0600",
"owner": "root",
"size": 19985,
"src": "/root/.ansible/tmp/ansible-tmp-1593610319.71-186211502156362/source",
"state": "file",
"uid": 0
}
172.16.1.7 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "1f31c0e82b36e84604af41733e88821d541ee9ed",
"dest": "/tmp/ansible.cfg",
"gid": 0,
"group": "root",
"md5sum": "9b86926c0c703847d5e32f31d92a93bd",
"mode": "0600",
"owner": "root",
"size": 19985,
"src": "/root/.ansible/tmp/ansible-tmp-1593610319.7-281148617678622/source",
"state": "file",
"uid": 0
}
[root@m01 ansible_ad-hoc]# ansible web -m copy -a "content='ansible' dest=./test.txt"
172.16.1.8 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "bef45157a43c9e5f469d188810814a4a8ab9f2ed",
"dest": "./test.txt",
"gid": 0,
"group": "root",
"md5sum": "640c8a5376aa12fa15cf02130ce239a6",
"mode": "0644",
"owner": "root",
"size": 7,
"src": "/root/.ansible/tmp/ansible-tmp-1593610440.57-2437460552443/source",
"state": "file",
"uid": 0
}
172.16.1.7 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "bef45157a43c9e5f469d188810814a4a8ab9f2ed",
"dest": "./test.txt",
"gid": 0,
"group": "root",
"md5sum": "640c8a5376aa12fa15cf02130ce239a6",
"mode": "0644",
"owner": "root",
"size": 7,
"src": "/root/.ansible/tmp/ansible-tmp-1593610440.57-196172343252805/source",
"state": "file",
"uid": 0
}
九、ansible的file模块
file:
path #路径
state #状态
owner #属主
group #属组
mode #权限
recurse: yes #递归授权
group :
name #组名称
state #组状态
gid #组gid
[root@m01 ~]# ansible web -m file -a "path=/rot state=directory owner=root group=root mode=0755"
10.0.0.8 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/rot",
"size": 6,
"state": "directory",
"uid": 0
}
10.0.0.7 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/rot",
"size": 6,
"state": "directory",
"uid": 0
}
[root@m01 ~]# ansible web -m group -a "name=ttt gid=888 state=present"
10.0.0.7 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 888,
"name": "ttt",
"state": "present",
"system": false
}
10.0.0.8 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 888,
"name": "ttt",
"state": "present",
"system": false
}
十、ansible的fentch模块
将管理主机数据进行拉取
[root@m01 ~]# ansible web -m fetch -a "src=/tmp/back.txt dest=/root/"
10.0.0.7 | CHANGED => {
"changed": true,
"checksum": "ea3279fd7a2b6b7bc7fc64e8e2d460090d1f2007",
"dest": "/root/10.0.0.7/tmp/back.txt",
"md5sum": "6e5b6fdddf760007a8ffd1591ac25ebb",
"remote_checksum": "ea3279fd7a2b6b7bc7fc64e8e2d460090d1f2007",
"remote_md5sum": null
}
10.0.0.8 | CHANGED => {
"changed": true,
"checksum": "c72f0281b1145de32031bb393a065c32896a0747",
"dest": "/root/10.0.0.8/tmp/back.txt",
"md5sum": "f982777b5355be2aaf5e595fef335811",
"remote_checksum": "c72f0281b1145de32031bb393a065c32896a0747",
"remote_md5sum": null
}
[root@m01 ~]# ls
10.0.0.7 10.0.0.8
[root@m01 ~]# ls 10.0.0.7/tmp/back.txt
10.0.0.7/tmp/back.txt
[root@m01 ~]# ls 10.0.0.8/tmp/back.txt
10.0.0.8/tmp/back.txt
十一、ansible的yum模块
[root@m01 ansible_ad-hoc]# ansible web -m yum -a "name=lrzsz state=latest"
172.16.1.7 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"installed": [
"lrzsz"
],
"updated": []
},
"msg": "",
"obsoletes": {
"iwl7265-firmware": {
"dist": "noarch",
"repo": "@anaconda",
"version": "22.0.7.0-69.el7"
}
},
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirrors.aliyun.com\n * extras: mirrors.aliyun.com\n * updates: mirrors.aliyun.com\nResolving Dependencies\n--> Running transaction check\n---> Package lrzsz.x86_64 0:0.12.20-36.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n lrzsz x86_64 0.12.20-36.el7 base 78 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 78 k\nInstalled size: 181 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : lrzsz-0.12.20-36.el7.x86_64 1/1 \n Verifying : lrzsz-0.12.20-36.el7.x86_64 1/1 \n\nInstalled:\n lrzsz.x86_64 0:0.12.20-36.el7 \n\nComplete!\n"
]
}
172.16.1.8 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"changes": {
"installed": [],
"updated": []
},
"msg": "",
"obsoletes": {
"iwl7265-firmware": {
"dist": "noarch",
"repo": "@anaconda",
"version": "22.0.7.0-69.el7"
}
},
"rc": 0,
"results": [
"All packages providing lrzsz are up to date",
""
]
}
十二、ansible的service模块
[root@m01 ~]# ansible web -m service -a "name=nginx state=stopped "
ansible的cron模块
1.创建定时任务
[root@m01 ~]# ansible web -m cron -a "name='exec shell scripts03' minute=0 hour=2 job='/bin/sh /server/scripts/xf.sh'"
10.0.0.7 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"exec shell scripts03"
]
}
10.0.0.8 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"exec shell scripts03"
]
}
[root@web1 ~]# crontab -l
01 00 * * * sh /scripts/client_push_data_server.sh &>/dev/null
* * * * * /usr/bin/sh /server/scripts/rm.sh
#Ansible: exec shell scripts03
0 2 * * * /bin/sh /server/scripts/xf.sh
2.删除定时任务
[root@m01 ~]# ansible web -m cron -a "name='exec shell scripts03' state=absent"
10.0.0.7 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": []
}
10.0.0.8 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": []
}
3.注释定时任务
[root@m01 ~]# ansible web -m cron -a "name='exec shell scripts03' minute=0 hour=2 job='/bin/sh /server/scripts/xf.sh' disabled=yes"
10.0.0.8 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"exec shell scripts03"
]
}
10.0.0.7 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"exec shell scripts03"
]
}
[root@web1 ~]# crontab -l
01 00 * * * sh /scripts/client_push_data_server.sh &>/dev/null
* * * * * /usr/bin/sh /server/scripts/rm.sh
#Ansible: exec shell scripts03
#0 2 * * * /bin/sh /server/scripts/xf.sh
十三、ansible的user模块
user:
name #用户名称
uid #用户uid
group #用户组名称
create_home #创建用户家目录
shell # 用户的bash
system #系统用户
password #用户密码
[root@m01 ~]# ansible web -m user -a "name=service uid=7777 group=ttt groups=root shell=/bin/bash create_home=yes"
10.0.0.7 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 888,
"groups": "root",
"home": "/home/service",
"name": "service",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 7777
}
10.0.0.8 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 888,
"groups": "root",
"home": "/home/service",
"name": "service",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 7777
}
[root@web1 ~]# id service
uid=7777(service) gid=888(ttt) 组=888(ttt),0(root)
[root@m01 ~]# ansible web -m user -a "name=hello system=yes shell=/sbin/nologin create_home=no"
10.0.0.7 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": false,
"group": 662,
"home": "/home/hello",
"name": "hello",
"shell": "/sbin/nologin",
"state": "present",
"system": true,
"uid": 662
}
10.0.0.8 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": false,
"group": 663,
"home": "/home/hello",
"name": "hello",
"shell": "/sbin/nologin",
"state": "present",
"system": true,
"uid": 663
}
[root@web1 ~]# id hello
uid=662(hello) gid=662(hello) 组=662(hello)
十四、ansible的unarchive模块
[root@m01 ansible_ad-hoc]# ansible web -m unarchive -a "src=/test/test.tgz dest=/test/"
[root@web1 test]# ls
1.txt 2.txt
[root@m01 test]# ansible web -m unarchive -a "src=/test/test.zip dest=/test/"
10.0.0.8 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/test/",
"extract_results": {
"cmd": [
"/usr/bin/unzip",
"-o",
"/root/.ansible/tmp/ansible-tmp-1593616034.72-108750537816018/source",
"-d",
"/test/"
],
"err": "",
"out": "Archive: /root/.ansible/tmp/ansible-tmp-1593616034.72-108750537816018/source\n extracting: /test/1.txt \n extracting: /test/2.txt \n inflating: /test/test.tar \n",
"rc": 0
},
"gid": 0,
"group": "root",
"handler": "ZipArchive",
"mode": "0755",
"owner": "root",
"size": 48,
"src": "/root/.ansible/tmp/ansible-tmp-1593616034.72-108750537816018/source",
"state": "directory",
"uid": 0
}
10.0.0.7 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/test/",
"extract_results": {
"cmd": [
"/usr/bin/unzip",
"-o",
"/root/.ansible/tmp/ansible-tmp-1593616034.72-203787298254403/source",
"-d",
"/test/"
],
"err": "",
"out": "Archive: /root/.ansible/tmp/ansible-tmp-1593616034.72-203787298254403/source\n extracting: /test/1.txt \n extracting: /test/2.txt \n inflating: /test/test.tar \n",
"rc": 0
},
"gid": 0,
"group": "root",
"handler": "ZipArchive",
"mode": "0755",
"owner": "root",
"size": 48,
"src": "/root/.ansible/tmp/ansible-tmp-1593616034.72-203787298254403/source",
"state": "directory",
"uid": 0
}
0 Comments