小编Zei*_*tor的帖子

如何获取小写的 ansible 事实

我正在收集可靠的事实。序列号有大写和小写两种形式。见下文。

5A2c32a-f1G85g-2035-0483-1fe9c129216d 
Run Code Online (Sandbox Code Playgroud)

我需要将该事实与另一个完全小写的序列号进行比较。请参见下文。

5a2c32a-f1g85g-2035-0483-1fe9c129216d 
Run Code Online (Sandbox Code Playgroud)

如果我使用 jinja2 过滤器转换为小写。然后我无法操作 json 输出。例如。我试过这个

- set_fact:
     facts: "{{ facts | lower | to_json }}"

- debug:
    var: facts.instance
Run Code Online (Sandbox Code Playgroud)

它抛出错误

TASK [play : set_fact] ******************************************************************************
ok: [localhost]

TASK [play : debug] ******************************************************************************
ok: [localhost] => {
    "facts.instance": "VARIABLE IS NOT DEFINED!"
}
Run Code Online (Sandbox Code Playgroud)

但如果我facts只调试。它给了我输出。下面的作品,

- set_fact:
     facts: "{{ facts | lower | to_json }}"

- debug:
    var: facts
Run Code Online (Sandbox Code Playgroud)

但我需要获得更多的价值facts.instance.disk

python jquery jinja2 ansible ansible-facts

6
推荐指数
1
解决办法
1万
查看次数

错误:无法满足的约束:curl(丢失):在构建 jmeter dockerfile 时

我无法构建 jmeter docker 文件,出现以下错误。

    WARNING: Ignoring http://dl-cdn.alpinelinux.org/alpine/v3.9/main/x86_64/APKINDEX.tar.gz: temporary error (try again later)
    WARNING: Ignoring http://dl-cdn.alpinelinux.org/alpine/v3.9/community/x86_64/APKINDEX.tar.gz: temporary error (try again later)
    ERROR: unsatisfiable constraints:
      curl (missing):
        required by: world[curl]
      fontconfig (missing):
        required by: world[fontconfig]
      net-tools (missing):
        required by: world[net-tools]
      shadow (missing):
        required by: world[shadow]
      su-exec (missing):
        required by: world[su-exec]
      tcpdump (missing):
        required by: world[tcpdump]
      ttf-dejavu (missing):
        required by: world[ttf-dejavu]
    The command '/bin/sh -c chmod +x /usr/local/bin/entrypoint.sh  && apk add --no-cache     curl     fontconfig     net-tools     shadow     su-exec     tcpdump      ttf-dejavu  && cd /tmp/  && …
Run Code Online (Sandbox Code Playgroud)

jmeter dockerfile

6
推荐指数
1
解决办法
5723
查看次数

如何将十进制字符串转换为十六进制字符串?

我有一个剧本,用于查询服务器的 SystemID,可以使用供应商提供的将 id 映射到模型的表将其转换为型号。服务器返回十进制值,但表使用等效的十六进制值。

我想要做的是将十进制字符串转换为可以与供应商提供的表中的条目匹配的十六进制字符串。

例子:

Server returns: SystemID = 1792
Matching entry in vendor table: 0x0700
Run Code Online (Sandbox Code Playgroud)

我在 Ansible 文档和 Web 中搜索了本机 Ansible 命令或 jinja2 表达式来进行转换。

我只发现int(value, base=x)jinja2 函数的作用与我想要做的相反。

本机 pythonhex()命令可以做到这一点。但如果可能的话我想避免这种情况。

以下是解析服务器标准输出以获取 systemid 值的 playbook 任务:

Server returns: SystemID = 1792
Matching entry in vendor table: 0x0700
Run Code Online (Sandbox Code Playgroud)

环境:Ansible 2.9.7 Python 3.8.0 macOS 10.15.4

jinja2 ansible

6
推荐指数
1
解决办法
6277
查看次数

无法导入所需的Python库(botocore或boto3)

我正在本地主机上运行一个 playbook,并且我已经安装了 ansible 和所需的包(如 boto3)。该剧本在远程主机上执行任务时工作正常,但在本地运行时输出以下错误。

命令:

ansible-playbook app.yaml
Run Code Online (Sandbox Code Playgroud)

错误:

fatal: [localhost]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (botocore or boto3) on DESKTOP-9NTDHK1's Python /usr/bin/python3. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"}
Run Code Online (Sandbox Code Playgroud)

安斯波版本:

ansible 2.10.3
  config file = /home/user/ansible/ansible.cfg
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location …
Run Code Online (Sandbox Code Playgroud)

ansible boto3

6
推荐指数
1
解决办法
2万
查看次数

在 Ansible 中使用 if 条件将 True 或 False 分配给 Gather_facts 不起作用

