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
    • 最新
    • 标签
主页 / user-433824

Rafal Niznik's questions

Martin Hope
Rafal Niznik
Asked: 2023-04-16 21:40:58 +0800 CST

Ansible:如何更新与另一个目录键匹配的数组键值

  • 6

我有以下词典:

question:
  first_run:
    app:
      - answer: null
        name: first_name
        question: What is your First name?
      - answer: null
        name: last_name
        question: What is your Last name?
    core:
      - question: question1
      - question: question2
answer:
  first_name: John
  last_name: Smith

我使用以下任务手动更新question.first_run.app下的答案值,效果很好

- name: Update question variable
  set_fact:
    question:
      first_run:
        app:
          - name: first_name
            question: "What is your First name?"
            answer: "{{ answer.first_name }}"
          - name: last_name
            question: "What is your Last name?"
            answer: "{{ answer.last_name }}"

我还测试了以下任务(对于这种情况,这将是更可取的方法)

- name: "TEST-1"
  set_fact:
    question: "{{ question | combine({'first_run': {'app': question.first_run.app | map('combine', {'answer': update }) | list }}, recursive=True) }}"
  loop: "{{ question.first_run.app }}"
  vars:
    update: "{{ answer[item.name] if item.name in answer.keys() else item.answer }}"

- name: "TEST-2"
  set_fact:
    question: "{{ question | combine({'first_run': {'app': question.first_run.app | map('combine', {'answer': (answer[item.name])})}}, recursive=True) }}"
  loop: "{{ question.first_run.app }}"
  vars:c
    update: "{{ answer | dict2items | selectattr('key', 'in', [item.name]) | map(attribute='value') | first }}"
#    update: "{{ answer[item.name] }}"    # THis also works

但是 TEST-1 和 TEST-2 的输出总是这样的:

question:
  first_run:
    app:
      - answer: Smith
        name: first_name
        question: What is your First name?
      - answer: Smith
        name: last_name
        question: What is your Last name?
    core:
      - question: question1
      - question: question2

我测试的最后一个任务是:
它可以工作,但所需的结果保存在新的 var app_list 下

- name: "TEST-3"
  set_fact:
    question: "{{ question | combine({'first_run': {'app': app_list}}, recursive=True) }}"
  vars:
    app_list: []
  loop: "{{ question.first_run.app }}"
  set_fact:
    app_list: "{{ app_list + [item | combine({'answer': answer[item.name]})] }}"

问题:

我需要更新任务“更新问题变量”,这样我就不必手动放置数组question.first_run.app中的所有这些键

我如何使用循环将question.first_run.app.name与answer.key匹配,以便输出看起来像这样?最好调整 TEST-1 或 TEST-2 或 TEST-3

question:
  first_run:
    app:
      - answer: John
        name: first_name
        question: What is your First name?
      - answer: Smith
        name: last_name
        question: What is your Last name?
    core:
      - question: question1
      - question: question2
ansible
  • 1 个回答
  • 49 Views
Martin Hope
Rafal Niznik
Asked: 2023-01-22 07:32:53 +0800 CST

Ansible:Firewalld 如何设置多项服务(从另一个剧本循环)

  • 5

当我尝试为该区域设置多个服务时,我看到了这个问题。
只设置了第一个,另一个总是出现以下错误:

TASK [Set services] *******************************************************************************************************************************************************************************************************************************
changed: [localhost] => (item=ssh)
failed: [localhost] (item= samba) => {"ansible_loop_var": "item", "changed": false, "item": " samba", "msg": "ERROR: Exception caught: org.fedoraproject.FirewallD1.Exception: INVALID_SERVICE: Zone 'work': ' samba' not among existing services Permanent operation, Services are defined by port/tcp relationship and named as they are in /etc/services (on most systems)"}


如果我切换服务并将 samba 作为第一个而不是为 ssh 生成相同的错误

主要剧本:

- name: Configure Firewalld
  hosts: localhost
  gather_facts: no

  vars:
    firewall:
    - zone: work
      service: ssh, samba
    - zone: public
      service: samba

  tasks:
  - name: Set services
    include_tasks: ./service.yml
    loop: "{{firewall | selectattr('service', 'defined') | list}}"
    loop_control:
      loop_var: service

服务剧本:


- set_fact:
    zone: "{{service.zone}}"


- name: "Servcie name"
  debug:
    var: item 
  with_items: "{{service.service | split(',')}}"


- name: Set services 
  ansible.posix.firewalld:
    zone: "{{zone}}"
    service: "{{item}}"
    state: enabled
    permanent: yes
  with_items: "{{service.service | split(',')}}"
  register: result

任何建议如何解决这个问题?

ansible
  • 1 个回答
  • 13 Views
Martin Hope
Rafal Niznik
Asked: 2022-05-28 06:58:33 +0800 CST

如何使用 ansible.builtin.pause 模块动态构建菜单?

  • 3

这里的目标是从一个可用的变量文件动态地构建一个菜单

在此示例中,我使用ansible.builtin.pause模块,但我不确定这是最好的方法

变量文件:vars.yml

---
menu:
  ansible:
    main:
    - option: 1
      name: "Add..."
    - option: 2
      name: "Delete..."
    - option: 3
      name: "Empty..."
    add:
      - option: 1
        name: "Add something..."
      - option: 2
        name: "Add something to..."
    delete:
    empty:
  ssh:
    main:

剧本:test.yml

- name: "PLAY: > TEST"
  hosts: localhost
  gather_facts: no
  vars_files: vars.yml
  pre_tasks:

  - name: Dynamicaly construct menu
    pause:
      prompt:
        "\n
        Ansible options:\n
        =====================================\n
        {{item.option}}- {{item.name}}"
    register: result
    loop: "{{menu.ansible.main}}"

  - debug: 
      msg: "Option 1 was selected"
    when: result.user_input == '1'

输出:

PLAY [PLAY: > TEST] *******************************************************************************************************************************************************************************************************************************************************

TASK [Dynamicaly construct menu] ******************************************************************************************************************************************************************************************************************************************
[Dynamicaly construct menu]

 Ansible options:
 =====================================
 1- Add...:

如您所见,它仅显示主要部分,而不是全部。

问题:

如何一次显示所有可用选项并保存用户选择,以便根据条件运行下一个任务?

我很确定必须先生成菜单并将其保存在一个变量下,然后再发送到,ansible.builtin.pause但我不确定如何实现。

感谢帮助

ansible menu
  • 1 个回答
  • 135 Views
Martin Hope
Rafal Niznik
Asked: 2022-05-05 06:48:38 +0800 CST

Alpine Linux:将 Podman systemd 转换为 openrc

  • 1

我使用 Alpine Linux 和 podman 命令生成容器 systemd

命令:

podman generate systemd -n test -f

输出:

# container-test
# autogenerated by Podman 3.4.7
# Fri Apr 29 02:29:47 CEST 2022

[Unit]
Description=Podman container-test.service
Documentation=man:podman-generate-systemd(1)
Wants=network-online.target
After=network-online.target
RequiresMountsFor=/run/containers/storage

[Service]
Environment=PODMAN_SYSTEMD_UNIT=%n
Restart=on-failure
TimeoutStopSec=70
ExecStart=/usr/bin/podman start test
ExecStop=/usr/bin/podman stop -t 10 test
ExecStopPost=/usr/bin/podman stop -t 10 test
PIDFile=/run/containers/storage/overlay-containers/fc64dadf99ddb38f98ddcb3e022bc575ca39cd57f4e2e8c5cc63595377e2c3ad/userdata/conmon.pid
Type=forking

[Install]
WantedBy=default.target

因为 Alpine Linux 没有使用 systemd 我需要将此容器服务转换为 openrc

目标是:
将特定容器作为服务启动,并且此容器必须仅对 root 可见

systemd alpine-linux
  • 1 个回答
  • 334 Views
Martin Hope
Rafal Niznik
Asked: 2022-04-25 07:07:47 +0800 CST

Ansible:如何验证 ansible_become_user,以及是否缺少用户输入提示(多个主机)

  • 1

我使用多个主机,如果清单或剧本中未设置 ansible_become_user 和 ansible_become_password,我想提示用户提供 sudo 用户名和密码

我使用以下内容:

库存文件:

