ansible

ansible介绍

ansible是用python编写的,允许管理员批量在远程主机上执行命令.

安装

1. 安装 epel 源

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo 

2. 安装 ansible

yum install ansible -y

ansible 命令格式

Usage: ansible <host-pattern> [options]
-a MODULE_ARGS, --args=MODULE_ARGS #模块的参数
-C, --check #会执行,但是不做任何的改变,干跑
-f FORKS, --forks=FORKS #指定进程数,做并发
--list-hosts #列出主机
-m MODULE_NAME # 模块名称
--syntax-check #检查语法
-k, --ask-pass #指定密码

查看ansible 安装生成的文件

rpm -ql ansible | more

/etc/ansible
/etc/ansible/ansible.cfg  #配置文件

/etc/ansible/hosts #主要文件,添加远程主机,可以设置分组

除了能够添加远程主机,hosts文件中还可以做如下配置:

ansible_ssh_host
  ansible通过ssh连接的IP或者FQDN
ansible_ssh_port
  SSH连接端口
ansible_ssh_user
  默认SSH连接用户
ansible_ssh_pass
  SSH连接的密码(这是不安全的,ansible极力推荐使用--ask-pass选项或使用SSH keys)
ansible_sudo_pass
  sudo用户的密码
ansible_connection
  SSH连接的类型:local,ssh,paramiko,在ansible 1.2之前默认是paramiko,后来智能选择,优先使用基于ControlPersist的ssh(支持的前提)
ansible_ssh_private_key_file
  SSH连接的公钥文件

ansible 第一个命令

ansible 远程主机ip -m ping #ping单个主机
ansible 分组 -m ping #指定分组
ansible all -m ping #hosts文件中的所有主机

host-pattern的格式
1.单个主机
2.多个主机,主机间以逗号隔开
3.单个组
4.多个组
  .交集 '分组1:&分组2'
  .并集 '分组1:分组2' 或 分组1,分组2
  .差集 '分组1:!分组2' #分组1有,分组2没有
5.所有主机

ansible 模块

#显示模块全部信息
ansible-doc -l
#统计ansible 的模块数量
ansible-doc -l | wc -l #2080

command

[root@localhost ~]# ansible-doc -s command
- name: Executes a command on a remote node
  command:
      argv:                  # Allows the user to provide the command as a list vs. a string.
                               Only the string or the list form can
                               be provided, not both.  One or the
                               other must be provided.
      chdir:                 # Change into this directory before running the command.
      creates:               # A filename or (since 2.0) glob pattern. If it already exists, this
                               step *won't* be run.
      free_form:             # (required) The command module takes a free form command to run.
                               There is no parameter actually named
                               'free form'. See the examples!
      removes:               # A filename or (since 2.0) glob pattern. If it already exists, this
                               step *will* be run.
      stdin:                 # Set the stdin of the command directly to the specified value.
      warn:                  # If command_warnings are on in ansible.cfg, do not warn about this
                               particular line if set to `no'.
command的帮助信息
chdir 切换目录
ansible all -a "chdir=/tmp pwd" #-m 默认为command,可以不写
creates #判断目录是否存在,存在不执行,不存在执行
ansible all -a "creates=/tmp pwd" #不执行
ansible all -a "creates=/tmp2 pwd" #执行
removes #与creates相反,存在就执行,不存在就不执行
ansible all -a "removes=/tmp pwd" #执行
ansible all -a "removes=/tmp2 pwd" #不执行

 给用户设置密码

ansible web -m command -a "echo 'pwd'|passwd --stdin user"
当适用command模块执行这条命令时,是不成功的,原因是
那怎么办呢?
接下来我们看看shell 模块

 shell

chdir 切换目录
ansible all -m shell -a "chdir=/tmp pwd"  
creates #判断目录是否存在,存在不执行,不存在执行
ansible all -m shell -a "creates=/tmp pwd" #不执行
ansible all -m shell -a "creates=/tmp2 pwd" #执行
removes #与creates相反,存在就执行,不存在就不执行
ansible all -m shell -a "removes=/tmp pwd" #执行
ansible all -m shell -a "removes=/tmp2 pwd" #不执行
shell参数

用来执行远程主机上的命令或者脚本

给用户设置密码那条命令,就可以用shell实现

执行脚本文件:

script

script也是用来执行脚本文件,与shell不同的是,script执行的是 本地的脚本

 

posted @ 2019-04-10 17:50  yara-ning  阅读(193)  评论(0编辑  收藏  举报