1. 胖哥叨逼叨首页
  2. OpenSource
  3. Ansible

Ansible由浅入深 – hello ansible

今天开始学习Ansible,Ansible由浅入深系列也是一个学习笔记,我坚持能更新到“深”而不是停留在“浅”就结束。争取不断更。哈哈

环境介绍

服务器名称服务器IP用途
ansible-control192.168.31.97ansible控制端
ansible-node1192.168.31.128ansible受控节点1
ansible-node2192.168.31.81ansible受控节点2
ansible环境介绍

编辑控制端host文件

# 将三台机器写到host记录中
vim /etc/hosts

192.168.31.97   ansible-control
192.168.31.128  ansible-node1
192.168.31.81   ansible-node2

vscode准备

ansible相关内容的编写后面都会使用vscode来编写,是一个非常方便的工具。在vscode中安装两个插件

  • sftp
  • ansible
Ansible由浅入深 - hello ansible
SFTP
Ansible由浅入深 - hello ansible
ansible

vscode配置SFTP插件

当vscode安装完成SFTP插件后,需要通过Ctrl+shift+p进行配置,选择“SFTP:Config”编辑sftp.json

{
    "name": "ansible-control",
    "host": "192.168.31.97",
    "protocol": "sftp",
    "port": 22,
    "username": "root",
    "remotePath": "/root/ansible-code",
    "uploadOnSave": true
}

Ansible安装

yum install ansible -y
yum install epel-release
yum repolist

安装完成后,我们查看一下ansible的版本

ansible --version
ansible 2.9.10
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Apr  2 2020, 13:16:51) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]

Hello Ansible

通过vscdoe创建一个inventory.ini文件里面输入简单的连接信息,我们尝试体验一下ansible ping

ansible-node1 ansible_connection=ssh ansible_user=root ansible_ssh_pass=ansible_pwd
ansible-node2 ansible_connection=ssh ansible_user=root ansible_ssh_pass=ansible_pwd

通过ansible control执行如下命令查看ansible ping结果

ansible all -m ping -i inventory.ini

执行完成后,我们会查看到如下内容,完成了我们本次Hello Ansible

ansible-node2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
ansible-node1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

此文章为原创文章,作者:胖哥叨逼叨,如若转载,请与我联系并注明出处:https://www.pangshare.com/2371.htm

发表评论

电子邮件地址不会被公开。 必填项已用*标注