ad-hoc
与常用模块ad-hoc
就是临时命令,执行完成即结束,并不会保存
可以查看多台节点是的进程是否存在
拷贝指定文件至本地
使用范例 1 ansible 'groups' -m command -a "df -h"
ad-hoc
执行过程1 2 3 4 5 6 7 1.加载配置文件,默认 /etc/ansible/ansible.cfg 2.读取inventory 3.操作对应的目标主机组;如果组不存在则报错 4.构建对应的py文件,推送到远程目标主机 5.远程主机执行该文件 6.执行完成后,删除对应的py文件 7.像服务端返回最终执行结果
执行状态 1 2 3 4 返回结果的颜色说明 绿色: 代表被管理端主机没有被修改 黄色: 代表被管理端主机发现变更 红色: 代表出现了故障,注意查看提示
单独项目使用ansible
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 cd ~mkdir project1 cd project1cp /etc/ansible/ansible.cfg . cp /etc/ansible/hosts hosts_group vim ansible.cfg ... inventory = ./hosts_group ... ansible --version ansible all -m ping
Ansible
常用模块常用模块较多,可以使用ansible-doc 模块名
查看帮助
command模块 功能:在远程主机执行shell命令,是一个默认模块,可以忽略参数 -m
,但是不支持管道命令 |
参数
选项
含义
chdir
chdir /opt
执行ansible时,切换到指定的目录
creates
creates /data/file
如果文件在,则跳过执行
removes
removes /data/file
如果文件存在,则执行
1 2 3 4 5 6 7 ansible all -m command -a "creates=/data/opt ifconfig eth0" ansible all -m command -a "removes=/data/opt ifconfig eth0"
shell
模块command支持在这个模块一样能执行,被控端已有的shell
命令都可以执行,且支持管道
yum
模块1 2 3 4 5 6 7 8 9 name: state: present absent latest enablerepo disablerepo excludekernel
使用
1 2 3 4 5 6 7 8 9 10 11 ansible all -m yum -a 'name=vsftpd state=present' ansible all -m yum -a 'name=vsftpd state=absent' ansible all -m yum -a 'name=httpd state=present enablerepo=epel' ansible all -m yum -a 'name=* state=present exclude="kernel*"'
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 28 src: 控制端的源文件路径 dest: 被控端的文件路径 owner: 属主 group: 属组 mode: 权限 backup: 备份 validate: 验证 content: 往一个文件写入内容 ansible all -m copy -a 'src=./exports.j2 dest=/etc/exports owner=root group=root mode=0644 backup=yes' ansible all -m copy -a 'content="123" dest=/data/test.txt owner=root group=root mode="0600" backup=yes' cat tt.yaml - hosts: all tasks: - name: Copy a "sudoers" file on the remote machine for editing copy: src: ./sudoers dest: /etc/sudoers validate: /usr/sbin/visudo -csf %s ansible-ploybook tt.yamy
systemd
模块1 2 3 4 5 6 7 8 9 10 11 12 13 14 name state started stopped restarted reloaded enabled daemon_reload: yes ansible all -m systemd -a "name=nfs state=started enabled=yes" ansible all -m systemd -a "name=nfs state=stopped enabled=no"
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 file: path: 在被控端创建的路径 owner: 属主 group: 属组 mode: 权限 state: 类型 touch: 文件 directory: 目录 link: 软链接 hard:硬链接 recurse: yes 递归授权 gropuadd -g 888 www useradd -u 888 -g 888 www ansible webservers -m file -a 'path=/data/www owner=www group=www mode="0755" state=directory recurse=yes' ansible webservers -m file -a 'path=/data/www/books.html owner=www group=www mode="0644" state=touch'
group
模块1 2 3 4 5 6 7 8 9 10 11 12 name: 指定组名称 gid: 指定gid state: present:创建 默认 absent:删除 ansible webservers -m group -a 'name=www gid=666 state=present' ansible webservers -m group -a 'name=mysqldb system=yes state=present'
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 user: name: 创建的名称 uid: 指定uid group: 指定基本组 shell: 登录shell类型默认/bin/bash create_home 是否创建家目录 password 设定对应的密码,必须是加密后的字符串才行,否则不生效; system 系统用户 groups: admins,dev 附加组 append: yes 追加 state: absent 删除 remove: yes 家目录一起结束 ansible webservers -m user -a 'name=www uid=666 group=www shell=/sbin/nologin create_home=no' ansible webservers -m user -a 'name=db group=root groups=adm,sys append=yes shell=/bin/bash create_home=yes' ansible localhost -m debug -a "msg={{ '123' | password_hash('sha512','salt')}}" ansible webservers -m user -a 'name=ddd password=$6$salt$jkHSO0tOjmLW0S1NFlw5veSIDRAVsiQQMTrkOKy4xdCCLPNIsHhZkIRlzfzIvKyXeGdOfCBoW1wJZPLyQ9Qx/1 shell=/bin/bash create_home=yes' ansible webservers -m user -a 'name=dev generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa'
mount
模块1 2 3 4 5 6 7 8 9 10 11 12 src: 源设备路径,或网络地址; path: 挂载至本地哪个路径下; fstype: 设备类型; nfs opts: 挂载的选项 state: 挂载还是卸载 present 永久挂载,但没有立即生效 absent 卸载,临时挂载+永久挂载 mounted 临时(fstab)挂载 unmounted 临时卸载 ansible 172.16.1.8 -m mount -a 'src=172.16.1.7:/data path=/opt fstype=nfs opts=defaults state=mounted'
cron
模块1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 name: 描述信息,描述脚本的作用 minute: 分钟 hour: 小时 weekday: 周 user: 该任务由哪个用户取运行;默认root job: 任务 ansible webservers -m cron -a 'name="backups app data scripts" hour=03 minute=00 job="/bin/bash /scripts/client_push_data_server.sh &>/dev/null"' ansible webservers -m cron -a 'name="backups app data scripts" hour=03 minute=00 job="/bin/bash /scripts/client_push_data_server.sh &>/dev/null" state=absent' ansible webservers -m cron -a 'name="backups app data scripts" hour=03 minute=00 job="/bin/bash /scripts/client_push_data_server.sh &>/dev/null" disabled=yes'
get_url
1 2 3 4 5 6 7 8 9 10 11 get_url: url: 下载地址 dest: 下载到本地的路径; mode: 权限; checksum:对资源做校验; ansible webservers -m get_url -a 'url=http://nginx.org/download/nginx-1.20.2.tar.gz dest=/tmp mode=0666' ansible webservers -m get_url -a 'url=http://nginx.org/download/nginx-1.20.2.tar.gz dest=/opt mode=0666 checksum=md5:3bcc5ccdc052c35d0d3c5557cf56c7d2'
unarchive
模块1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 unarchive: src: 控制端的源文件 dest: 解压到被控端的路径 remote_src: yes 源文本是否在被控端,yes则在,no则不在 ansible webservers -m unarchive -a 'src=./test.tar.gz dest=/mnt' ansible webservers -m unarchive -a 'src=/tmp/test.tar.gz dest=/mnt remote_src=yes' ansible webservers -m archive -a 'path=/opt dest=/mnt/opt.tar.gz format=gz'
selinux
模块1 2 selinux 防火墙模块: ansible webservers -m selinux -a 'state=disabled'
firewalld
模块1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 zone: 要操作的区域 默认public source : 来源地址 service: 服务名称 http,https,sshd,...... port: 端口 permanent: 永久生效,但不会立即生效 immediate: 临时生效; state: 启用和关闭; disabled enabled ansible webservers -m systemd -a 'name=firewalld state=started' ansible webservers -m firewalld -a 'port=80/tcp immediate=yes state=present' ansible webservers -m systemd -a 'name=firewalld state=started' ansible webservers -m firewalld -a 'service=https immediate=yes state=present'
iptables
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 iptables: table: 表 chain: 链 source : 来源IP destination 目标IP destination_port 目标端口 protocol 协议 jump: DROP 动作 action 如何添加规则 insert:插入 append:追加 ansible webservers -m iptables -a 'table=filter chain=INPUT source=192.168.1.1/32 destination=1.1.1.1 destination_port=80 protocol=tcp jump=DROP action=insert' DNAT: 如果请求1.1.1:80端口,则DNAT到2.2.2.2:8800 ansible webservers -m iptables -a 'table=nat chain=PREROUTING protocol=tcp destination=1.1.1.1 destination_port=80 jump=DNAT to_destination="2.2.2.2:8800"' DNAT: 如果请求1.1.1:81端口,则DNAT到3.3.3.3:8800 ansible webservers -m iptables -a 'table=nat chain=PREROUTING protocol=tcp destination=1.1.1.1 destination_port=81 jump=DNAT to_destination="3.3.3.3:8800"' SNAT: POSTROUTING iptables -t nat -I POSTROUTING -s 172.16.1.0/24 -j SNAT --to-source 5.5.5.5 ansible webservers -m iptables -a 'table=nat chain=POSTROUTING source=172.16.2.0/24 jump=SNAT to_source=6.6.6.6' ansible webservers -m iptables -a 'table=nat chain=POSTROUTING source=172.16.3.0/24 jump=SNAT to_source=7.7.7.7 action=insert'
yum_repo
1 2 3 4 5 6 7 8 9 yum_repository name 名称,文件名称 description 描述,必填 baseurl 仓库的地址 gpgcheck 验证开启 gpgkey ansible webservers -m yum_repository -a 'name=ansible_nginx description=xxx baseurl="http://nginx.org/packages/centos/$releasever/$basearch/" gpgcheck=yes gpgkey="https://nginx.org/keys/nginx_signing.key"'
hostname
1 2 3 4 hostname 修改主机名称: name: 修改后的主机名称; ansible webservers -m hostname -a 'name=web_cluster'
sysctl
1 2 3 4 5 6 7 sysctl 修改内核参数模块 - sysctl: name: vm.swappiness value: '5' state: present ansible webservers -m sysctl -a 'name=net.ipv4.ip_forward value=1 state=present'
lineinfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 lineinfile 替换|追加|删除 path: 被控端的路径 regexp: '^Listen ' 正则匹配语法格式 line: Listen 8080 填充的内容 state: absent 删除 insertafter: '^#Listen ' insertbefore: '^www.*80/tcp' ansible webservers -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf regexp="^Listen" line="Listen 8080"' ansible webservers -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth1 line="GATEWAY=172.16.1.200"' ansible webservers -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth1 regexp="^GATEWAY" state=absent' ansible webservers -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth1 insertafter="ONBOOT=yes" line="GATEWAY=172.16.1.200"' ansible webservers -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth1 insertbefore="ONBOOT=yes" line="test=172.16.1.200"'