ansible笔记1&2:安装和基本使用

1. 安装部署

为了便于测试机上的项目管理,一般使用pyvenv进行项目部署,不影响系统全局,且每个项目都是一个独立的文件夹,目录清晰

关于更多的部署方式,ansible installation guide 说的很清楚,自行查阅

1.1 环境配置

OS:CentOS Linux release 7.6.1810 (Core)

Kernel:Linux VM_10_40_centos 3.10.0-1062.9.1.el7.x86_64 #1 SMP Fri Dec 6 15:49:49 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

具体:腾讯云 CVM,官方镜像

yum install -y python3 python3-devel
mkdir /app/ansible
pyvenv /app/ansible/py36

1.2 Ansible部署

ansible 安装

source /app/ansible/py36/bin/activate
pip install ansible
cd /app/ansible
# 新建几个工作目录
mkdir ansible-etc ansible-playbook ansible-plugins ansible-roles tmp

ansible.cfg 配置:到 ansible github 上找到配置文件样本 这里

wget https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg -O /app/ansible/ansible-etc/ansible.cfg
# 添加配置文件的路径到环境变量
vi /app/ansible/py36/bin/activate
# 添加以下两行
ANSIBLE_CONFIG="/app/ansible/ansible-etc/ansible.cfg"
export ANSIBLE_CONFIG
# 重置环境
source /app/ansible/py36/bin/activate

修改 ansible.cfg,因为我们的路径全变了,所以修改为:

[defaults]
inventory       = /app/ansible/ansible-etc/hosts
library         = /app/ansible/ansible-plugins/modules:/usr/share/ansible/plugins/modules
module_utils    = /app/ansible/ansible-plugins/module_utils:/usr/share/ansible/plugins/module_utils
remote_tmp      = /app/ansible/tmp
local_tmp       = /app/ansible/tmp

inventory 创建

vi /app/ansible/ansible-etc/hosts
# 添加测试用例,10.3.10.40为本地lan地址
[test_server]
10.3.10.40 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=GIMDvZcgqhaYhWk9

测试一下,我们的cfg和Inventory都正常工作了

(py36) [root@VM_10_40_centos tmp]# ansible test_server -m ping
10.3.10.40 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

2. 基本使用

ansible 提供了大量的 Modules 给你直接使用,可以管理

  1. a10/bigip 这一类硬件负载均衡
  2. aci 这一类交换机
  3. aws/azure/vmware 这一类公有云/虚拟化
  4. docker 容器
  5. win/os 操作系统
  6. ...

2.1 Inventory

inventory 是 ansible 用来定义资源的账本,可以有多种定义方式,后面再深入

我们先定义一组server,组名为 test_01,名下有一台主机 10.0.0.40,详细内容有ssh相关信息

[test_01]
10.3.10.40 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=woshimima

2.2 CLI

通过 ansible 命令行工具,可以直接对主机组,或者主机IP进行操作和管理

(py36) [root@VM_10_40_centos tmp]# ansible test_01 -m command -a 'echo "Hello World"'
10.3.10.40 | CHANGED | rc=0 >>
Hello World

ansible命令行还可以支持更多选项,例如ssh选项,是否使用密钥,是否切换账号,账号密码,超时设置,线程数量。敲下 ansible --help查看

2.3 Playbook

playbook 是定义受控节点状态的配置文件,是文件即配置的思想,可以看到后期docker,k8s以及其它很多工具全部都是配置文件定义运行状态的路子

这是最佳的数据中心管理模式,但是很多传统企业没有这种意识,还是会依赖ITSM去进行事务管理,只会在操作层面进行自动化

运行playbook也叫”跑剧本”,做好角色,非常形象了

创建一个playbook,hello-world.yml,进行基本的OS管理

- hosts: test_01
  tasks:
  - name: Install Package BY Yum
    yum:
      name: "{{ packages }}"
    vars:
      packages:
      - python3
      - python3-devel
      - nginx
  - name: Remove Package BY Yum
    yum:
      name: nginx
      state: absent
  - name: Disable firewalld BY Systemctl
    systemd:
      name: firewalld
      state: stopped
      enabled: no
  - name: Add os user 'zhangwei'
    user:
      name: zhangwei
      password: default_password
      update_password: on_create
      shell: /bin/bash
      groups: root
      append: True

执行playbook,完成受控主机的配置

(py36) [root@VM_10_40_centos ansible]# ansible-playbook ansible-playbook/hello-world.yml 

PLAY [test_01] **************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************
ok: [10.3.10.40]

TASK [Install Package BY Yum] ***********************************************************************************
changed: [10.3.10.40]

TASK [Remove Package BY Yum] ************************************************************************************
changed: [10.3.10.40]

TASK [Disable firewalld BY Systemctl] ***************************************************************************
ok: [10.3.10.40]

TASK [Add os user 'zhangwei'] ***********************************************************************************
[WARNING]: The input password appears not to have been hashed. The 'password' argument must be encrypted for
this module to work properly.
changed: [10.3.10.40]

PLAY RECAP ******************************************************************************************************
10.3.10.40                 : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

ok:即为4个自定义task,和1个初始task(facts更新)的运行状态,全部正常。

changed:即3个task的操作触发了变更,包括了

  1. yum install nginx,安装上了,原来没有
  2. yum remove nginx,卸载掉了,因为刚刚安装了
  3. useradd zhangwei,创建了,原来没有

也有1个task是没有任何变化的,即 firewalld 的定义,因为原来就stop&disable了

posted @ 2020-03-18 15:20  zw1027  阅读(205)  评论(1编辑  收藏  举报