ansible

install

$ sudo yum install epel-release
$ sudo yum install ansible


rpm -ql ansible
/etc/ansible
/etc/ansible/ansible.cfg
/etc/ansible/hosts
/etc/ansible/roles
/usr/bin/ansible
/usr/bin/ansible-2
/usr/bin/ansible-2.7
/usr/bin/ansible-config
/usr/bin/ansible-connection
/usr/bin/ansible-console
/usr/bin/ansible-console-2
/usr/bin/ansible-console-2.7
/usr/bin/ansible-doc
/usr/bin/ansible-doc-2
/usr/bin/ansible-doc-2.7
/usr/bin/ansible-galaxy
/usr/bin/ansible-galaxy-2
/usr/bin/ansible-galaxy-2.7
/usr/bin/ansible-inventory
/usr/bin/ansible-playbook
/usr/bin/ansible-playbook-2
/usr/bin/ansible-playbook-2.7
/usr/bin/ansible-pull
/usr/bin/ansible-pull-2
/usr/bin/ansible-pull-2.7
/usr/bin/ansible-vault
/usr/bin/ansible-vault-2
/usr/bin/ansible-vault-2.7




验证安装:
ansible all -m ping 

第一次host验证,如果机器多会麻烦,可以打开配置,不需要验证:
# uncomment this to disable SSH key host checking
host_key_checking = False  



copy 模块,复制文件
ansible web -m copy -a 'src=~/hello dest=/data/hello'
复制目录:

拷贝目录时 src=/etc/ansible dest=/tmp :

    ① 如果远程客户端 /tmp/ansible 目录不存在,则会直接把源目录拷贝成这个目录名
    ② 如果远程客户端 /tmp/ansible 目录存在,则会把源目录拷贝到该目录下,最终目录是 /tmp/ansible/ansible



带权限copy

ansible nsqd -m copy -a "src=/opt/nsq/nsqlookupd dest=/opt/nsq/ mode=0755"  注意这里前边的补零 用0755



shell模块执行命令
ansible web -m shell -a 'cat /etc/passwd |grep "keer"'

yum 模块:
ansible all -m yum -a 'name=nginx state=latest' 安装最新nginx



ansible执行sed:
ansible samba-client -m shell -a "sed -i '\$d' /etc/fstab" (注意这里的$要转义)


如果不使用公钥免密的方式,可使用-k参数,要求输入密码:(同时配置文件要打开host_key_checking 参数,不验证主机签名)
ansible etcd -m shell -a "systemctl status etcd" -k


ansible中使用sudo执行,注意新版本ansible 已经弃用--sudo参数,需要使用--become系列参数:
先在配置文件/etc/ansible/ansible.cfg中开启,sudo 将#sudo_user      = zhangsan 注释去掉,并确保zhangsan是具有目标机器的sudo权限的,然后执行
 ansible post -m copy -a "src='/home/zhangsan/cloudscan/systemd_service_file/cspost.service' dest='/usr/lib/systemd/system/'" -k  --become  --become-method=sudo  --become-user=root




ansible sudo copy 没有改变文件的属主:
原来是app用户app组,用sudo copy后还是app:app

ansible front -m copy -a "src='/home/hjx/cloudscan/app_bin/v0.0.1/csfront' dest='/opt/cloudscan/' mode=0755" -k --become --become-method=sudo --become-user=root
10.6 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "33a0d46c42eefa9deb",
"dest": "/opt/cloudscan/csfront",
"gid": 1008,
"group": "app",
"md5sum": "5ac941ec3ff27fb3eedc",
"mode": "0755",
"owner": "app",
"size": 3612,
"src": "/home/hxj/.ansible/tmp/ansible-tmp-1643177945.72-28485-120808158151379/source",
"state": "file",
"uid": 10000
}
10.5 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "33a0dc93bce746c42eefa9deb",
"dest": "/opt/cloudscan/csfront",
"gid": 1007,
"group": "app",
"md5sum": "5ac4222d941ec3ff27fb3eedc",
"mode": "0755",
"owner": "app",
"size": 3619,
"src": "/home/hjx/.ansible/tmp/ansible-tmp-1643177945.72-28484-23458988881562/source",
"state": "file",
"uid": 10000

script模块,直接执行中控机脚本,不用显示复制到被管机

ansible windows 使用:

shell

ansible windows -m win_shell -a 'd:\v0.0.0\supervisord.exe ctl status '

copy

