[toc]
AD-hoc概述
什么是ad-hoc?
ad-hoc简而言之就是“临时命令”,执行完即结束,并不会保存
官方模块参考:官方模块
ansible执行任务的方式
1 2 3 4 5 6
| AD-hoc playbook
ansible 主机 -m 模块 -a '动作'
|
AD-hoc结果返回颜色
1 2 3 4
| 绿色:命令执行成功无变化的颜色 黄色:命令执行成功有变化的颜色 红色:命令执行失败,报错msg 粉色:warning 警告一般无需处理
|
ansible查看模块帮助
1 2 3 4 5
| ansible-doc 模块
/EXAMPLES
|
ad-hoc常用模块
1 2 3 4 5 6 7 8 9 10 11 12 13
| command shell scripts yum_repository yum copy file service mount cron get_url firewalld selinux
|
Ansible命令模块
command模块
1 2 3
| ansible web_group -m command -a 'ps -ef'
|
shell模块
1 2 3
| ansible web_group -m shell -a 'ps -ef'
|
script模块
1 2 3
| ansible web_group -m script -a '/root/touch.sh'
|
Ansible文件管理模块
file模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| src dest path mode owner group recurse -yes -true -no -false state -directory -touch -link -hard -absent
ansible web_group -m file -a "path=/opt/test state=directory"
ansible web_group -m file -a "path=/opt/test owner=www group=www mode=777"
ansible web_group -m file -a "path=/opt/abc owner=www group=www mode=777 state=touch"
ansible web_group -m file -a "path=/tmp/aaa/111/adc recurse=true owner=www group=www mode=666 state=directory "
|
copy模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| src dest owner group mode content
backup -yes -true -no -false remote_src -yes -no
ansible web_group -m copy -a "src=/root/www.xxx.conf dest=/opt"
ansible web_group -m copy -a "src=/root/www.xxx.conf dest=/opt backup=true"
ansible web_group -m copy -a "src=/tmp/abc dest=/root remote_src=true"
ansible web_group -m copy -a "content='zls' dest=/tmp/zls.txt"
|
get_url模块
1 2 3 4 5 6 7 8 9 10
| url dest mode checksum md5 sha256
ansible web_group -m get_url -a "url=http://test.driverzeng.com/Nginx_Code/QQ2.8.zip dest=/tmp mode=644"
|
Ansible软件管理模块
yum
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| exclude=kernel*,foo* list=ansible disablerepo="epel,ol7_latest" download_only=true name -file:// -http:// -httpd state -installed -present -removed -absent -latest
ansible web_group -m yum -a "name=httpd state=present"
|
yum repository
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| name description baseurl file name owner group mode gpgkey gpgcheck -yes -no enabled -yes -no state -persent -absent
ansible web_group -m yum_repository -a "name=zls_epel description=EPEL baseurl=https://download.fedoraproject.org/pub/epel/$releasever/$basearch/" -i ./hosts
ansible web_group -m yum_repository -a 'name=zls_epel description=EPEL file=test_zls baseurl=https://download.fedoraproject.org/pub/base/$releasever/$basearch/ gpgcheck=no' -i ./hosts
ansible web_group -m yum_repository -a 'name=zls_epel file=test_zls state=absent' -i ./hosts
ansible web_group -m yum_repository -a 'name=zls_epel description=EPEL file=test_zls baseurl=https://download.fedoraproject.org/pub/base/$releasever/$basearch/ gpgcheck=no mirrorlist=http://mirrorlist.repoforge.org/el7/mirrors-rpmforge enabled=no' -i ./hosts
|
service、systemd模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| name state -started -stopped -restarted -reloaded enabled -yes -true -no -false
ansible web_group -m service -a 'name=nginx state=started enabled=true'
ansible web_group -m service -a "name=crond state=stoped enabled=no"
|
Ansible用户管理模块
user模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| name comment uid group shell append groups password ssh_key_bits: 2048 ssh_key_file: .ssh/id_rsa state -absent -present remove -yes -no create_home -yes -no
ansible web_group -m user -a 'name=www shell=/sbin/nologin create_home=false'
ansible web_group -m user -a "name=zls uid=888 group=888 shell=/sbin/nologin create_home=false"
ansible web_group -m user -a "name=zls uid=888 group=root shell=/bin/bash generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa" -i ./hosts web01 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "comment": "", "create_home": true, "group": 0, "home": "/home/zls", "name": "zls", "shell": "/bin/bash", "ssh_fingerprint": "2048 SHA256:WEMHCpSjxxqFwlzrCk1FqrPqeq6N/SHxL1gFTSqHlGM ansible-generated on web01 (RSA)", "ssh_key_file": "/home/zls/.ssh/id_rsa", "ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRx+bCYGh4FqpKoPzyXrR8ef9GwoY6l6QEFQ0+XPynR22fd9Lbs1eUxWDm5aH4ZO8sPaI8a5xmj88Sipwl0FxlQTjD2X/vreZNEDbwFWrbZ24VvPkfPSSWBh5SxLH6pJt8pGQpPVWuLRMx6yOOxRB1hh9bGFzQNg5z8xqzeogTOoI7cxSFZVuUb5affNj8H5mCw2nAvblV+HNhRzbMlwr+9/EWcCWHDnlVYcELHXjpNJcyGB3VFOu1MPkmLaSTcaB73O0eRvZQkYMBePKJC44tvjHihGhvCk9rzh8qvzHxvMgoMD/+0uKAlIwEvOyfAczb7fxllU0rDtbyPtjbuLsR ansible-generated on web01", "state": "present", "system": false, "uid": 888 } web02 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "comment": "", "create_home": true, "group": 0, "home": "/home/zls", "name": "zls", "shell": "/bin/bash", "ssh_fingerprint": "2048 SHA256:IepfOosi2Xm8kfr4nOPAhG3fec6o8kpMnJ0/RwN+0F8 ansible-generated on web02 (RSA)", "ssh_key_file": "/home/zls/.ssh/id_rsa", "ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDEcO9iDKg4X8ya/y9E0eDelAFMp/rxiDSzW31r+REawaQyF4oywcdIagpz0MTg2BeF2WdaYUmHmtmSTfSOMif26+R1FLcL9f9NYu3io/0388jukcTfyN02diXWgqoKtt4Gbm8Bq8sWE4tX/FSYl42fG6bX1AyDSMzzB7ERr2AD/Y9KuKt7cEXDinGjqTFEXw6+x1wBHpotkUisYiZCci+1Nx4YSznVRBveZTlpxMUYmKgwkUXQIt+RoOYzjgD++0md8O7lwJGgODZkahlrf2pOQnmpS4isLi9or4N+DVnqD+cXb/RjgJzPIJZYazgRY3vtAU9DDqm5i049x/VxEqFj ansible-generated on web02", "state": "present", "system": false, "uid": 888 }
ansible web_group -m debug -a "msg={{ 'zls' | password_hash('sha512', 'salt') }}" -i ./hosts web01 | SUCCESS => { "msg": "$6$salt$gaWhNcZweYlKQcLU1CqyY/UbYqIeUffVz6ESj87aMNfMX.xYBx0Z.67wzLN/hkkxmNut7SvkksPZ2Zlrse98m/" } web02 | SUCCESS => { "msg": "$6$salt$gaWhNcZweYlKQcLU1CqyY/UbYqIeUffVz6ESj87aMNfMX.xYBx0Z.67wzLN/hkkxmNut7SvkksPZ2Zlrse98m/" }
ansible web_group -m user -a 'name=zls1 password=$6$salt$gaWhNcZweYlKQcLU1CqyY/UbYqIeUffVz6ESj87aMNfMX.xYBx0Z.67wzLN/hkkxmNut7SvkksPZ2Zlrse98m/ create_home=true shell=/bin/bash' -i ./hosts
|
group模块
1 2 3 4 5 6 7 8 9 10 11 12 13
| name gid state -present -absent
ansible all -m group -a 'name=www gid=666'
ansible all -m user -a 'name=www uid=666 group=666 shell=/sbin/nologin create_home=false'
|
ansible定时任务模块
cron模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| * * * * * /usr/bin/ntpdata time1.aliyun.com&>/dev/null 00 05 * * * /usr/bin/ntpdata time1.aliyun.com&>/dev/null name minute hour day month weekday job state -present -absent
ansible web_group -m cron -a "minute=* hour=* day=* month=* weekday=* job='/bin/sh /server/scripts/test.sh'" ansible web_group -m cron -a "job='/bin/sh /server/scripts/test.sh'" ansible web01 -m cron -a 'name="time_rsyn" minute=00 hour=05 job="/usr/bin/ntpdata time1.aliyun.com&>/dev/null" state=absent'
ansible web_group -m cron -a "name='cron01' job='/bin/sh /server/scripts/test.sh'"
ansible web_group -m cron -a "name='ansible cron02' minute=0 hour=0 job='/bin/sh /server/scripts/test.sh' state=absent"
ansible web_group -m cron -a "name='ansible cron01' minute=0 hour=0 job='/bin/sh /server/scripts/test.sh' disabled=no"
|
Ansible磁盘挂载模块
mount模块
1 2 3 4 5 6 7 8 9 10 11 12 13
| path src fstype stata -present -mounted -umounted -absent
ansible web01 -m mount -a 'path=/code/wordpress/wp-content/uploads src=172.16.1.31:/data fstype=nfs state=mounted'
|
Ansible解压缩模块
unarchive模块
1 2 3 4 5 6 7 8 9
| src dest owner group mode remote_src -yes -no
|
ansible数据库模块
mysql_user模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| name password host priv login_user login_password state -present -absent MySQL-python
- hosts: mysql_group tasks: - name: 安装Python连接MySQL库 yum: name: MySQL-python state: present - name: 创建mysql test用户 mysql_user: login_user: root login_password: '123' name: test password: '123' host: '%' priv: '*.*:ALL' state: present
vim /etc/my.cnf [mysqld] skip_name_resolve systemctl restart mariadb
|
mysql_db模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| create database wordpress; mysqldump -uroot -p123 -Bwordpress > /tmp/wp.sql
name target src login_user login_password state - present - absent - dump - import
|