ansible完全攻略

ansible完全攻略

ansible安装:

apt安装:

apt install ansible  #安装ansible

ansible --version #ansible

ansible简单使用:

ansible 主机列表 参数
主机列表默认值得是 /etc/ansible/hosts
可默认指定为仅可以用localhost但是不隐式指定
ansible localhost -m command -a "参数"  #-m指定模块,默认为command
ex:
ansible localhost -m command -a "ls"  #在本机执行ls命令,可以用-v,-vv,-vvv来显示更加详细的命令执行信息
ansible localhost -m ping #执行ping命令操作

ansible-doc -l #列出所有模块,按Q退出
ansible-doc -h #ansible-doc帮助命令
ansible-doc -s [模块] #显示某个模块的简单参数信息

ex:
root@dokcer:~# ansible localhost -m command -a  "chdir=/tmp pwd" #使用command的chdir参数改变执行命令的目录
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'

localhost | SUCCESS | rc=0 >>
/tmp   #显示已经更改了执行命令的目录

ansible-config -h #显示配置的帮助信息
ansible-config list #以yaml的格式来显示配置信息
ansible-config view #以json的格式来显示配置信息
ansible-config dump #显示所有配置的变量信息

主机清单:

主机清单文件:

文件位置:/etc/ansible/hosts

主机属性:(用于过滤主机)
all 所有主机
ungrouped 所有没有组的主机
localhost 表示本机

主机格式:
	散列主机列表
		主机名/IP地址:[ssh端口]
  主机组列表
		[主机组名]
		主机列表1
		[主机组:children]
		子组1
		子组2
		...
	主机范围
		主机域名[001:006] 
		IP地址[01:60]
		
	
  
 

如果连接过主机但是失败了,会在~/.ssh/known_hosts中有记录,删除之后重新连接就可以了。

报错:

to use the 'ssh' connection type with passwords, you must install the sshpass program
sudo apt-get install sshpass  #安装sshpass模块

PermitRootLogin yes #ansible默认执行命令的用户为root,设置为root可以登录

ansible连接主机的属性

192.168.0.50 ansible_connection=local  #设置主机连接方式为本地连接
ansible 192.168.0.50 -a "ls" #不用输入密码就可以连接,相当于localhost方式

192.168.0.50 ansible_ssh_pass=ssh登录密码 
ansible 192.168.0.50 -a "ls" #不用输入ssh密码就可以输入命令了

ansible免密钥认证:
1.生成密钥对
root@dokcer:~# ssh-keygen -t rsa   
2.发送控制端的公钥到目标主机
ssh-copy-id -i /root/.ssh/id_rsa.pub lion@192.168.0.50  #输入lion用户的密码就传输完成了

error:
No such file or directory #没有.ssh文件夹
lion@dokcer:~$ ssh localhost  #连接本地生成ssh文件夹
3.实现免密码登录

ansible配置文件:

配置文件目录:

root@dokcer:/root/.ssh# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg  #核心配置文件
└── hosts        #主机列表文件
└── roles        #角色管理文件

核心配置文件为ansible.cfg
通过修改
1.环境变量$ANSIBLE_CONFIG #通过环境变量来定义配置文件的位置
2.工作目录    #仅限当前目录
3.用户家目录  #仅限当前系统用户
4.软件目录    #全局生效

配置格式:
[配置段]
配置项:属性值

root@dokcer:~# egrep -v '^$|^#' /etc/ansible/ansible.cfg  #过滤掉空格和#号显示
[defaults]
[inventory]
[privilege_escalation]
[paramiko_connection]
[ssh_connection]
[persistent_connection]
[accelerate]
[selinux]
[colors]
[diff]

[defaults]                                     #默认配置

# some basic default values...

#inventory      = /etc/ansible/hosts            #主机配置列表
#library        = /usr/share/my_modules/        #模块位置
#module_utils   = /usr/share/my_module_utils/
#remote_tmp     = ~/.ansible/tmp                #远程主机临时执行命令目录          
#local_tmp      = ~/.ansible/tmp                #本地主机临时执行命令目录
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks          = 5
#poll_interval  = 15
#sudo_user      = root                             
#ask_sudo_pass = True
#ask_pass      = True
#transport      = smart
#remote_port    = 22                            #ssh端口
#remote_user    = root                          #远程登录用户
#module_lang    = C
#module_set_locale = False

ansilbe利用指定用户提权执行命令

ansible localhost -u lion -a "ls /root" -b -K  #输入sudo密钥后执行超级管理员命令

设置某一个用户拥有root权限

root@dokcer:~# cat /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL          #admin组用用户拥有root权限
# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL     #sudo组用户拥有sudo来执行任何命令

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d
root@dokcer:~#  grep sudo /etc/gshadow #在gshandown文件中查看sudo组的成员
sudo:*::lion
root@dokcer:~# usermod -G sudo lion  #增加某一用户到sudo组中

%sudo   ALL=(ALL:ALL) ALL  / %sudo   ALL=(ALL:ALL) NOPASSWD:ALL  #默认切换sudo执行命令时不需要输入密码

root@dokcer:~# ansible localhost -u lion -a "ls /root" -b   #不用-K输入密码就可以执行超级命令
localhost | SUCCESS | rc=0 >>
snap

ansible主机匹配:

匹配所有: all
正则匹配 *(通配符)
逻辑或 :(并集),一般由于主机组
逻辑或 :&(交集),一般由于主机组
逻辑非 :!(补集),一般由于主机组

root@dokcer:/root/.ssh# ansible '*' -m ping -k  #匹配所有主机进行ping,用单引号括住主机列表部分
SSH password:
192.168.0.50 | SUCCESS => {
   
    "changed": false,
    "ping": "pong"
}

ansible命令模块:

命令模块
command:默认模块,可以远程权限运行所有的shell命令,不支持特殊符号,但是支持系统变量,不支持命令别名
shell:用于执行某些带特殊符号的命令<>!#$
scripts:可以执行脚本文件,也可以从控制主机的脚本运行到远程主机

**command模块:**
root@dokcer:~# ansible localhost -a "chdir=/tmp ls"  #切换执行命令的目录

root@dokcer:~# ansible localhost -a 'chdir=/tmp creates=1.txt ls'  #如果creates文件存在,则跳过命令执行,反之亦然
localhost | SUCCESS | rc=0 >>
skipped, since 1.txt exists

root@dokcer:~# ansible localhost -a 'chdir=/tmp removes=1.txt ls'  #如果removes的文件存在则执行命令,反之亦然
localhost | SUCCESS | rc=0 >>
1.txt
ansible_VtVGmv
snap.docker
systemd-private-66d00680e20c4dba801130d9387e8e6f-systemd-resolved.service-KVfCH2
systemd-private-66d00680e20c4dba801130d9387e8e6f-systemd-timesyncd.service-xhgHcZ

root@dokcer:~# ansible localhost -a 'echo $SHELL'  #command值支持系统变量不支持自定义变量
localhost | SUCCESS | rc=0 >>
/bin/bash

root@dokcer:~# ansible localhost -a 'la=lala; echo $la'
localhost | FAILED | rc=2 >>
[Errno 2] No such file or directory

root@dokcer:~# ansible localhost -a 'env | grep 1'   #command不支持特殊管道命令
localhost | FAILED | rc=127 >>
env: ‘|’: No such file or directorynon-zero return code

**shell模块**:
root@dokcer:~# ansible localhost -m shell -a "la=lala; echo $la"  #双引号有特殊符号时不生效
localhost | SUCCESS | rc=0 >>

root@dokcer:~# ansible localhost -m shell -a 'la=lala; echo $la'  #单引号命令成功
localhost | SUCCESS | rc=0 >>
lala

root@dokcer:~# ansible localhost -m shell -a 'env | grep $SHELL'  #shell执行管道符命令
localhost | SUCCESS | rc=0 >>
SUDO_COMMAND=/bin/bash
SHELL=/bin/bash

root@dokcer:~# ansible localhost -m shell -a '/bin/bash /tmp/1.sh admin' #shell执行远程脚本
localhost | SUCCESS | rc=0 >>
user is admin

**script模块:**

root@dokcer:~# ansible 192.168.0.50 -m script -a '/bin/bash /tmp/1.sh amdin' -kSSH password:
192.168.0.50 | SUCCESS => {
   
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.0.50 closed.\r\n",
    "stdout": "user is amdin\r\n",
    "stdout_lines": [
        "user is amdin"                         #可以执行存在于控制端,但是不存在被控端的脚本文件
    ]
}

#script使用executable参数来指定不存在于被控端的脚本文件
root@dokcer:~# ansible 192.168.0.50 -m script -a 'executable=/bin/bash /tmp/1.sh amdin' -k
SSH password:
192.168.0.50 | SUCCESS => {
   
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.0.50 closed.\r\n",
    "stdout": "user is amdin\r\n",
    "stdout_lines": [
        "user is amdin"
    ]
}

ansible系统模块:

hostname模块
修改主机名,立刻生效,并且永久生效,hostname只会修改/etc/hostname,而不会修改/etc/hosts
root@dokcer:~# ansible localhost  -m hostname -a 'name=lion'  #修改hostname
localhost | SUCCESS => {
   
    "ansible_facts": {
   
        "ansible_domain": "",
        "ansible_fqdn": "lion",
        "ansible_hostname": "lion",
        "ansible_nodename": "lion"
    },
    "changed": true,
    "name": "lion"
}
root@dokcer:~# ansible localhost -a 'hostname'  #修改成功
localhost | SUCCESS | rc=0 >>
docker

user
创建一个用户,name lion2 系统输入,用户组是root, uid 10010 注释是lion2 禁止登录  state=presen状态为存在
root@dokcer:~# ansible localhost -m user -a 'name=lion2 system=yes groups=root uid=10010 comment=lion2 shell=/sbin/nologin state=present'
localhost | SUCCESS => {
   
    "changed": true,
    "comment": "lion2",
    "create_home": true,
    "group": 999,
    "groups": "root",
    "home": "/home/lion2",
    "name": "lion2",
    "shell": "/sbin/nologin",
    "state": "present",
    "system": true,
    "uid": 10010
}

root@dokcer:~# ansible localhost -a "id lion2"  #创建用户成功,要注意uid
localhost | SUCCESS | rc=0 >>
uid=10010(lion2) gid=999(lion2) groups=999(lion2),0(root)

root@dokcer:~# ansible localhost -a "getent passwd lion2" #查看lion2用户的密码
localhost | SUCCESS | rc=0 >>
lion2:x:10010:999:lion2:/home/lion2:/sbin/nologin

#创建带ssh私钥的用户
root@dokcer:~
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值