ansible all -m win_copy -a 'src=/home/bin/v1.0.9/csscand.exe dest=D:\\v0.0.0\\app\\csscand.exe' (注意windows路径里的转义)

 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

ansible访问Windows:

官方文档:https://docs.ansible.com/ansible/latest/user_guide/windows_setup.html

这篇文章不错:

https://www.cnblogs.com/kingleft/p/6391652.html

总的来说,就是linux控制机,装pywinrm python模块,然后配置/etc/ansible/hosts

windows上做这些:(而这些其实是Windows远程管理所必要的,所以同事调Jenkins才能复用)

  • 安装Framework 3.0+
  • 更改powershell策略为remotesigned
  • 升级PowerShell至3.0+
  • 设置Windows远端管理,英文全称WS-Management(WinRM)

详细操作,见下面原文,另外有个注意的地方

我们服务器是window 2008系统,powershell3.0这个场景下,有个bug,当然上面列的官方文档里也说了https://docs.ansible.com/ansible/latest/user_guide/windows_setup.html#winrm-memory-hotfix

相关bug讨论,来自google 讨论组:

https://groups.google.com/g/ansible-project/c/UVyj6hxgDAA

在powershell操作序列:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $url = "https://raw.githubusercontent.com/jborean93/ansible-windows/master/scripts/Install-WMF3Hotfix.ps1" $file = "$env:temp\Install-WMF3Hotfix.ps1" (New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file) powershell.exe -ExecutionPolicy ByPass -File $file -Verbose
一共5条命令,最后一步执行完会重启机器。

 这个修复bug命令操作完。ansible 访问windows通了:

ansible windows -m win_ping

 | SUCCESS => {
"changed": false,
"ping": "pong"
}

linux控制机上:

我的系统不带pip而且是python2.7,所以需要安装pip

1021 curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py
1022 python get-pip.py
1024 pip --version
1026 pip install pywinrm
1028 vi /etc/ansible/hosts

/etc/ansible/hosts配置:这个配置很重要,所有连接信息都在这里

[windows]
192.168 ansible_ssh_user="Administrator" ansible_ssh_pass="" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore


1037 

ansible windows -m win_ping
192.168 | SUCCESS => {
"changed": false,
"ping": "pong"
}

原文:

一、前言

近期打算搞搞自动部署,因为是windows服务器,一些工具和系统支持都不是太好。最后发现ansible比较火,最重要的是他支持windows。本文主要就ansible 在windows使用环境搭建过程分享。

二、Ansible简介
    ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:

(1)、连接插件connection plugins:负责和被监控端实现通信;

