目录

一、ansible的配置文件和清单文件

二、ansible的模块

三、实际操作命令

一、ansible的配置文件和清单文件

(一)看ansible的配置文件

1、命令:ansible --version

2、一般的配置文件是:/etc/ansible/ansible.cfg

PS:配置文件的优先级:/home/xiaoming/ansible.cfg > /home/xiaoming/.ansible.cfg > /etc/ansible/ansible.cfg/

3、配置文件的内容:

[defaults]
inventory = ./inventory        ;指定清单文件
remote_user = xiaoming        ;指定连接受控主机的xiaoming用户
ask_pass = false            ;是否提示输入ssh密码。如果使用ssh公钥身份认证,则可以是false

[privilege_escalation]
become=True                    ;登录到受控主机后是否切换用户
become_method=sudo            ;可以使用su或者sudo的方式切换用户,
become_user=root            ;切换到哪个用户
become_ask_pass=False        ;是否为become_method提示输入密码,默认为false
注:如果become_method=sudo,需要提前在受控节点设置用户的sudo配置

(二)清单文件(inventory)

默认清单文件是/etc/ansible/hosts

指定使用某个清单文件:ansible --list all -i inventory

二、ansible的模块

文件模块

copy:本地文件复制到受管主机

file:设置文件的权限和其他属性

lineinfile:确保特定行是否在文件中

synchronize:使用rsync同步内容

软件包模块

yum:使用yum软件包管理器管理软件包

dnf:使用dnf软件包管理器管理软件包

apt:使用apt软件包管理器管理软件包

pip:从pypi管理python软件包

yum仓库模块

yum_repository:使用该模块部署yum仓库

系统模块

user:管理用户账户

openssh_keypair:生成openssh的公私钥

authorized_key:添加或者删除

authorized_key firewalld:使用firewalld管理任意端口和服务

reboot:重新启动计算机

service:管理服务

mysql_db:添加或者删除数据库

mysql_user:从mysql数据库添加或者删除用户

parted:对磁盘进行分区

filesystem:格式化

mount:挂载

Net Tools模块

nmcli:管理网络

get_url:通过http、https或ftp下载文件

uri:与web服务交互

命令模块

command:执行shell命令的模块

shell:执行shell命令的模块(支持管道符和重定向)

三、实际操作命令

1、以all主机组为目标执行id命令

ansible windows 客户端使用方法 ansible become_user_配置文件

 2、使用copy模块修改所有主机上的/etc/motd文件内容为welcome to ansible

ansible windows 客户端使用方法 ansible become_user_运维_02

 3、使用command模块查看/etc/motd文件的内容

ansible windows 客户端使用方法 ansible become_user_配置文件_03

 4、使用user模块创建用户wukong,uid为2000

ansible windows 客户端使用方法 ansible become_user_linux_04

 5、使用yum模块安装httpd软件包并使用service模块启动该服务

ansible windows 客户端使用方法 ansible become_user_配置文件_05

ansible windows 客户端使用方法 ansible become_user_配置文件_06

都看到这里了,创作不易,大家点个赞再走呗!! ( ˃̶̤́ ꒳ ˂̶̤̀ )