---
- hosts: "{{ run_on_node|default('mysql_cluster_sql[0]')}}"
  connection: "{% if migrated is defined and migrated == 'yes' %}local{% else %}ssh{% endif %}" # This works as we are assigning non boolean value
  gather_facts: "{% if migrated is defined and migrated == 'yes' %}false{% else %}true{% endif %}" #This doesnt work well
  tasks:
    - debug: var=ansible_all_ipv4_addresses
    - debug: var=ansible_default_ipv4.address
Run Code Online (Sandbox Code Playgroud)

库存文件:

[mysql_cluster_sql]
10.200.1.191 migrated=yes
Run Code Online (Sandbox Code Playgroud)

该变量根据条件具有 true 和 false 值,但即使 Gather_facts 为 false,它也会收集事实。

ansible devops ansible-facts

6
推荐指数
1
解决办法
8219
查看次数

仅打印“docker-container ls -la”输出中的“Names”列

发出docker container ls -la命令时,输出如下所示:

CONTAINER ID   IMAGE     COMMAND               CREATED          STATUS          PORTS     NAMES
a67f0c2b1769   busybox   "tail -f /dev/null"   26 seconds ago   Up 25 seconds             recursing_liskov
Run Code Online (Sandbox Code Playgroud)

我只想获取列中存在的容器名称Names

我的想法是使用 bash 反转命令输出中的列,以便将该Names列置于第一个位置并剪切容器的名称。

这是我尝试过的:

sudo docker container ls -la | rev |tail -n +2 | tr -s ' ' |  cut -d ' ' -f 1
Run Code Online (Sandbox Code Playgroud)

然而,这并没有给出预期的结果,并且我收到以下错误:

rev: stdin: Invalid or incomplete multibyte or wide character
Run Code Online (Sandbox Code Playgroud)

我被困住了,因为我不知道如何处理这个错误。可以修复以获得我期望的结果吗?或者还有其他方法可以获得我的结果吗?

linux bash docker

6
推荐指数
1
解决办法
1802
查看次数

在 Ansible 剧本中:在“when”子句中使用可能未定义的布尔值

Ansible 任务可以有when这样的子句:

- name: Conditional output
  debug:
    msg: This is a conditional output
  when: some_var
Run Code Online (Sandbox Code Playgroud)

为了防止some_var未定义,可以使用

- name: Conditional output
  debug:
    msg: This is a conditional output
  when: some_var is defined and some_var
Run Code Online (Sandbox Code Playgroud)

似乎还有这样的变体:

- name: Conditional output
  debug:
    msg: This is a conditional output
  when: some_var | d() | bool
Run Code Online (Sandbox Code Playgroud)

这些变体相同吗?它们的优点/缺点是什么?

ansible

6
推荐指数
1
解决办法
545
查看次数

列出容器时如何在命令列中看到完整的命令字符串?

它是这个问题的补充,典型的解决方案是使用sudo docker container ls显示的命令

\n
CONTAINER ID        IMAGE                 COMMAND                  CREATED             ...\n4114e655e2b1        xx/yy                 "/bin/sh -c \'exec po\xe2\x80\xa6"   3 days ago          ... \n
Run Code Online (Sandbox Code Playgroud)\n

这里的问题是如何查看完整的 COMMAND 列内容?

\n

PS:没有人使用该--format选项,如何以最简单的即插即用方式使用它?

\n

docker

5
推荐指数
1
解决办法
2513
查看次数

从“--limit”选项之外的主机获取数据

我有以下剧本 ( playbook.yaml)

- hosts: myfirsthost[0]
  tasks:
    - name: Get a token
      slurp:
        src: /var/mytoken
      register: tokenFile

- hosts: myotherhosts
  vars:
    fileToken: "{{ hostvars[groups['myfirsthost'][0]]['tokenFile']['content'] | b64decode | replace('\n', '') }}"
  tasks:
    - debug:
    msg: The token {{fileToken}}
Run Code Online (Sandbox Code Playgroud)

当我为所有主机运行它时,它工作正常。

但是当我针对组中包含的单个主机myotherhosts(不在 group 中myfirsthosts)运行它时

ansible playbook.yaml --limit thesinglehost 
Run Code Online (Sandbox Code Playgroud)

它不执行第一个任务,然后变量无法初始化,这是预期的。

您是否知道我如何强制所有主机执行“获取令牌”任务,即使它们不在myfirsthost

ansible

5
推荐指数
1
解决办法
1515
查看次数

如何迭代枚举标志别名?

问题

如何迭代枚举标志别名?

from enum import Flag

class Color(Flag):
    RED = 1
    BLUE = 2
    GREEN = 4
    WHITE = RED | BLUE | GREEN # <- How do I retrieve this alias?

list(Color) 
# [<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 4>]
# How do I include Color.White and other aliases in this list?
Run Code Online (Sandbox Code Playgroud)

根据文档,__iter__仅返回非别名成员。

__iter__(self)
返回所有包含的非别名成员
版本 3.11 中的更改:迭代期间不再返回别名。

python python-3.11

5
推荐指数
1
解决办法
129
查看次数