AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题

问题[ansible](server)

Martin Hope
Suffren
Asked: 2025-04-09 17:49:16 +0800 CST

Ansible vmware.vmware.vms 动态库存插件

  • 5

我想使用 vmware.vmware.vms ansible 插件创建动态清单。我想自定义清单以仅选择 Linux 主机。

我不明白该插件是如何工作的,并且 ansible 文档中的示例和解释很差(https://docs.ansible.com/ansible/latest/collections/vmware/vmware/vms_inventory.html#ansible-collections-vmware-vmware-vms-inventory)

使用下面的代码,插件返回了3个组(linux、PoweredOn、PoweredOff)。在创建的“linux”组下,所有来自vmware的主机(包括Windows主机)都会返回。即使我告诉插件使用“guest.guestFamily”属性进行分组,插件仍然会返回一长串包含不需要的值的输出。

plugin: vmware.vmware.vms
hostname: "REDACTED"
username: "REDACTED"
password: "REDACTED"
validate_certs: false
properties: ["guest"]
groups:
  linux: guest.guestFamily

我的目标是:

  • 如何使用插件来过滤特定主机并将其添加到组中
  • 如何避免输出不需要的组(例如 PoweredOn 和 PoweredOff 默认组)
  • 如何将插件的返回结果输出为可用的 .yaml/.ini 格式或文件(可与剧本一起使用)
ansible
  • 1 个回答
  • 20 Views
Martin Hope
Prade
Asked: 2025-04-02 09:04:06 +0800 CST

Ansible 使用 Post 方法进行循环

  • 7

我正在尝试从命令行旋转列表,并使用 Ansible URI 模块将其传递给 Servicenow 的 Post 方法。我非常接近了,但没有得到我想要的。如果有人能纠正我,我将不胜感激。

角色

  • 添加管理器
    • 任务/main.yml
    • vars/ondemand_add.yml
  1. 任务/main.yml
    - name: Submit API request to add managers
      uri:
        url: "{{ contacturl }}"
        headers: "{{ headers }}"
        user: "{{ key }}"
        password: "{{ secret }}"
        body: "{{ item }}"
        force_basic_auth: yes
        body_format: json
        method: POST
        status_code: 201
        validate_certs: false
      with_items: "{{ od_user }}"
      register: result
      changed_when: False
      failed_when: False
      ignore_errors: True
  1. vars/ondemand_add.yml
od_user:
  - cmdbRequest: "contactCreate"
    user: "{{ manager_id }}"
    servername: "{{ ansible_hostname }}"
    type: "Server Management"
    contactType: "Manager"

我的剧本命令如下

#ansible-playbook -i myhost,-e '{"role": "add_manager", "manager_id ":['1234', '4567']}' --top.yml

ansible
  • 1 个回答
  • 33 Views
Martin Hope
fccoelho
Asked: 2024-11-05 22:43:45 +0800 CST

ssh_security ansible 角色的正确 YAML 配置

  • 5

我正在创建一个 ansible rolke,用于按照文档在服务器上配置 ssh 安全性。这是我的 YAML 配置文件:

---
- name: Enable SSH security
  hosts: webservers
  tasks:
      - name: Ensure SSH is installed
        apt:
          name: openssh-server
          state: present

      - name: Configure SSH to disable password authentication
        lineinfile:
          path: /etc/ssh/sshd_config
          regexp: '^#?PasswordAuthentication'
          line: 'PasswordAuthentication no'
        notify: Restart SSH

      - name: Ensure public key authentication is enabled
        lineinfile:
          path: /etc/ssh/sshd_config
          regexp: '^#?PubkeyAuthentication'
          line: 'PubkeyAuthentication yes'
        notify: Restart SSH

      - name: Disable root SSH access
        lineinfile:
          path: /etc/ssh/sshd_config
          regexp: '^#?PermitRootLogin'
          line: 'PermitRootLogin no'
        notify: Restart SSH

  handlers:
      - name: Restart SSH
        service:
          name: ssh
          state: restarted

它看起来正确且格式良好,但是当我运行剧本时,出现此错误:

SSH password: 
BECOME password[defaults to SSH password]: 
ERROR! conflicting action statements: hosts, tasks

The error appears to be in '/<full path>/Hetzner/roles/ssh_security/tasks/main.yml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

---
- name: Enable SSH security
  ^ here

Pycharm IDE yaml 解析器未提示此错误。此配置可能存在什么问题?

ansible
  • 1 个回答
  • 14 Views
Martin Hope
user25613212
Asked: 2024-09-30 17:59:21 +0800 CST

Ansible 替换:正则表达式/替换多行?

  • 5

在 ansible 中,我想替换这个:

pgsql:
  useCluster: false
  addr: 127.0.0.1:5432
  password: ""
email:
  to: [email protected]
  port: 465
  password: ""

替换为:

pgsql:
  useCluster: false
  addr: 127.0.0.1:5432
  password: "2024xxx"
email:
  to: [email protected]
  port: 465
  password: ""

因为有两个相同的变量无法替换,所以我用了这个方法,但是无法实现,大家该如何处理呢?

replace:
      path: "config.yaml"
      regexp: '(redis:[^<]*)password: ""'
      replace: '\1password: "2024xxx"'
ansible
  • 1 个回答
  • 47 Views
Martin Hope
Panduar
Asked: 2024-09-05 04:34:55 +0800 CST

Ansible 全局 group_vars/all.yml 优先于 inventory group_vars/all.yml

  • 5

在运行 Python 3.12.4 (通过 homebrew) 的 Mac (M1) 上的 Ansible 2.16.8 中,我尝试设置一个全局 group_vars/all.yml,如果需要,可以通过 inventory/x/group_vars/all.yml 覆盖该 group_vars/all.yml。但是,似乎全局文件正在覆盖库存特定值。

给定以下文件:

# group_vars/all.yml
level: global all
# inventory/x/group_vars/all.yml
level: inventory all
# inventory/x/group_vars/test.yml
level: inventory group

并运行:

ansible all -i inventory/x -m debug -a "var=level" -o

我得到:

test-host | SUCCESS => { "changed": false, "level": "inventory group" }
non-test-host | SUCCESS => { "changed": false, "level": "global all" }

但我期望得到:

test-host | SUCCESS => { "changed": false, "level": "inventory group" }
non-test-host | SUCCESS => { "changed": false, "level": "inventory all" }
ansible
  • 1 个回答
  • 58 Views
Martin Hope
Martin Garbe
Asked: 2024-07-12 15:05:49 +0800 CST

为什么 ansible 每次访问时都会重新评估变量

  • 9

我需要在 ansible 中生成一些唯一 ID。生成成功,但访问变量会导致一些“意料之外”的行为。

剧本非常简单:

---
- hosts: localhost
  vars:
    - var1: "{{ 99999999 | random }}"
  tasks:
    - debug: msg="{{ var1 }}"
    - debug: msg="{{ var1 }}"
    - debug: msg="{{ var1 }}"

我期望总是有相同的输出,但现实却有所不同:

ansible-playbook -i localhost setup-env-test.yml
[WARNING]: Unable to parse .... as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] ***************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************
ok: [localhost]

