标签: ansible-inventory

如何在ansible库存文件中定义hash(dict)?

我能够在group_vars/all中定义如下所示的哈希(dict):

region_subnet_matrix:
  site1:
    region: "{{ aws_region }}"
    subnet: "subnet-xxxxxxx"
    zone: "{{aws_region}}a"
  site2:
    region: "{{ aws_region }}"
    subnet: "subnet-xxxxxxx"
    zone: "{{aws_region}}b"
Run Code Online (Sandbox Code Playgroud)

但对于我的生活,我无法弄清楚如何在hosts文件下定义它

[all:vars]
region_subnet_matrix="{
  site1:
    region: "{{ aws_region }}"
    subnet: "subnet-xxxxxxx"
    zone: "{{aws_region}}a"
  site2:
    region: "{{ aws_region }}"
    subnet: "subnet-xxxxxxx"
    zone: "{{aws_region}}b"
}"
Run Code Online (Sandbox Code Playgroud)

我知道这是不正确的,但我不知道正确的方法.有人可以开导我吗?

ansible ansible-inventory

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

将文件从 ansible 中的相对路径复制到服务器

如何传递相对路径,以便 Ansible 可以从node/keys服务器复制文件并将其复制到服务器?

剧本是ansible/playbook

我的目录结构是:

??? ansible
?   ??? inventory
?   ??? playbook
??? node
?   ??? keys
?   ??? index.js
?   ??? node_modules
?   ??? package-lock.json
?   ??? utils
??? shell
    ??? data.json
    ??? create-data.sh
    ??? destory.sh
    ??? firewall-rules.sh
Run Code Online (Sandbox Code Playgroud)

下面是剧本:

- hosts: all
  vars:
    source: "{{ source }}"
    destination: /home/ubuntu

  tasks: 

    - name: Copy files
      copy: 
        src:  "{{ source }}"
        dest: "{{ destination }}"
Run Code Online (Sandbox Code Playgroud)

这就是我运行的方式:

ansible-playbook -i inventory/inventory.yaml playbook/crypto-generate.yaml
 --extra-vars "source=../node/keys"
Run Code Online (Sandbox Code Playgroud)

我正在尝试传递相对路径。

ansible ansible-facts ansible-inventory

8
推荐指数
2
解决办法
5311
查看次数

如何在运行ansible的服务器上创建目录

我是 Ansible 的新手,我正在尝试创建一个 Ansible Playbook,它将不同主机的目录备份到控制服务器(这是调用 Ansible 的同一台机器)。

我想首先在控制服务器上创建目录,然后执行同步(ansible.posix.synchronize)或复制(ansible.builtin.copyansible.builtin.fetch)命令将所有目录从所有主机复制到控制服务器。

ansible ansible-inventory

8
推荐指数
1
解决办法
8307
查看次数

Ansible inventory:aws_ec2 模块——寻找工作“groups:”函数的示例

我有一个使用 aws_ec2 模块的动态 ansible 库存。一般来说,它运作得很好。然而,有一个问题。我曾多次尝试使用“groups:”关键字创建组,但所有尝试都失败了。文档有点稀疏——事实上,完整的文档就在这里:

groups: (dictionary)
Default:{}
Add hosts to group based on Jinja2 conditionals.
Run Code Online (Sandbox Code Playgroud)

我尝试了多种语法、Jinja2、条件、声明,但到目前为止还没有成功创建名为“foo”的组。有一段时间我想也许我需要传递一个返回 true 或 false 的小代码 snibbet,从而包含或排除目标主机。事实似乎并非如此。我想知道这里是否有人使用过“groups:”关键字并且比我更进一步。我在谷歌搜索时发现很少。FWIW,我在 Linux 上使用 ansible 2.9.9。

一些不起作用的例子:

---
plugin: aws_ec2

### fails to create a group
groups:
  foo: >-
    tags.get('Name') if tags.get('Name') == 'foo-server'

### returns every host in the AWS account. 
groups: 
  bar:
    - "{{ tags.get('Name') == 'bar-server' }}"
Run Code Online (Sandbox Code Playgroud)

此外,该模块很难使用ansible的调试工具。尽管我发现它们在其他上下文中非常有用,但许多(例如 playbook 调试器)似乎无法正常工作。如果您在这种情况下可能有任何调试技巧,我们将不胜感激。

jinja2 ansible ansible-2.x ansible-inventory

8
推荐指数
1
解决办法
1160
查看次数

运行 playbook EC2 库存时收到 python 警告

我对 Ansible 非常陌生,我讨厌在运行剧本时收到警告。这个环境正在用于我的教育。

环境:

  • AWS EC2
  • 4 乌班图 20
  • 3 个 Amazon Linux2 主机

存货

  • 使用动态清单脚本

剧本

  • 只需对所有主机运行一个简单的 ping 操作即可。我想测试库存

警告

我尝试过的事情

  • 更新了主机上的所有符号链接以指向 python3 版本
  • 将行“ansible_python_interpreter = /usr/bin/python”添加到“/etc/ansible/ansible.cfg”
  • 我依赖那个 cfg 文件

