ad-hoc
- 什么是ad-hoc
ad-hoc简而言之就是“临时命令”,执行完即结束,并不会保存 - 使用场景
比如在多台机器上查看某个进程是否启动,或拷贝指定文件到本地等等
状态状态解释:
黄色: 表示命令执行成功并发生了变化
绿色: 表示执行成功,但是文件没有发生变化,不会实际执行任务,效果等同于执行成功
红色: 表示执行失败
紫色: 表示警告,提示所执行的命令有专有的模块可以使用
蓝色: 表示执行的详细过程
常用模块
参考:
ansible官方文档
ansible_all_ipv4_addresses:仅显示ipv4的信息。
ansible_devices:仅显示磁盘设备信息。
ansible_distribution:显示是什么系统,例:centos,suse等。
ansible_distribution_major_version:显示是系统主版本。
ansible_distribution_version:仅显示系统版本。
ansible_machine:显示系统类型,例:32位,还是64位。
ansible_eth0:仅显示eth0的信息。
ansible_hostname:仅显示主机名。
ansible_kernel:仅显示内核版本。
ansible_lvm:显示lvm相关信息。
ansible_memtotal_mb:显示系统总内存。
ansible_memfree_mb:显示可用系统内存。
ansible_memory_mb:详细显示内存情况。
ansible_swaptotal_mb:显示总的swap内存。
ansible_swapfree_mb:显示swap内存的可用内存。
ansible_mounts:显示系统磁盘挂载情况。
ansible_processor:显示cpu个数(具体显示每个cpu的型号)。
ansible_processor_vcpus:显示cpu个数(只显示总的个数)。
- shell(Execute shell commands on targets)
- script(Runs a local script on a remote node after transferring it)
- yum
- yum_repository
- group
- user
- copy
- file
- service
- mount
- cron
- get_url(Downloads files from HTTP, HTTPS, or FTP to node)
- firewalld
- selinux
- lineinfile
template(Templates are processed by the Jinja2 templating language.)剧本中的templates
template 可以在远程主机创建原来不存在的文件,如本例子file.conf文件在远程主机不存在,执行后能创建该文件
synchronize(rsync的一个包装器,可以让你的剧本中的常见任务变得简单快捷。)
[root@m01 ~]# ansible-doc -l # 查看所有模块说明
[root@m01 ~]# ansible-doc copy # 查看指定模块方法
[root@m01 ~]# ansible-doc -s copy # 查看指定模块参数
例子
-
name: Add Base Yum Repository
yum_repository:
name: base
description: Base Aliyun Repository
baseurl: http://mirrors.aliyun.com/centos/ r e l e a s e v e r / o s / releasever/os/ releasever/os/basearch/
gpgcheck: yes
gpgkey: http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 -
name: Add Epel Yum Repository
yum_repository:
name: epel
description: Epel Aliyun Repository
baseurl: http://mirrors.aliyun.com/epel/7/$basearch
gpgcheck: no -
name: Add Nginx Yum Repository
yum_repository:
name: nginx
description: Nginx Repository
baseurl: http://nginx.org/packages/centos/7/$basearch/
gpgcheck: no
when: (ansible_hostname is match(“web*”)) or
(ansible_hostname is match(“lb*”)) -
name: Add PHP Yum Repository
yum_repository:
name: php71w
description: php Repository
baseurl: http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck: no
when: (ansible_hostname is match(‘web*’)) -
name: Installed Packages
yum: name={{ packages }} state=present
vars:
packages:
- rsync
- nfs-utils
- inotify-tools -
name: Stop Firewalld Service
service: name=firewalld state=stopped enabled=no -
name: Disable Selinux
selinux: state=disabled -
name: Configure SSH Server
template: src=sshd.conf.j2 dest=/etc/ssh/sshd_config
notify: Restart SSHD Server -
name: Add Group WWW
group: name=www gid=666 -
name: Add User WWW
user: name=www uid=666 group=www create_home=no shell=/sbin/nologin -
name: Create Code Directory
file: path=/code state=directory owner=www group=www
when: (ansible_hostname is match(‘web*’)) -
name: Copy Rsync Backup Scripts
copy: src=./files/push_data_rsync.sh dest=/server/scripts/ mode=755
when: (ansible_hostname != “backup”) -
name: Configure Crontab
cron:
name: Rsync Backup
minute: ‘*/5’
job: /bin/bash /server/scripts/push_data_rsync.sh &>/dev/null
when: (ansible_hostname != “backup”)
使用了register注册变量,但是注册的变量只能针对当前主机生效。通过设置全局的fact变量来实现跨主机变量的使用,从而自动获取到logfile和pot。
#将logfile注册成变量。
- name: Get logfile
shell: mysql -uroot -p123456 -e "show master status;"|awk 'NR==2{print $1}'
register: file
when: ( ansible_hostname is match "mysql01" )
#将pot注册成变量
- name: master-bin-pot
shell: mysql -uroot -p123456 -e "show master status;"|awk 'NR==2{print $2}'
register: pot
when: ( ansible_hostname is match "mysql01" )
通过set_fact模块来设置全局的fact变量
- name: set facts file
set_fact: masterbin={{ file.stdout_lines[0] }} <--将获取到的logfile赋值给masterbin
when: ( ansible_hostname is match "mysql01" )
- name: set facts position
set_fact: position={{ pot.stdout_lines[0] }} <--将获取到的pot赋值给position
when: ( ansible_hostname is match "mysql01" )
从库上执行change master to 命令
- name: Changemaster to
mysql_replication:
login_user: root
login_password: '123456'
mode: changemaster
master_user: web
master_password: '123456'
master_host: 172.16.1.51
master_log_file: "{{ hostvars['172.16.1.51']['masterbin'] }}" <--引用51的主机变量
master_log_pos: "{{ hostvars['172.16.1.51']['position'] }}" <--引用51的主机变量
when: ( ansible_hostname is match "mysql02" )