标签: ansible-inventory

Ansible:如何在 Ansible 中仅从 FQDN 获取 IP 地址?

所以,这是设置。

我通过额外的变量将 FQDN 列表传递给剧本,但控制机器的密钥与其对应的 IP 地址一起复制。此外,它们在清单中也没有特定的一组。

这个剧本的主机是一个服务器,它可能是也可能不是我通过额外变量传递的这个列表的一部分。因此,剧本没有任何关于这些节点的事实。

所以,我的问题是如何即时获取各个主机的 IP 地址,以便我可以遍历它们以对我之前通过另一个剧本/角色使用它们的 FQDN 设置的某些属性进行一些检查。(注意:我严格需要 FQDN 来设置这些属性)

我已经使用了 delegate_to 模块,并传递了 FQDN 并且它可以工作,但它询问我是否要继续连接(是/否)并等待用户输入以进行初始运行(如果给出是,它按预期工作)。他们还有其他更简单、更干净的方法来解决这个问题吗?
(注意:请记住,我无法修改库存的内容,例如放置别名等)

以下是代码片段:

- name: Checks
  become_user: hdfs
  shell: hdfs dfsadmin –report
  delegate_to: "{{ item.host_name }}"
  with_items:
    - "{{rackDetails}}"
Run Code Online (Sandbox Code Playgroud)

这是主 yml 文件的片段,其中调用了上述角色:

- name: Checks for the property
  hosts: servernode
  roles:
    - Checks
Run Code Online (Sandbox Code Playgroud)

库存代码片段如下所示:

[servernode]
10.0.2.15 ansible_ssh_user =授予ansible_ssh_pass =许可
[全部]
10.0.2.15 ansible_ssh_user =授予ansible_ssh_pass =授权
10.0.2.16 ansible_ssh_user =授权ansible_ssh_pass =授权
10.0.2.17 ansible_ssh_user =授权ansible_ssh_pass =授权
10.0.2.18 ansible_ssh_user =授权ansible_ssh_pass =授予
10.0.2.19 ansible_ssh_user=grant ansible_ssh_pass=grant

最后,这就是我通过额外变量传递的内容:

{"rackDetails":[{"host_name":"prod_node1.paas.com","rack_name":"/rack1"},{"host_name":"prod_node2.paas.com","rack_name":"/rack2"},{"host_name":"prod_node3.paas.com","rack_name":"/rack3"}]}
Run Code Online (Sandbox Code Playgroud)

ansible ansible-facts ansible-inventory

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

有没有办法验证 Ansible Inventory 文件中组的主机数量?

我的要求如下所示,我有一个 Ansible 库存文件,它根据下面显示的组件分为几组:

[all]

node1 
node2
node3
node4

[webapp]
node3
node4

[ui]
node1
Run Code Online (Sandbox Code Playgroud)

如果条件失败,那么有没有办法验证库存文件中组的主机数量,那么 playbook 不应运行?

我的条件是:ui组应该始终只有一个主机。

前任:

[ui]
node1  -- condition check pass proceed with playbook execution

[ui]
node1 
node2  -- condition fails should stop playbook execution with exception 
          with ui group cannot have more than one hosts
Run Code Online (Sandbox Code Playgroud)

ansible ansible-inventory

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

Ansible 打印创建的主机

我写了一个 Ansible Playbook 来创建多个 VM。Playbook 分为两个文件。Main.yaml 和 vars.yaml。它创建了虚拟机,它似乎运行良好。我没有收到任何错误,所以我认为它已成功将创建的主机添加到库存中。我想检查创建的主机是否已添加到清单中。我如何打印/列出库存的主机?我的目标是稍后在创建的 VM 上运行脚本。谢谢。

**Main.yaml**
#########CREATING VM#########
---
- hosts: localhost
  vars:
    http_port: 80
max_clients: 200
  vars_files:
   - vars.yaml
  tasks:
  - name: create VM
    os_server:
      name: "{{ item.name }}"
      state: present
      image: "{{ item.image }}"
      boot_from_volume: True
      security_groups: ssh
      flavor: "{{ item.flavor }}"
      key_name: mykey
      region_name: "{{ lookup('env', 'OS_REGION_NAME') }}"
      nics:
        - net-name: private
      wait: yes
    register: instances
    with_items: "{{ instance_definitions }}"
 ############################################
  - name: whait 15 seconds
    pause: seconds=15
    when: instances.changed
######DEBUG#################################
  - …
Run Code Online (Sandbox Code Playgroud)

openstack ansible ansible-inventory

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

Ansible Inventory 中主机之间的暂停时间

我正在我的剧本中尝试以下任务。但不执行暂停。我希望在删除每个主机后播放应该暂停 30 秒。

name: delete host from the NagiosXI
shell: curl -k -XDELETE "https://10.000.00.00/nagiosxi/api/v1/config/host?apikey=qdjcwc&pretty=1&host_name={{ item }}&applyconfig=1"

  - pause:
    seconds: 120

  ignore_error: yes
  with_items:
    - "{{ groups['grp1'] }}"