os:
  children:
    centos:
      hosts:
        clean_centos_1:
          vars:
          ansible_become_user: root
          ansible_become_password: root
    rocky:
      hosts:
        clean_rocky_1:
    ubuntu:
      hosts:
        clean_ubuntu_1:
    debian:
      hosts:
        clean_debian_1:
    alpine:
      hosts:
        clean_alpine_1:
          vars:
          ansible_become_user: root
          ansible_become_password: root
  vars:
    ansible_user: test 
    ansible_password: test

剧本

- name: "PLAY1"
  hosts: all
  gather_facts: no

  tasks:
## Identify ansible_become_user if present
#------------------------------------------------------
  - name: "Test if ansible_become_user is empty" 
    debug:
      var: ansible_become_user
    register: result
    when: ansible_become_user | length > 0
    ignore_errors: yes

  - name: "Set status_ansible_become_user"
    set_fact:
      status_ansible_become_user: "{{ status_ansible_become_user|default({}) | combine( { ansible_host: 'absent' if result.failed is true else 'present' }) }}"


## Get user input
#------------------------------------------------------
  - name: "User-input: <ansible_become_user>"
    pause:
      prompt: "\nEnter root username or sudo username for host: {{ansible_host}}"
    when: item == 'absent'
    loop: "{{status_ansible_become_user.values()}}"

  - set_fact:
      ansible_become_user: "{{ result.user_input }}"
    when: result.failed is false


  - debug:
      var: item == 'absent'
    loop: "{{status_ansible_become_user.values()}}"

上例中因为 clean_centos_1 排在第一位,并且设置了 ansible_become_user 和 ansible_become_passwor,其他主机如下跳过,没有提示

TASK [User-input: <ansible_become_user>] **********************************************************************************************************************************************************************************************************************************
skipping: [clean_centos_1] => (item=present)

TASK [set_fact] ***********************************************************************************************************************************************************************************************************************************************************
fatal: [clean_centos_1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'user_input'\n\nThe error appears to be in '/git/ansible/role/rar.pkg.python/playbook/test.yml': line 28, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - set_fact:\n    ^ here\n"}
skipping: [clean_rocky_1]
skipping: [clean_debian_1]
skipping: [clean_ubuntu_1]
fatal: [clean_alpine_1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'user_input'\n\nThe error appears to be in '/git/ansible/role/rar.pkg.python/playbook/test.yml': line 28, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - set_fact:\n    ^ here\n"}


问题:
如何正确验证是否设置了 ansible_become_user 和 ansible_become_password,如果未设置,如何提示使用以提供每个主机可能不同的 ansible_become_user 和 ansible_become_password?

ansible prompt
  • 1 个回答
  • 184 Views
Martin Hope
Rafal Niznik
Asked: 2022-04-22 00:28:34 +0800 CST

Ansible:从任务运行新剧本

  • 2

假设如下:

主要剧本.yml

- name: Play-1
  hosts: localhost
  connection: local
  gather_facts: no
  roles:
    - role: my-role
      vars:
        newhost: 192.168.1.1

生成的剧本.yml

- name: Play-1
  hosts: newhost
  gather_facts: yes
  
  tasks:
  - name: Task1
  - name: Task2
  - name: Task3

角色的主要任务:

- name: "Role MAIN-1"
  add_host:
    name: newhost
    ansible_host: "{{newhost}}"

- include: generated_playbook.yml

错误:


ERROR! conflicting action statements: hosts, tasks

The error appears to be in 'generated_playbook.yml': line 1, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- name: Generated Playbook
  ^ here

我创建了新角色并将此角色包含在主要剧本中。
在这个角色中,我将新主机添加到内存清单中,然后,我使用 j2 模板示例输出生成了新的剧本 generate_playbook.yml

问题:
有没有办法只在新添加的主机上运行这个新生成的剧本而不向主剧本添加任何其他内容?
我试图使用 import-playbook 或包含在角色中,但这失败了

ansible display
  • 1 个回答
  • 63 Views
Martin Hope
Rafal Niznik
Asked: 2022-04-20 05:25:10 +0800 CST

Ansible 将字典转换为字典/数组的混合

  • 1

我使用以下文件:变量文件:db.yml

x86_64:  
  alpine:
    version: 3.15.0
  debian:
    version: 11.3.0

aarch64:
  alpine:
    version: 3.15.0
  debian:
    version: 11.3.0

剧本:剧本.yml

---
- name: "Playbook" 
  hosts: localhost
  connection: local
  gather_facts: no

  tasks:
  - name: Import variables
    ansible.builtin.include_vars:
      file: db.yml
      name: db

  - name: DENUG >>> db
    debug:
      var: db


  - name: DENUG >>> db
    debug:
      var: db[item].alpine
    loop: "{{ db.keys()|list }}"
    when: db[item].alpine is defined

输出:

ok: [localhost] => (item=x86_64) => {
    "ansible_loop_var": "item",
    "db[item].alpine": {
        "version": "3.15.0"
    },
    "item": "x86_64"
}
ok: [localhost] => (item=aarch64) => {
    "ansible_loop_var": "item",
    "db[item].alpine": {
        "version": "3.15.0"
    },
    "item": "aarch64"
}

现在我已将 db.yml 转换为新样式,如下所示:

x86_64:
  - distribution: alpine
    version: 3.15.0
  - distribution: debian
    version: 11.3.0

aarch64:
  - distribution: alpine
    version: 3.15.0
  - distribution: debian
    version: 11.3.0

问题:
如何运行类似的命令如下:

- name: DENUG >>> db
    debug:
      var: db[item].alpine
    loop: "{{ db.keys()|list }}"
    when: db[item].alpine is defined  

并获得与输出中可用的相同结果。
基本上如何遍历这个新的 db.yml 来获取 db.x86_64.alpine.version 的值

ansible array
  • 1 个回答
  • 1174 Views
Martin Hope
Rafal Niznik
Asked: 2022-03-21 06:14:31 +0800 CST

Ansible:为什么在以下情况下跳过任务:var为真

  • 1

从下面的示例中,
为什么当 db.x86_64.alpine.update 为真时跳过了 DEBUG 2
我也一直在尝试“何时:db.x86_64.alpine.update|bool 为真”,但这也失败了

变量文件 db.yml

shell> cat db.yml
x86_64:
  alpine:
    update: true
    version_current: 3.14.0
    version_new: 3.15.

aarch64:
  alpine:
    update: true
    version_current: 3.14.0
    version_new: 3.15.0

剧本

---
- name: Playbook test
  hosts: localhost
  tasks:
  - ansible.builtin.include_vars:
      file: db.yml
      name: db

  - name: DEBUG 1
    debug:
      var: db.x86_64.alpine.update |type_debug

  - name: DEBUG 2
    debug:
      msg:
        - "MESSAGE"
    when: db.x86_64.alpine.update is true

运行时输出

PLAY [Playbook test] ******************************************************************************************************************************************************************************************************************************************************

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

TASK [ansible.builtin.include_vars] ***************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [debug] **************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "db.x86_64.alpine.update |type_debug": "builtin_function_or_method"
}

TASK [debug] **************************************************************************************************************************************************************************************************************************************************************
skipping: [localhost]

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

问题:
当 db.x86_64.alpine.update 为 true 时如何显示消息

variable ansible
  • 2 个回答
  • 412 Views
Martin Hope
Rafal Niznik
Asked: 2022-03-19 13:03:48 +0800 CST

Ansible:如何更新文件中的目录值

  • 1

我使用以下内容:
目录文件:dir.yml

x86_64:
  alpine:
    update:
    version: 3.14.0

aarch64:
  alpine:
    update:
    version: 3.14.0 

可靠的剧本:

---
- name: Playbook  
  hosts: localhost

  vars:
    new_version: 3.15.0
    update:

  tasks:
  
    - name: "Include: dir.yml"
      ansible.builtin.include_vars:
        file: dir.yml
    
    - debug:
        msg: 
          - "{{x86_64.alpine.version}}"
          - "{{new_version}}"
          - "{{update}}"
    
    - set_fact:
        update: "{{'true' if x86_64.alpine.version < new_version else 'false'}}"

问题:
如何更新 dir.yml 中的以下字段

x86_64:
  alpine:
    update: true <------

我试图使用 ansible.builtin.replace 或 ansible.builtin.lineinfile 但仍然无法弄清楚

有没有人对此有任何解决方案

ansible
  • 1 个回答
  • 105 Views

Sidebar

Stats

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

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve