首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
清单首页运维文章详情

ansible基础使用

本文将从无到有注明ansible的基础使用, 以三个例子进行教学

  1. 测试连通性
  2. 使用常规Linux下的命令
  3. 进行apt的更新

实测

准备hosts文件

首先更新ansible主目录下的hosts文件用作测试

由于在生产中, 出于安全性考虑, 不使用ssh互信进行ansible通信, 可以在配置文件中通过键值对的方式定义变量, 注明用户名与密码

代码语言:javascript
复制
[all:vars]
ansible_python_interpreter='/usr/bin/python3'
​
[test:vars]
ansible_ssh_user='username'
ansible_ssh_pass='password'
​
[test]
10.122.1.77
10.122.1.78
测试连通性

命令格式: ansible group_name/ip -m moudle_name [-a moudel_args]

代码语言:javascript
复制
[root@localhost ansible]# ansible test -m ping
10.122.1.77 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
10.122.1.78 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
​
​
使用常规命令

以确认时区为例子

代码语言:javascript
复制
[root@localhost ansible]# ansible test -m shell -a 'timedatectl| grep Time'
10.122.1.78 | CHANGED | rc=0 >>
                       Time zone: Etc/UTC (UTC, +0000)
10.122.1.77 | CHANGED | rc=0 >>
                       Time zone: Asia/Shanghai (CST, +0800)
进行apt更新

此步可以有两种方式进行完成, 以下将一一实现

1. 使用shell模块, 直接输入相关命令
代码语言:javascript
复制
[root@localhost ansible]# ansible test -m shell -a 'apt update'
10.122.1.78 | FAILED | rc=100 >>
Reading package lists...
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
​
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)
W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)non-zero return code
​
  1. 可以看到这里由于权限问题, 未能完成apt源的更新, 此时, 可以对ansible.cfg中的become_user进行修改, 这个配置的含义是将以哪个用户的身份进行命令执行
代码语言:javascript
复制
vim /etc/ansible/ansible.cfg
become=True
become_method=sudo
become_user=root
become_ask_pass=False
# 默认注释, 解除注释即可, 意义很明显, 就不赘述了
  1. 对应的, 受管主机也要设置sudo免密, 或直接以sudo用户登录
  2. 修改后配置文件后成功
代码语言:javascript
复制
[root@localhost ansible]# ansible test -m shell -a 'apt update'
10.122.1.77 | CHANGED | rc=0 >>
Hit:1 http://cn.archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 http://cn.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:3 http://cn.archive.ubuntu.com/ubuntu bionic-backports InRelease
Hit:4 http://cn.archive.ubuntu.com/ubuntu bionic-security InRelease
Reading package lists...
Building dependency tree...
Reading state information...
12 packages can be upgraded. Run 'apt list --upgradable' to see them.
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
2. 使用ansible内置的apt模块更新
  1. 创建playbook所用的yml文件
代码语言:javascript
复制
[root@localhost test]# vim apt_update.yml 
​
---
  
- hosts: test
  tasks:
    - name: update apt source
      apt:
        update_cache: yes
        cache_valid_time: 86400
  1. 执行playbook [root@localhost test]# ansible-playbook apt_update.yml ​ PLAY [test] ******************************************************************************************************************************************************************* ​ TASK [Gathering Facts] ******************************************************************************************************************************************************** ok: [10.122.1.78] ok: [10.122.1.77] ​ TASK [update apt source] ****************************************************************************************************************************************************** ok: [10.122.1.78] ok: [10.122.1.77] ​ PLAY RECAP ******************************************************************************************************************************************************************** 10.122.1.77 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 10.122.1.78 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 ​ ​
修改配置文件以使用多个inventory
代码语言:javascript
复制
inventory      = /home/test/hosts

此时/home/test/hosts下的任何inventory文件都可以被ansible识别并使用

下一篇
举报
领券