Run Code Online (Sandbox Code Playgroud)

如果以正确的方式做或向我提出建议,有人可以建议这是否是正确的方式。我也使用了 serial=1 模块,但它仍然无法正常工作。

ansible pause ansible-inventory

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

Ansible:从命令行覆盖清单的 ansible_host

我有一个简单的库存hosts

dockermachine ansible_host=10.10.10.10
Run Code Online (Sandbox Code Playgroud)

我需要能够从命令行提供我希望的任何其他 IP,覆盖默认值,例如: ansible-playbook -i hosts@dockermachine.ansible_host=11.11.11.11 site.yml

我怎样才能做到这一点?

ansible ansible-inventory

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

使用ansible在远程服务器上执行python脚本错误

我使用 ansible 2.8.3 Rhel 8 以 root@xxx12 身份登录。
我希望将一些文件复制到 root@xxx13 Rhel 8,然后执行 python 脚本。
我能够使用 ansible成功复制文件。我什至复制了密钥,现在它不再需要 ssh。

但在执行脚本期间:'致命:[web_node1]:失败!=> {"changed": false, "msg": "无法在 Ansible 控制器上找到或访问 '/root/ansible_copy/write_file.py'。\n如果您正在使用模块并期望该文件存在于远程,请参阅remote_src选项“}'
请注意,我是ansible的新手。
我猜有一些权限问题。
如果可能的话请帮助我。期待中的感谢

**yaml_file**
-
    name: Copy_all_ansible_files_to_servers
    hosts: copy_Servers
    become: true
    become_user: root
    tasks:
    -
      name: copy_to_all
      copy:
       src: /home/testuser/ansible_project/{{item}}
       dest: /root/ansible_copy/{{item}}
       owner: root
       group: root
       mode: u=rxw,g=rxw,o=rxw
      with_items:
         - write_file.py
         - sink.txt
         - ansible_playbook_task.yaml
         - copy_codes_2.yaml
      notify :
           - Run_date_command

    -
      name: Run_python_script
      script: /root/ansible_copy/write_file.py > /root/ansible_copy/sink.txt
      args:
        #chdir: '{{ role_path }}' …
Run Code Online (Sandbox Code Playgroud)

ansible ansible-inventory

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

如何在ansible中将主机与变量配对

我有一个库存,例如:

all:
  children:
    server_group1:
      hosts:
        host1:
    server_group2:
      children:
        app1:
          hosts:
            host2:
            host3:
        app2:
          hosts:
            host4:
            host5:
    server_group3:
...
Run Code Online (Sandbox Code Playgroud)

我已经像这样组织了我的服务器变量:

我试图以该组的名字命名我的字典(从而使它们独一无二)并在我的剧本中访问它:

hosts: server_group2
tasks:
  - name: check file
    local_action: stat path=path/to/test/{{hostvars[0].name1}}
    register: payld_txt

  - name: conditional transfer
    copy:
      src: path/to/test/{{hostvars[0].name1}}
      dest: /svr/path/{{hostvars[0].name2}}
    when: payld_txt.stat.exists
Run Code Online (Sandbox Code Playgroud)

我最终遇到了这个错误:

该任务包含一个带有未定义变量的选项。错误是:“name1”未定义

我哪里错了?

ansible ansible-inventory

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

Ansible 中的 SQL Server 数据库查询

我想知道社区中是否有人使用 Ansible 来操作 Microsoft SQL Server 数据库?

我们想要向用 Ansible 编写的环境配置脚本添加一个任务,该任务将INSERT行写入 SQL Server 表。这些行将具有在变量文件中指定的参数。

我一直无法找到实现此目的的特定 Ansible 模块,因此想听听是否有人以其他方式取得了成功?

sql-server ansible ansible-inventory

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

如何使用ansible中的预任务模块来验证输入参数?

在 Ansible 中运行主要任务之前,我想验证一些事情。例如,下面的命令从用户那里获取 2 个输入参数,因此我想在执行主要任务之前验证它们。

ansible-playbook -i my-inventory my-main.yml --tags=repodownload -e release_version=5.0.0-07 -e target_env=dev/prod/preprod

在上述情况下,release_version 不应为空,并且 target_env 必须是这些类型的值 - 5.0.0.34

我想向用户显示一条有关错误的消息。我如何实现它?

任何帮助表示赞赏。

ansible ansible-inventory

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

Ansible 检查特定目录是否存在,如果不存在则创建它

我试图找出 ansible 中是否存在特定的导演,如果没有,创建一个..我在这里面临的问题是下面的代码不应该创建 /var/tmp/directory,但它在运行时创建.. (以下代码不是用于创建我认为的目录)

   tasks:
     - name: Checking /var/tmp/ansible directory exists
       stat: path=/var/tmp/ansible
       register: status
Run Code Online (Sandbox Code Playgroud)

谁能帮我解决这个问题..

ansible ansible-inventory

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