TASK [debug] *******************************************************************************************************************************
ok: [localhost] => {
    "msg": "23317042"
}

TASK [debug] *******************************************************************************************************************************
ok: [localhost] => {
    "msg": "23320954"
}

TASK [debug] *******************************************************************************************************************************
ok: [localhost] => {
    "msg": "96866238"
}

PLAY RECAP *********************************************************************************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

我看起来好像每次访问时都会重新评估变量。

我知道当上下文发生变化时会重新求值,例如输入角色,但这里不是这种情况。我还知道通过使用 set_fact,这种行为会发生变化,并且不会再次求值变量内容。

有人能告诉我为什么要进行重新评估吗?如果能找到解释这一点的 ansible 文档就好了。

ansible
  • 1 个回答
  • 241 Views
Martin Hope
Matt Wagner
Asked: 2024-07-04 04:09:13 +0800 CST

Ansible:如何将列表和字典合并为新的嵌套字典

  • 5

我正在尝试为绑定建立反向查找(PTR)记录。

我有一个 CSV 文件,其中 IP 和主机名字段已加载到字典中(来自 read_csv),所以我猜它实际上是一个字典列表:

ok: [localhost] => {
    "msg": [
        {
            "changed": false,
            "dict": {},
            "failed": false,
            "list": [
                {
                    "hostname": "host1",
                    "ip": "10.32.1.32"                                                            
                },
                {
                    "hostname": "host2",
                    "ip": "10.50.6.71"                                                            
                },
                {                                                                                     
                    "hostname": "host3",
                    "ip": "10.36.254.41"                                                          
                },
                {
                    "hostname": "host4",
                    "ip": "10.36.254.42"
                }
            ]
        },
    ]
}

我有一个从上述字典中得出的唯一的反转最后两个八位字节列表:

ok: [localhost] => {
    "msg": [
        [
            "32.10",
            "50.10",
            "36.10"
        ],
    ]
}

我想要的是一个如下所示的新字典(格式可能不正确,请原谅):

{
   "32.10": 
          {
              "hostname": "host1",
              "ip": "10.32.1.32"                                                            
          },
   "50.10":
          {
              "hostname": "host2",
              "ip": "10.50.6.71"                                                            
          },
   "36.10":
          {                                                                                     
              "hostname": "host3",
              "ip": "10.36.254.41"                                                          
          },
          {
              "hostname": "host4",
              "ip": "10.36.254.42"
          }
}

新的“超级”字典应该只包含匹配网络的 IP + 主机名条目(例如,所有 10.36.xx 条目都应位于“36.10”下)。

从这个新的字典中,我应该能够在模板中循环它来生成 PTR 记录。

我不知道如何合并这两个数据结构。:-/

谢谢你!

ansible
  • 1 个回答
  • 12 Views
Martin Hope
Appleoddity
Asked: 2024-05-28 23:20:54 +0800 CST

为什么此代码会生成 Ansible 弃用警告?

  • 5
# Build a list of Adtran AP mac addresses found on directly connected switch ports
- name: PARSE AND FILTER MAC ADDRESS-TABLE
  ansible.builtin.set_fact:
    macs: "{{ macs + [item] }}"
  loop: "{{ macs_raw.stdout[0] | ansible.netcommon.parse_cli_textfsm('templates/cisco_ios_show_mac-address-table.textfsm') }}"
  when:
    - item.DESTINATION_PORT[0] not in neighbors.keys()   # Ignore macs found on neighboring switches
    - item.DESTINATION_ADDRESS is search('^0019\.92.*$')   # Ignore macs that aren't Adtran APs

我收到以下弃用警告:

[DEPRECATION WARNING]: Use 'ansible.utils.hwaddr' module instead. This feature will be removed from ansible.netcommon in a release after 2024-01-01. Deprecation warnings can be disabled by
 setting deprecation_warnings=False in ansible.cfg.

ansible --version

ansible [core 2.16.6]
  config file = /workspaces/git/my-repo/ansible/ansible.cfg
  configured module search path = ['/home/vscode/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/python/3.11.6/lib/python3.11/site-packages/ansible
  ansible collection location = /home/vscode/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/python/current/bin/ansible
  python version = 3.11.6 (main, Dec 19 2023, 21:21:06) [GCC 11.4.0] (/usr/local/python/3.11.6/bin/python)
  jinja version = 3.1.4
  libyaml = True

为什么?

ansible
  • 1 个回答
  • 32 Views
Martin Hope
Saaru Lindestøkke
Asked: 2024-05-18 01:59:04 +0800 CST

如何在 Windows 上使用 Ansible 隐藏文件或文件夹?

  • 4

有没有办法在 Windows 上使用 Ansible 将文件或文件夹标记为隐藏?

我检查过win_file模块,

https://docs.ansible.com/ansible/latest/collections/ansible/windows/win_file_module.html

但找不到创建隐藏文件的选项。

我知道我可以运行Powershell 脚本,但为了不让我的日志乱七八糟地带有“已更改”标记,我还需要检查某个项目是否已隐藏。

是否存在使用 Ansible 创建隐藏项目或隐藏现有项目的现有方法,而无需检查该项目是否隐藏?

ansible
  • 1 个回答
  • 82 Views
Martin Hope
Saaru Lindestøkke
Asked: 2024-05-02 23:42:05 +0800 CST

如何根据循环项具有的键设置 Ansible 命令参数?

  • 6

语境

我正在使用 Ansible 通过win_chocolatey模块设置 Windows 服务器。

在我的group_vars/windows_servers.yml文件中我有:

choco_packages:
  - 7zip
  - git
  - vscode

在我的剧本任务中,我有:

- name: Install and upgrade software using Chocolatey
  hosts: windows_servers
  tasks:
    - name: Install latest package versions
      chocolatey.chocolatey.win_chocolatey:
        name: "{{ item }}"
        state: latest
      loop: "{{ choco_packages }}"

到目前为止,一切都很好。

但是,现在我有一些软件包需要安装特定版本和/或需要传递某些安装参数。我可以在 my 中创建三个列表group_vars/windows_servers.yml,每个列表对应一个目标(最新包、特定版本、带参数的包),并由每个列表提供三个任务,设置适当的键。

但为了减少维护负担,我宁愿有一个包列表,其中我可以选择设置所需的键,并有一个任务根据可用的项目键设置适当的选项。例如:

choco_packages:
  - { name: "7zip", version: "22.1" }
  - { name: "git" , install_args: [ "/NoGuiHereIntegration", "/NoGitLfs"] }
  - { name: "vscode" }

问题

我应该如何编写 Ansible 任务来设置版本和安装参数(如果提供),并安装最新版本(如果没有)?我希望能**kwargs在 Python 中找到类似的东西。

我尝试过的

使用when:条件,我可以创建三个处理适用列表部分的任务:

- name: Install and upgrade software using Chocolatey
  hosts: windows_servers
  tasks:
    - name: Install latest versions
      chocolatey.chocolatey.win_chocolatey:
        name: "{{ item.name }}"
        state: latest
      loop: "{{ choco_packages }}"
      when: item.version is not defined and item.install_args is not defined
    - name: Install specific versions
      chocolatey.chocolatey.win_chocolatey:
        name: "{{ item.name }}"
        version: "{{ item.version }}"
        state: present
      loop: "{{ choco_packages }}"
      when: item.version is defined
    - name: Install with parameters
      chocolatey.chocolatey.win_chocolatey:
        name: "{{ item.name }}"
        install_args: "{{ item.install_args }}"
        state: present
      loop: "{{ choco_packages }}"
      when: item.install_args is defined

但这不处理versionand的组合install_args并且相当冗长。

ansible
  • 2 个回答
  • 35 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve