一、常用命令
语法:
ansible all(受管主机名) -m shell(模块) -a ‘cat x.yml’(传递参数)
查找相关的模块:
[student@workstation ansible]$ ansible-doc -l |grep yum
yum Manages packages with the `yum’ package manage…
yum_repository Add or remove YUM repositories
查看模块帮助文档:
[student@workstation ansible]$ ansible-doc yum_repository
二、常用模块
1.超级模块:(无状态检查)
command:不可执行重定向、传送
shell:可执行重定向、传送
raw:可绕过子模块,调用远程shell,常用与网络设备等无法安装python程序的设备
2.文件模块:
copy:复制文件,管理机—>被管机
fetch:复制文件,管理机<—被管机
lineinfile:确保特定行在文件中
synchronize:使用rsync同步内容
3.软件包模块:
yum、apt、dnf:使用对应软件包管理器管理软件包
package:使用系统本机的自动检测软件包管理器管理软件包
gem:管理ruby gem
pip:从pypi管理python软件包
4.系统模块:
firewalld:管理防火墙端口和服务
reboot:重启
service:管理服务
user:用户相关操作
5.网络模块:
get_url:通过http、https、ftp下载文件
nmcli:管理网络
uri:与web服务交互
- 磁盘工具模块
Parted:
Lvg:
Lvol:
Filesystem:
三、ansible命令行选项
查看帮助文档获取:ansible —help
inventory>>-i
remote_user>>-u
become>>-b
become_method>>—become-method
become_user>>—become-user
become_ask_pass>>-K
四、使用 Ansible 临时命令编写shell 脚本在各个受管节点上安装 yum 存储库(考题)
[student@workstation ansible]$ vim adhoc.sh
#!/bin/bash
ansible all -m yum_repository -a ‘ name=EX294_BASE description=”EX294 base software” baseurl=http://content/rhel8.0/x86_64/dvd/BaseOS gpgcheck=yes gpgkey=http://content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release enabled=yes’
ansible all -m yum_repository -a ‘ name=EX294_STREAM description=”EX294 stream software” baseurl=http://content/rhel8.0/x86_64/dvd/AppStream gpgcheck=yes gpgkey=http://content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release enabled=yes’
[student@workstation ansible]$ chmod a+x adhoc.sh —添加执行的权限
[student@workstation ansible]$ ./adhoc.sh