- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为 list 主机列表编写剧本:-
如果主机可访问,写入“connection=1”并写入文件。如果主机不可访问,写入同一个文件“connection=0”
据我了解,Ansible 不会以可访问的方式存储无法访问的热点信息(当 ssh 失败时)。
你能帮我解决一下吗?我的剧本贴在下面。shell 任务根本不执行,因为主机不可访问
- hosts: '{{ host }}'
gather_facts: False
vars:
dest: /tmp/trace
tasks:
- copy:
content: ''
dest: "{{ dest }}"
run_once: yes
delegate_to: 127.0.0.1
- shell: ping {{ inventory_hostname }} -c 1
register: ping_status
ignore_errors: yes
- setup:
filter: ansible_*
- lineinfile:
dest: "{{ dest }}"
line: 'Host:{{ inventory_hostname }},OS:{{ ansible_distribution }},Kernel:{{ansible_kernel}},OSVersion:{{ansible_distribution_version}},FreeMemory:{{ansible_memfree_mb}},connection:{{ping_status.rc}}'
ignore_errors: true
delegate_to: 127.0.0.1
最佳答案
您的剧本存在一些问题。首先是您正在尝试同时执行 shell
和一个 setup
远程主机上的任务,如果该主机不可用,这当然不会工作。
运行ping
甚至没有意义远程主机上的任务:您想使用委托(delegate)在本地主机上运行该任务。我们可以做这样的事情来记录每个主机的可用性作为主机变量:
---
- hosts: all
gather_facts: false
tasks:
- delegate_to: localhost
command: ping -c1 "{{ hostvars[inventory_hostname].ansible_host|default(inventory_hostname) }}"
register: ping
ignore_errors: true
- set_fact:
available: "{{ ping.rc == 0 }}"
您正在尝试运行 setup
针对远程主机的模块,但这只有在远程主机可用时才有意义,因此我们需要以 ping
的结果为条件任务:
- setup:
filter: "ansible_*"
when: ping.rc == 0
有了它,我们可以生成一个文件,其中包含有关每个主机可用性的信息。我正在使用 lineinfile
在这里因为那是你在你的例子中使用的,但如果我自己写这个我可能会使用 template
任务:
- hosts: localhost
gather_facts: false
tasks:
- lineinfile:
dest: ./available.txt
line: "Host: {{ item }}, connection={{ hostvars[item].available }}"
regexp: "Host: {{ item }}"
create: true
loop: "{{ groups.all }}"
当然,在您的示例中,您试图包含有关主机的各种其他事实:
line: 'Host:{{ inventory_hostname }},OS:{{ ansible_distribution }},Kernel:{{ansible_kernel}},OSVersion:{{ansible_distribution_version}},FreeMemory:{{ansible_memfree_mb}},connection:{{ping_status.rc}}'
如果目标主机不可用,这些事实将不可用,因此您需要使用 {% if <condition> %}...{% endif %}
使所有这些成为条件。构造:
line: "Host:{{ item }},connection:{{ hostvars[item].available }}{% if hostvars[item].available %},OS:{{ hostvars[item].ansible_distribution }},Kernel:{{ hostvars[item].ansible_kernel }},OSVersion:{{ hostvars[item].ansible_distribution_version }},FreeMemory:{{ hostvars[item].ansible_memfree_mb }}{% endif %}"
这使得最终的剧本看起来像这样:
---
- hosts: all
gather_facts: false
tasks:
- delegate_to: localhost
command: ping -c1 "{{ hostvars[inventory_hostname].ansible_host|default(inventory_hostname) }}"
register: ping
ignore_errors: true
- set_fact:
available: "{{ ping.rc == 0 }}"
- setup:
when: ping.rc == 0
- hosts: localhost
gather_facts: false
tasks:
- lineinfile:
dest: ./available.txt
line: "Host:{{ item }},connection:{{ hostvars[item].available }}{% if hostvars[item].available %},OS:{{ hostvars[item].ansible_distribution }},Kernel:{{ hostvars[item].ansible_kernel }},OSVersion:{{ hostvars[item].ansible_distribution_version }},FreeMemory:{{ hostvars[item].ansible_memfree_mb }}{% endif %}"
regexp: "Host: {{ item }}"
create: true
loop: "{{ groups.all }}"
关于Ansible 剧本 : How to write unreachable inventory host names to a file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55935582/
我刚刚开始学习 Ansible 并且有一个关于什么定义“游戏”的问题。如果我在看一座寺庙,我的理解是戏剧通常从以下开始: - name: xxx hosts: yyy 我读过 姓名 : 关键字
我正在尝试从 Ansible 2.3.1.0 运行剧本。我的远程计算机是 RHEL7,Python 安装在/usr/bin/python 中。 我的剧本如下所示: --- - hosts: my-te
我正在使用主机文件在主机列表上运行一个 ansible 剧本: [consul] ${HOST1} ansible_ssh_host=${HOST1} ansible_ssh_user=devops
我以前没有使用过 Ansible,有谁知道如何编写一个简单的 playbook 来卸载 nano 并在 Linux 服务器上安装 vim?我想在完成上述操作后,您需要包含一个选项来配置您想要的文本编辑
我有一个包含多个主机的系统以确保可用性。我只需要在一台主机上运行我的 Ansible 剧本。但是,其中一些主机可能无法访问。假设我有 host1、host2、host3 ... 如果无法访问 host
我不断收到用户错误:(play)在剧本中。 Ansible 似乎在提示结肠,但我尝试的任何方法都不起作用。 - hosts: all become: root tasks: -name:
可能之前已经有人问过这个问题,但目前还找不到任何有用的东西。 我有这个任务: - name: Create folder if not exists win_file: path:
我想知道是否有人找到了一种解决方案,可以在库存组未定义或为空时避免显示任何警告。 我只想让剧本的一部分运行如果组存在并且不为空,如果不存在,跳过而不警告。 请阅读https://github.com/
我想知道是否有人找到了一种解决方案,可以在库存组未定义或为空时避免显示任何警告。 我只想让剧本的一部分运行如果组存在并且不为空,如果不存在,跳过而不警告。 请阅读https://github.com/
我有一个 python 应用程序调用下面的代码,计划通过 Ansible API 以编程方式运行 Ansible 剧本,而不是使用子进程之类的东西。 下面的代码运行但实际上似乎没有执行任何内容。抓取结
目前我的剧本结构是这样的: ~/test_ansible_roles ❯❯❯ tree . . ├── checkout_sources │ └── tasks │ └── main.
我有一个使用 Vagrant 创建并使用 Ansible 配置的 EC2 实例。 我有这个使用 apt 安装 2 个软件包的任务. --- - name: Install GIT & TIG ac
我对 SSH 服务器和 Ansible 很陌生,所以这可能是一个愚蠢的问题。 尝试运行 Ansible 剧本,同时使用下面的 bash 命令使用私钥访问服务器。 ansible-playbook db
我有一堆需要经常修补的服务器。我计划使用 Ansible 来协调修补过程。这里的要点是它必须是“全有或全无”修补。要么所有服务器都打补丁,要么没有。 我正在为我的剧本考虑的任务是这样的: 1 - 转到
当我表演时 ansible -i /home/linux/ansible/my-prereqs/hosts -m ping 'test' 172.16.57.101 | SUCCESS => {
我有以下剧本: - name: packagexx version check shell: apk version packagexx | awk 'NR>1{$1=$1;print}'
我想使用公共(public) aws key 对 .pem 文件来运行 ansible 剧本。我想在不更改我的 ~/.ssh/id_rsa.pub 的情况下执行此操作我无法从当前的 ~/.ssh/id
我正在尝试运行一个相对简单的 Ansible 剧本来通过 Vagrant 配置 Ubuntu 虚拟机。大致分为 3 个步骤: 更新并安装必要的软件包 将 rbenv 安装到指定用户的家中 通过rben
我是 ansible(和 docker)的新手。我想在任何暂存/生产服务器上使用我的 ansible playbook 之前对其进行测试。 由于我无法访问空的远程服务器,我认为最简单的测试方法是使用
通过 fork 变量,我可以告诉 ansible 应该并行执行多少台主机。当之前的所有主机完成时,将执行下一个主机。如果主机上的执行时间不同,我必须等待最后一台主机完成。 是否可以告诉 ansible
我是一名优秀的程序员,十分优秀!