我想知道如何解决这个问题。由于我没有运行静态清单,因此我认为我无法在每个主机或主机组上指定一个解释器。当剧本运行时,似乎有些东西配置不正确,我想对其进行排序。这仅存在于 Amazon Linux 实例上。Ubuntu 实例很好。

迈克尔

ansible ansible-inventory

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

在Ansible中为所有主机定义默认变量的正确方法

[all:vars]我的ansible清单文件中有一个定义如下:

[all:vars]
ansible_shell_type=bash
ansible_user=certain_user
ansible_ssh_common_args="-o ConnectionAttempts=10"
Run Code Online (Sandbox Code Playgroud)

我计划将这些变量移动到ansible.cfg为所有主机设置默认值.它会以类似的方式工作还是有任何情况需要考虑?还有哪些其他替代品可以[all:vars]从库存文件中删除?

ansible ansible-inventory

7
推荐指数
2
解决办法
5749
查看次数

我可以在命令行上覆盖 Ansible SSH 密钥清单变量 ansible_ssh_private_key_file 吗?

在 Ansible 2.4.2.0 中,我可以使用ansible_ssh_private_key_file命令行覆盖清单中设置的 SSH 密钥吗?--private-key在此问题上报告的选项是不可能的:未考虑命令行标志(可变权重)(错误或功能?)

库存文件:

[group1]
instance1   ansible_host=host1  ansible_user=user1  ansible_ssh_private_key_file=~/.ssh/user1host1-id_rsa
Run Code Online (Sandbox Code Playgroud)

命令行:

ansible-playbook playbook1.yml --private-key=some_other-id_rsa
Run Code Online (Sandbox Code Playgroud)

ssh ssh-keys ansible ansible-inventory

7
推荐指数
1
解决办法
8284
查看次数

Ansible 停止通过 ssh 连接到主机

介绍

一个多月以来,我一直在运行以下命令:

ansible-playbook -vvvvi host_test rhel-tests.yml
Run Code Online (Sandbox Code Playgroud)

它通过 SSH 连接并在主机上成功运行测试,没有任何问题。但截至最近几天,我在运行时收到以下信息:

fatal: [10.2.16.2]: UNREACHABLE! => {
    "changed": false, 
    "unreachable": true
}

MSG:

Failed to connect to the host via ssh: OpenSSH_7.6p1, LibreSSL 2.6.2
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 48: Applying options for *
debug1: auto-mux: Trying existing master
debug2: fd 3 setting O_NONBLOCK
debug2: mux_client_hello_exchange: master version 4
debug3: mux_client_forwards: request forwardings: 0 local, 0 remote
debug3: mux_client_request_session: entering
debug3: mux_client_request_alive: entering
debug3: mux_client_request_alive: done pid = 35742
debug3: …
Run Code Online (Sandbox Code Playgroud)

ssh ansible ansible-2.x ansible-inventory

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

如何修复“无法匹配提供的主机模式,忽略:bigip”错误,适用于 Ansible,而不是 Tower

我在 VirtualBox 上运行的 ubuntu 16.04 VM 上运行 Ansible Tower v3.4.1 和 Ansible v2.7.6。我运行的剧本在我使用“ansible-playbook”从命令行运行时有效,但当我尝试从 Ansible Tower 运行时失败。我知道我一定在 ansible 塔中有一些配置错误的东西,但我找不到它。

无论我对清单(主机)文件进行什么更改,我都会收到此警告。

$ ansible-playbook 2.7.6

config file = /etc/ansible/ansible.cfg    
configured module search path = [u'/var/lib/awx/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']

ansible python module location = /usr/lib/python2.7/dist-packages/ansible    

executable location = /usr/bin/ansible-playbook
python version = 2.7.12 (default, Nov 12 2018, 14:36:49) [GCC 5.4.0 20160609]

Using /etc/ansible/ansible.cfg as config file

SSH password:     
**/tmp/awx_74_z6yJB4/tmpVlXGCX did not meet host_list requirements**, check plugin documentation if this is unexpected

Parsed /tmp/awx_74_z6yJB4/tmpVlXGCX inventory source with …
Run Code Online (Sandbox Code Playgroud)

ansible ansible-inventory

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

Ansible 清单可以包含另一个清单吗?

我们有一组在应用程序的不同实例之间共享的服务器,并且希望它们的列表成为一个单独的文件,以及其他特定于实例的清单(包括它)。

(我知道,这可以通过动态库存来完成,但可以这么说,这些都是代码,我们希望我们的服务器列表保留数据。)

对于 INI 库存,这是不可能的,但对于 YAML 来说,这是非常接近的。例如,这个答案显示了如何通过!include向 Python 的 YAML 解析器添加处理程序来完成此操作。然后我们可以这样写:

all:
  group1:
    host1:
    host2:
  sharedservers: !include shared-servers.yaml
Run Code Online (Sandbox Code Playgroud)

如何将此功能添加到自己的 Ansible 存储库中——最好不要实现全新的库存插件(尽管从 Ansible 的现有插件继承也可以)?

yaml include ansible ansible-inventory

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