(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;

(3)、各种模块核心模块、command模块、自定义模块;

(4)、借助于插件完成记录日志邮件等功能;

(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。

三、Windows下Ansible工作模式

Ansible 从1.7+版本开始支持Windows,但前提是管理机必须为Linux系统,远程主机的通信方式也由SSH变更为PowerShell,同时管理机必须预安装Python的Winrm模块,方可和远程Windows主机正常通信,但PowerShell需3.0+版本且Management Framework 3.0+版本,实测Windows 7 SP1和Windows Server 2008 R2及以上版本系统经简单配置可正常与Ansible通信。简单总结如下:

(1)    管理机必须为Linux系统且需预安装Python Winrm模块

(2)    底层通信基于PowerShell,版本为3.0+,Management Framework版本为3.0+

(3)    远程主机开启Winrm服务

四、Ansible管理机部署安装

(1). 对管理主机的要求

    目前,只要机器上安装了 Python 2.6 或 Python 2.7 (windows系统不可以做控制主机),都可以运行Ansible.

主机的系统可以是 Red Hat, Debian, CentOS, OS X, BSD的各种版本,等等.

       (2) 从源码运行

    从项目的checkout中可以很容易运行Ansible,Ansible的运行不要求root权限,也不依赖于其他软件,不要求运行后台进程,也不需要设置数据库.因此我们社区的许多用户一直使用Ansible的开发版本,这样可以利用最新的功能特性,也方便对项目做贡献.因为不需要安装任何东西,跟进Ansible的开发版相对于其他开源项目要容易很多.

从源码安装的步骤 

$ git clone git://github.com/ansible/ansible.git --recursive
$ cd ./ansible

使用 Bash:

$ source ./hacking/env-setup

如果没有安装pip, 请先安装对应于你的Python版本的pip:

$ sudo easy_install pip

以下的Python模块也需要安装:

$ sudo pip install paramiko PyYAML Jinja2 httplib2 six

一旦运行env-setup脚本,就意味着Ansible从源码中运行起来了.默认的inventory文件是 /etc/ansible/hosts。

配置hosts文件:

$  vim /etc/ansible/hosts
[windows]
192.168.1.105 ansible_ssh_user="Administrator" ansible_ssh_pass="123456" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore
192.168.1.105是windows服务器的IP。

至此,服务端配置完毕。

五、 Windows系统配置

和Linux发版版稍有区别,远程主机系统如为Windows需预先如下配置:

  • 安装Framework 3.0+
  • 更改powershell策略为remotesigned
  • 升级PowerShell至3.0+
  • 设置Windows远端管理,英文全称WS-Management(WinRM)
(1)安装Framework 3.0+

下载链接为:http://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_x86_x64.exe。 下载至本地后双击左键安装即可,期间可能会多次重启,电脑需正常连接Internet。

(2)更改powershell策略为remotesigned
    set-executionpolicy remotesigned
(3)升级PowerShell至3.0+

Window 7和Windows Server 2008 R2默认安装的有PowerShell,但版本号一般为2.0版本,所以我们需升级至3.0+,如下图中数字1部分表示PowerShell版本过低需3.0+版本,数字2部分表示当前PowerShell版本为2.0。

 下脚本保存至本地后,右键选择“使用PowerShell运行”,执行完毕重启系统后,在PowerShell执行Get-Host命令结果如下图所示PowerShell版本为3.0为正常。 
upgrade_to_ps3.ps1
 

 

(4)设置Windows远端管理(WS-Management,WinRM)

winrm service 默认都是未启用的状态,先查看状态;如无返回信息,则是没有启动;

winrm enumerate winrm/config/listener

针对winrm service 进行基础配置:

winrm quickconfig

查看winrm service listener:

winrm e winrm/config/listener

为winrm service 配置auth:

winrm set winrm/config/service/auth '@{Basic="true"}'

为winrm service 配置加密方式为允许非加密:

winrm set winrm/config/service '@{AllowUnencrypted="true"}'
好了,远程Windows主机配置到此结束,我们验证配置的是否有问题。

备注:

我们服务器是window 2008系统,powershell3.0这个场景下,有个bug,当然上面列的官方文档里也说了https://docs.ansible.com/ansible/latest/user_guide/windows_setup.html#winrm-memory-hotfix

在powershell操作序列:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $url = "https://raw.githubusercontent.com/jborean93/ansible-windows/master/scripts/Install-WMF3Hotfix.ps1" $file = "$env:temp\Install-WMF3Hotfix.ps1" (New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file) powershell.exe -ExecutionPolicy ByPass -File $file -Verbose
一共5条命令,最后一步执行完会重启机器。
六、Windows下可用模块测试
(1)win_ping —Windows系统下的ping模块,常用来测试主机是否存活
ansible windows -m win_ping
  (2) win_copy—拷贝文件到远程Windows主机
传输/etc/passwd文件至远程F:\file\目录下

执行命令:

ansible windows -m win_copy -a 'src=/etc/passwd dest=F:\file\passwd'

返回结果:

1
2
3
4
5
6
7
192.168.1.105 | success >> {
     "changed" true ,
     "checksum" "896d4c79f49b42ff24f93abc25c38bc1aa20afa0" ,
     "operation" "file_copy" ,
     "original_basename" "passwd" ,
     "size" : 2563
}

部分返回结果诠释:

  • “operation”: “file_copy”—执行的操作为 file_copy;
  • “original_basename”: “passwd”—文件名为 passwd;
  • “size”: 2563—文件大小为 2563 bytes。

Playbook写法如下:

1
2
3
4
5
6
---
- name: windows module example
   hosts: windows
   tasks:
      - name: Move  file  on remote Windows Server from one location to another
        win_file: src= /etc/passwd  dest=F:\ file \ passwd
(3)win_file —创建,删除文件或目录
删除F:\file\passwd

执行命令:

ansible windows -m win_file -a "path=F:\file\passwd state=absent"

返回结果:

1
2
3
192.168.1.105 | success >> {
     "changed" true
}
七、总结
    至此,环境搭建完成,可以在本地远程控制windows服务器,如果想要自动部署,还需要码powershell脚本来完成自动部署的相关功能。还未测试,待测试通过后再来分享。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值