Ansible部署

本文详细介绍如何使用Ansible进行基础环境搭建,包括Ansible的安装、配置主机列表、实现免密钥登录及基本模块的使用。通过具体示例,如执行远程命令、文件管理、包管理等操作,帮助读者快速掌握Ansible的基础应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ansible基础环境准备

1、安装ansible

[root@node01 ~]# yum install ansible -y

2、配置主机列表

[root@node01 ~]# cp /etc/ansible/hosts{,.ori}
[root@node01 ~]# vim /etc/ansible/hosts
[webserver]
192.168.43.11

3、小测试

方法一,ssh免密钥登录

方法二,

[root@node01 ~]# cat /etc/ansible/hosts |tail -2
[webserver]
192.168.43.11 ansible_ssh_user=root ansible_ssh_pass=redhat

[root@node01 ~]# vim /etc/ansible/ansible.cfg
修改61行 host_key_checking = False
或者修改357行 ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s StrictHostKeyChecking=no

[root@node01 ~]# ansible webserver -m command -a "free -m"
192.168.43.11 | SUCCESS | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           3776         138        3464           8         172        3443
Swap:          3967           0        3967

[root@node01 ~]# 

4、模块

查找模块

[root@node01 ~]# ansible-doc -l #就相当于linux的所有命令

查看么个模块对应的帮助

[root@node01 ~]# ansible-doc -s command "linux命令参数"

特殊:不支持的东西,例如> < | & 等 &HOME,替代方案用shell模块

ansible命令参数

  -m MODULE_NAME, --module-name=MODULE_NAME
                        module name to execute(default=command)
     模块名字,默认command
  -a MODULE_ARGS, --args=MODULE_ARGS
                        module arguments
    模块参数
  -f FORKS, --forks=FORKS
                        specify number of parallel processes to use
                        (default=5)
   并发进程数,默认5个
   -i INVENTORY (default:/etc/ansible/hosts)
		指定主机列表文件
4.1.command模块(默认模块,可省略)

​ 用于执行命令

[root@node01 ~]# ansible webserver -m command -a "df -h" 
192.168.43.11 | SUCCESS | rc=0 >>
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   36G  999M   35G   3% /
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G     0  1.9G   0% /dev/shm
tmpfs                    1.9G  8.6M  1.9G   1% /run
tmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1                497M  125M  373M  25% /boot
tmpfs                    378M     0  378M   0% /run/user/0

command常用参数

[root@node01 ~]# ansible-doc -s command
- name: Executes a command on a remote node
  command:
      chdir:                 # Change into this directory
                               command.
      creates:               # A filename or (since 2.0) 
                               already exists,
                               this step will
                               *not* be run.
      free_form:             # (required) The command mod
                               command to run.
                               There is no
                               parameter
                               actually named
                               'free form'. See
                               the examples!
      removes:               # A filename or (since 2.0) 
                               does not exist,
                               this step will
                               *not* be run.
      stdin:                 # Set the stdin of the comma
                               specified value.
      warn:                  # If command_warnings are on
                               not warn about
                               this particular
                               line if set to
                               `no'.

chdir=/tmp配置相当于cd /tmp

[root@node01 ~]# ansible webserver -a "chdir=/etc/yum.repos.d/ pwd"
192.168.43.11 | SUCCESS | rc=0 >>
/etc/yum.repos.d

creates=/etc 相当于条件测试 [ -e /etc ]||pwd,如果这个文件存在就不执行pwd

[root@node01 ~]# ansible webserver -a "pwd creates=/etc"
192.168.43.11 | SUCCESS | rc=0 >>
skipped, since /etc exists

removes=/etc 相当于条件测试 [ -d /etc ]||pwd,如果这个文件存在就执行pwd

[root@node01 ~]# ansible webserver -a "pwd removes=/etc"
192.168.43.11 | SUCCESS | rc=0 >>
/root

warn=False忽略警告

[root@node01 ~]# ansible webserver -a "chmod 000 /opt"  
 [WARNING]: Consider using file module with mode rather than running chmod

192.168.43.11 | SUCCESS | rc=0 >>


[root@node01 ~]# ansible webserver -a "chmod 000 /opt warn=False"
192.168.43.11 | SUCCESS | rc=0 >>


4.2.shell模块

​ 功能说明:在远程节点上执行命令

4.3.script模块
4.4.file模块
4.5.yum模块
4.6.systemd模块
4.7.cron模块

在管理机上配置ansible


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值