ansible基本使用

环境信息:

| 系统版本 | 主机名/IP | Python版本 |
|–|–|–|–|
| CentOS 7.7.1908 | ansible/192.168.86.11 | 2.7.5 |
| CentOS 7.7.1908 | compute-1/192.168.86.13 | 2.7.5 |
| CentOS 7.7.1908 | compute-2/192.168.86.14 | 2.7.5 |

ansible安装要求:
server端: python2.6/2.7/3.x
client端: python2.6/2.7/3.x 并且支持ssh

一、ansible安装配置
1>ansible主机操作
安装ansible
yum -y install ansible

配置首次连接免交互
sed -i 's/#host_key_checking/host_key_checking/g' /etc/ansible/ansible.cfg
2>配置hosts验证ansible功能
ssh密码认证方式:
192.168.86.13 ansible_ssh_user=root ansible_ssh_pass=duan@1994

密钥认证格式:
生成密钥:
ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa 

拷贝到目标主机
ssh-copy-id root@192.168.86.13
或者
cat ~/.ssh/id_rsa.pub在目标主机用户的cat ~/.ssh/authorized_keys中添加ansible server端的公钥
192.168.86.13
对不同主机进行分组并添加到inventory

cat << EOF >> /etc/ansible/hosts
[web]
192.168.86.13 ansible_ssh_user=root ansible_ssh_pass=duan@1994
[db]
192.168.86.14 ansible_ssh_user=root ansible_ssh_pass=duan@1994
EOF

针对所有主机进行操作
ansible all -a "hostname"
使用组进行操作
ansible web -a "hostname"
ansible db -a "hostname"
针对具体主机进行操作
ansible 192.168.86.13 -a "hostname"
3>ansible配置文件分析
cat /etc/ansible/ansible.cfg
[defaults]
inventory = /etc/ansible/hosts  #指定被管理端主机清单
forks = 5   #指定ansible执行任务时并发数量
sudo_user = root  #普通用户提权为root用户  
remote_port  = 22  #目标主机默认ssh端口
host_key_checking = False  #主机key检查,避免首次ssh提示yes/no交互
timeout = 10  #连接主机超时时间
log_path = /var/log/ansible.log  #ansible基本操作日志
private_key_file = /root/.ssh/id_rsa  #指定密钥认证的私钥

bin_ansible_callbacks = True #控制是否启用 Ansible 的二进制回调。这通常用于某些类型的高级集成
display_ok_hosts = false     #此设置控制在执行 playbook 时,对于执行成功的主机是否应显示详细的反馈。将它设置为 false 可以使输出更简洁
stdout_callback = community.general.unixy  #插件将使输出看起来更接近于 Unix 风格,结果将不会有额外的装饰符号,而是简单的以纯文本形式输出
二、变量使用

变量优先级:主机变量>主机组变量>yml定义变量

1>主机变量
在web组的主机中添加服务端口
cat /etc/ansible/hosts
[web]
192.168.86.13 ansible_ssh_user=root ansible_ssh_pass=duan@1994 web_port=80

ansible web -a "echo {{web_port}}"
2>组变量
给web组添加nginx_version
cat /etc/ansible/hosts

[web]
192.168.86.13 ansible_ssh_user=root ansible_ssh_pass=duan@1994 web_port=80
[web:vars]
nginx_version=1.9.1

ansible web -a "echo {{nginx_version}}"
mkdir /etc/ansible/group_vars

cat << EOF >>  /etc/ansible/group_vars/db.yml
mysql_version: 5.7
db_region: beijing
EOF

ansible db -a "echo {{mysql_version}}"
ansible db -a "echo {{db_region}}"
三、ad-hoc命令常用模块
shell模块
ansible web -m shell -a "echo test >>/tmp/a.txt"

--list-hosts查看组中主机
ansible web --list-hosts

-vvv查看执行详细信息
ansible web -m shell -vvv -a "cat /tmp/a.txt"

-f指定并发数量
ansible all -m shell  -a "hostname" -f 2

-i指定inventory
ansible all -m shell  -a "hostname" -i /etc/ansible/hosts

-u指定用户 -k指定密码
ansible all -m shell  -a "hostname" -utest -ktest123

使用拥有sudo权限普通用户进行提权,-k输入普通密码,--become进行提权,--become-user root,提权到root用户,--ask-become-pass,输入普通用户sudo密码
ansible web -m shell -a "ls /root/" -u test -k --become --become-user root --ask-become-pass

copy模块【本地拷贝到远端】
ansible web -m copy -a "src=/root/a.sh dest=/tmp/"
fetch模块【拷贝远端到本地】
ansible web -m fetch -a "src=/tmp/a.sh dest=/opt/"

file模块:在目标主机创建一个文件夹,删除一个文件夹,创建一个空文件、删除一个空文件
ansible web -m file -a "dest=/tmp/createfilename mode=600 state=directory"
ansible web -m file -a "dest=/tmp/createfilename mode=600 state=absent"
ansible web -m file -a "dest=/tmp/createfilename mode=600 state=touch"
ansible web -m file -a "dest=/tmp/createfilename mode=600 state=absent"

yum模块:在目标主机安装软件,卸载软件、安装指定版本软件、安装最新版本软件
ansible web -m yum -a "name=git state=present"
ansible web -m yum -a "name=git  state=absent"
ansible web -m yum -a "name=git-1.8.3.1  state=present"
ansible web -m yum -a "name=git state=latest"

yum模块:管理目标主机服务状态,启动、停止、重启、开机启动
ansible web -m yum -a "name=httpd state=present"
ansible web -m service -a "name=httpd state=started"
ansible web -m service -a "name=httpd state=stopped"
ansible web -m service -a "name=httpd state=restarted"
ansible web -m service -a "name=httpd enabled=true"

user模块:创建用户、删除用户、创建nologin用户
ansible web -m user -a "name=foo password=foopassword"
ansible web -m user -a "name=foo state=absent"
ansible web -m user -a "name=foo password=foopassword shell=/sbin/nologin"

git模块:clone代码,前提是被管理主机内有git命令,并且目标目录需要是空目录
ansible web -m git -a "repo=https://github.com/easzlab/kubeasz.git dest=/mnt/"

setup模块获取主机信息、过滤主机信息、列出和内存相关的所有值。常用模块信息

ansible web -m setup 
ansible web -m setup -a "filter=ansible_nodename"
ansible web -m setup -a "filter=ansible_*_mb"

ansible_nodename 主机名
ansible_os_family   系统名称
ansible_pkg_mgr   包管理工具
ansible_processor
ansible_processor_cores  cpu核数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值