小编toy*_*ian的帖子

AnsibleUndefinedVariable:'ansible.vars.hostvars.HostVarsVars 对象'没有属性

我正在使用 Ansible,在运行我的 playbook 后收到此错误。

这是我的剧本:


---
- name: Set 192.168.122.4 Hostname
  hosts: 192.168.122.4
  gather_facts: false
  become: true
  tasks:
    - name : Name 4
      hostname:
        name:  ansible1.example.com

- name: Set 192.168.122.5 Hostname
  hosts: 192.168.122.5
  gather_facts: false
  become: true
  tasks:
    - name : Name 5
      hostname:
        name: ansible2.example.com

- name: Manage Hosts File
  hosts: all
  gather_facts: true
  become: true
  tasks:
    
    - name: Deploy Hosts Template
      template:
        src: hosts.j2
        dest: /etc/hosts

Run Code Online (Sandbox Code Playgroud)

这是我的模板


# Managed by Ansible
127.0.0.1 localhost
{% for host in groups['all'] …
Run Code Online (Sandbox Code Playgroud)

linux ansible

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

vim中保持光标线垂直居中

我希望在 vim 中光标始终位于屏幕中间。
我不太习惯不断地往下看文件的末尾。
您建议在配置中使用哪些命令来解决问题?

vim

10
推荐指数
2
解决办法
5946
查看次数

npm : 取决于: node-gyp (>= 3.6.2~) 但它不会被安装

我做了以下步骤:

git clone https://github.com/nibtehaz/NORTH-app.git
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install -y nodejs
npm install # which caused the following error
Run Code Online (Sandbox Code Playgroud)

并得到

The following packages have unmet dependencies:
 nodejs : Conflicts: npm
 npm : Depends: node-gyp (>= 3.6.2~) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
Run Code Online (Sandbox Code Playgroud)

我做了建议的事情,但它导致了另一个问题:

sudo apt install node-gyp
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not …
Run Code Online (Sandbox Code Playgroud)

node.js npm

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

将 AnsibleUnsafeText 转换为 int

假设 df 命令返回以下内容。

[john.doe@localhost ~]# df
Filesystem                           1K-blocks     Used Available Use% Mounted on
/dev/sda1                               372607   170989    177862  50% /boot
/dev/sda2                               129774     6994    122780   6% /
Run Code Online (Sandbox Code Playgroud)

借助 Ansible,我可以使用 shell 模块捕获 /dev/sda1 的可用值 (177862)。

[john.doe@localhost ~]# df
Filesystem                           1K-blocks     Used Available Use% Mounted on
/dev/sda1                               372607   170989    177862  50% /boot
/dev/sda2                               129774     6994    122780   6% /
Run Code Online (Sandbox Code Playgroud)

这将显示类型是AnsibleUnsafeText。如果我对 Gather_facts 执行相同操作,也会返回 AnsibleUnsafeText。我不知道如何使类型成为整数而不是 AnsibleUnsafeText。

TASK [standard out] 
ok: [localhost] => {
    "msg": "122780"
}

TASK [type] 
ok: [localhost] => {
    "msg": "AnsibleUnsafeText"
} …
Run Code Online (Sandbox Code Playgroud)

ansible

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

Changed_when 不工作,如果任务结果失败

为什么我无法使用来更改返回changed_when的任务的状态?failedchanged

例如,我有一个自定义模块,允许我检查卷是否存在,如果卷不存在,它将失败。

剧本片段:

---
- name: Example ansible playbook
  tasks:
    - name: Check existence of the volume
      custom_name_space_my_volume:
        name: ABC #name of the volume
      changed_when: true
Run Code Online (Sandbox Code Playgroud)

如果该卷ABC不存在,则结果为failed。但为什么没有change_when: true导致结果返回changed呢?

注意:上面的例子可能没有逻辑意义,因为它没有做一些有用的事情。但我只是想用它作为我的问题的样本。

ansible

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

来自 scikits cross_val_score 的所有类的 f1 分数

我正在使用cross_val_scorefrom scikit-learn (package sklearn.cross_validation) 来评估我的分类器。
如果我f1用于scoring参数,该函数将返回一个类的 f1 分数。为了获得我可以使用的平均值,f1_weighted但我不知道如何获得其他班级的 f1 分数。(准确率和召回率类似)

中的函数sklearn.metrics有一个labels执行此操作的参数,但我在文档中找不到类似的内容。

有没有办法一次获得所有类的 f1 分数,或者至少指定应该考虑的类cross_val_score

python scikit-learn cross-validation

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

提高 Ansible 性能

我正在尝试提高 ansible playbook 的性能。我有一个测试剧本如下:

---
- name: Test
  hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - name: Creating an empty file
      file:
        path: /tmp/hello
        state: touch

    - name: Test
      command: "echo 'Hello, World!' >> /tmp/hello"
      with_sequence: start=1 end=500
      delegate_to: localhost
Run Code Online (Sandbox Code Playgroud)

运行这需要惊人的 57 秒。与执行相同操作的 bash 脚本相比:

测试文件

#!/bin/bash
touch /tmp/hello
for i in {1..500}
do
  sh /home/admin/hello.sh
  echo "This is iteration $i"
done
Run Code Online (Sandbox Code Playgroud)

你好.sh

#!/bin/bash
echo "Hello, World!" >> /tmp/hello
Run Code Online (Sandbox Code Playgroud)

这需要大约 1.5 秒才能运行。

我已经对ansible.cfg文件进行了一些更改

[ssh_connection]
ssh_args = -o ControlMaster=auto …
Run Code Online (Sandbox Code Playgroud)

performance ansible

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

为什么ansible无法连接MySql?

我想使用这些配置部署 MySql 数据库服务器。

\n

这是我部署 MySql 的剧本:

\n
- name: "Inclure le fichier : install_utilitaires_mysql.yaml"\n  include: install_utilitaires_mysql.yaml\n  when: ansible_os_family == "Debian"\n\n- name: "Telechargement d\'un package .deb pour ajouter le repo mysql"\n  get_url:\n    url: https://dev.mysql.com/get/mysql-apt-config_0.8.15-1_all.deb\n    dest: /tmp\n  when: ansible_os_family == "Debian"\n\n- name: "Installation du package .deb"\n  apt:\n    deb: /tmp/mysql-apt-config_0.8.15-1_all.deb\n  when: ansible_os_family == "Debian"\n\n- name: "Mise a jour des sources list : apt update"\n  apt:\n   update_cache: yes\n  when: ansible_os_family == "Debian"\n\n- name: "Installation de MySql"\n  apt:\n    name: mysql-server\n    state: latest\n  when: ansible_os_family …
Run Code Online (Sandbox Code Playgroud)

mysql debian ansible

3
推荐指数
1
解决办法
4211
查看次数

错误:yaml.scanner.ScannerError:扫描简单密钥时

我收到以下错误docker-compose

\n
 docker-compose up -d\nERROR: yaml.scanner.ScannerError: while scanning a simple key\n  in "./docker-compose.yml", line 4, column 1\ncould not find expected ':'\n  in "./docker-compose.yml", line 5, column 1\n
Run Code Online (Sandbox Code Playgroud)\n

这是我的docker-compose.yaml

\n
version: '2.0'\nservices:\n\n\xc2\xa0 #PHP Service\n\xc2\xa0 app:\n\xc2\xa0\xc2\xa0\xc2\xa0 build:\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 context: .\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 dockerfile: Dockerfile\n\xc2\xa0\xc2\xa0\xc2\xa0 image: cloudsigma.com/php\n\xc2\xa0\xc2\xa0\xc2\xa0 container_name: app\n\xc2\xa0\xc2\xa0\xc2\xa0 restart: unless-stopped\n\xc2\xa0\xc2\xa0\xc2\xa0 tty: true\n\xc2\xa0\xc2\xa0\xc2\xa0 environment:\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 SERVICE_NAME: app\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 SERVICE_TAGS: dev\n\xc2\xa0\xc2\xa0\xc2\xa0 working_dir: /var/www/html/\n\xc2\xa0\xc2\xa0\xc2\xa0 volumes:\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 - ./:/var/www/html/\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 - ./php/laravel.ini:/usr/local/etc/php/conf.d/laravel.ini\n\xc2\xa0\xc2\xa0\xc2\xa0 networks:\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 - app-network\n
Run Code Online (Sandbox Code Playgroud)\n

有人可以告诉我我的撰写文件有什么问题吗?

\n

yaml docker-compose

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

ansible即使安装也找不到python包

尝试使用docker-compose该模块在 ansible 剧本中使用docker_compose。请注意,我可以在 ansible 中成功使用 docker 模块,但遇到docker_compose模块问题。我使用纯文本变量进行本地测试,使用 ansible-vault 来存储机密。

在 ansible 文档中,它解释说他们的 docker-compose 模块仅支持 docker-compose 的版本 1 和 2。

这是我的docker-compose.yml

---
version: "2.4"
services:
   my_demo:
       build: .
       networks:
          demo_net:
             ipv4_address: 172.0.1.2
       ports:
          - 8080:8080
       image: demo_image

networks:
    demo_net:
       driver: bridge
       ipam:
         driver: default
         config:
           - subnet: 172.0.1.0/30
             gateway: 172.0.1.1
Run Code Online (Sandbox Code Playgroud)

这是我的main.yml

---
- hosts: localhost
  connection: local
  become: true

  vars:
    - ansible_sudo_pass: password
    - ansible_python_interpreter: /usr/bin/python3

  tasks:
    - name: Docker compose …
Run Code Online (Sandbox Code Playgroud)

ansible docker docker-compose devops

3
推荐指数
1
解决办法
6430
查看次数