1.文件的第一行应该以“—”(三个连字符)开始,表明YAML文件的开始。
2.在同一行中,#之后的内容表示注释,类似于shell,python和ruby。
3.YAML中的列表元素以“-”开头并且跟着一个空格。后面为元素内容。
4.同一个列表之中的元素应该保持相同的缩进,否则会被当做错误处理。
5.play中hosts、variables、roles、tasks等对象的表示方法都是以键值中间以“:”分隔表示,并且“:”之后要加一个空格。
1、获取升级包
从XX网址中下载XXX安装包,并拷贝到对应的机器上并解压。
2、升级
进入安装包解压后所在目录,双击install.bat文件执行安装
3、启动服务
进入安装包解压后所在目录,双击start.bat文件启动服务。
1 ---
2 - name: "agent upgrade"
3 hosts: all
4 gather_facts: no
5 vars:
6 host_agent_url: "https://XXXXXXXX/host-agent.zip"
7 host_agent_destination: "C:\\host_agent_windows"
8 tasks:
9 - name: create destination directory
10 win_file:
11 path: "{{ host_agent_destination }}"
12 state: directory
13
14 - name: download host agent zip file
15 win_get_url:
16 url: "{{ host_agent_url }}"
17 dest: "{{ host_agent_destination }}\\host-agent.zip"
18 force: yes
19
20 - name: extract host agent zip file
21 win_unzip:
22 src: "{{ host_agent_destination }}\\host-agent.zip"
23 dest: "{{ host_agent_destination }}"
24 creates: "{{ host_agent_destination }}\\host-agent"
25
26 - name: install host agent
27 win_command: "{{ host_agent_destination }}\\host-agent\\install.bat"
28 args:
29 chdir: "{{ host_agent_destination }}\\host-agent"
30
31 - name: start host agent service
32 win_command: "{{ host_agent_destination }}\\host-agent\\start.bat"
33 args:
34 chdir: "{{ host_agent_destination }}\\host-agent"
具体含义如下:
url 参数指定了 host agent 压缩包的下载链接。dest 参数指定了下载的文件应该保存在哪个目录下,这里是 C:\host_agent_windows\host-agent.zip。force 参数指定了如果目标文件已经存在,是否要强制覆盖它。这里是 yes,表示无论文件是否存在都要覆盖。src 参数指定了要解压的文件路径,这里是 C:\host_agent_windows\host-agent.zip。dest 参数指定了解压后的文件应该保存在哪个目录下,这里是 C:\host_agent_windows。creates 参数指定了一个文件或目录的路径,如果这个文件或目录已经存在,那么这个任务就不会再次执行。这里是 C:\host_agent_windows\host-agent,表示如果这个目录已经存在,则不再执行解压任务。win_command 模块指定了要执行的命令,这里是 C:\host_agent_windows\host-agent\install.bat,表示要执行 host agent 的安装脚本。args 参数指定了要传递给命令的参数,这里是 chdir: "{{ host_agent_destination }}\host-agent",表示在执行命令之前要切换到 C:\host_agent_windows\host-agent 目录下。这个参数是必需的,因为 host agent 的安装脚本需要在这个目录下执行,否则会出现错误。注意:第 3 行应该是 hosts 可别写成 host。
我们对一台测试机器进行执行测试,首先我们创建 hosts 文件,其中输入信息根据实际场景来:
[win_base_image]
111.111.111.111
[win_base_image:vars]
ansible_user=Administrator
ansible_password=Administrator
ansible_connection=winrm
ansible_winrm_transport=basic
ansible_port=5555
然后我们通过ansible-playbook执行脚本:
ansible-playbook -i hosts playbooks/agent_upgrade_test.yml

可以发现执行成功。还有可以执行很多功能,等待我们后续探索。