使用ansible配置的用户需要太长时间

时间:2016-07-27 08:22:07

标签: ansible ansible-playbook ansible-2.x

我有将用户提供给远程主机的剧本:

    Array
{
    [0] => Array
    {
        [form_id] => 16
        [counId] => 2
        [teleId] => 4
        [name] => somthing
        [dob] => 06/07/2016
        [phone] => somthing
        [mobile] => somthing
        [email] => somthing@somthing.com
        [passport] => 5955686
        [expiry] => 07/07/2016
        [address] => 255505052
        [country] => USA
        [reference] => 
        [friend_name] => 
        [remarks] => 
        [mstatus] => single
        [date_of_marriage] => 
        [age_of_kids] => 
        [kids] => yes
        [visa_ref] => yes
        [visa_country] => canada
        [created] => 2016-07-26 23:54:03
        [updated] => 2016-07-26 23:54:03
        [id] => 24
        [education] => Camb
        [university] => Camb
        [start_year] => 2016-07-2
        [end_year] => 2016-07-2
        [percentage] => 2
        [employer_name] => q
        [designation] => w
        [from] => 15/07/2016
        [to] => 27/07/2016
        [course] => IELTS
        [testdate] => 05/07/2016
        [listening] => 2
        [reading] => 2
        [writing] => 2
        [speaking] => 2
        [total_score] => 33
    }

    [1] => Array
    {
        [form_id] => 16
        [counId] => 2
        [teleId] => 4
        [name] => somthing
        [dob] => 06/07/2016
        [phone] => somthing
        [mobile] => somthing
        [email] => somthing@somthing.com
        [passport] => 5955686
        [expiry] => 07/07/2016
        [address] => 255505052
        [country] => USA
        [reference] => 
        [friend_name] => 
        [remarks] => 
        [mstatus] => single
        [date_of_marriage] => 
        [age_of_kids] => 
        [kids] => yes
        [visa_ref] => yes
        [visa_country] => canada
        [created] => 2016-07-26 23:54:03
        [updated] => 2016-07-26 23:54:03
        [id] => 24
        [education] => DPS
        [university] => DPS2
        [start_year] => 27/07/2016
        [end_year] => 30/07/2016
        [percentage] => 4
        [employer_name] => q
        [designation] => w
        [from] => 15/07/2016
        [to] => 27/07/2016
        [course] => IELTS
        [testdate] => 05/07/2016
        [listening] => 2
        [reading] => 2
        [writing] => 2
        [speaking] => 2
        [total_score] => 33
    }
}

这个单一任务需要110秒,而且非常慢。

t[0] => Array
    {
        [form_id] => 16
        [counId] => 2
        [teleId] => 4
        [name] => somthing
        [dob] => 06/07/2016
        [phone] => 9786356
        [mobile] => 988830505
        [email] => somthing@somthing.com
        [passport] => 5955686
        [expiry] => 07/07/2016
        [address] => 255505052
        [country] => USA
        [reference] => 
        [friend_name] => 
        [remarks] => 
        [mstatus] => single
        [date_of_marriage] => 
        [age_of_kids] => 
        [kids] => yes
        [visa_ref] => yes
        [visa_country] => canada
        [created] => 2016-07-26 23:54:03
        [updated] => 2016-07-26 23:54:03
        [id] => 24
        [education] => oxford,Camb
        [university] => oxford,Camb
        [start_year] => 6,2016-07-2
        [end_year] => 6,2016-07-2
        [percentage] => 2
        [employer_name] => q
        [designation] => w
        [from] => 15/07/2016
        [to] => 27/07/2016
        [course] => IELTS
        [testdate] => 05/07/2016
        [listening] => 2
        [reading] => 2
        [writing] => 2
        [speaking] => 2
        [total_score] => 33
    }

查看full log

我尝试启用流水线操作,但它没有帮助。

我正在使用ansible 2.0.0.2 这是我的ansible.cfg:

---
- hosts: webserver
  remote_user: myuser
  sudo: yes
  tasks:
    - name: Add ssh Users
      authorized_key: user='ubuntu' key="{{ lookup('file', './keys/{{item}}.pub') }}"
      with_items:
        - user1
        - user2
        - user3
        - user4
        - user5
        - user6
        - user7
        - user8
        - user9
        - user10
        - user11
        - user12

2 个答案:

答案 0 :(得分:5)

指定:

ssh_args=-o ForwardAgent=yes

您已取代ansible的默认设置:

ssh_args = -o ControlMaster=auto -o ControlPersist=60s

因此禁用了ssh连接重用。将此选项添加到您的配置中,然后重试。

如果时间增益不够,您可能希望首先在本地连接所有.pub文件,然后以多行字符串作为关键参数执行authorized_key模块一次 - 该模块可以处理它。

答案 1 :(得分:1)

原始问题的

authorized_key任务已针对每个用户执行,并且每次都重新连接到服务器。在这种情况下,每个连接大约需要7秒钟。 首先,我尝试了answer from Konstantin并减少了运行时间到48秒。如果结果可以改善,我很好奇。 我通过使用生成所有ssh密钥的模板成功解决了我的问题:

---
- hosts: webserver
  remote_user: myuser
  sudo: yes
  vars:
    ssh_users: ['user1','user2','user3','user4','user5','user6','user7','user8','user9','user10','user11','user12']
  tasks:
  - name: Add ssh Users
    template:
      dest=/home/myuser/.ssh/authorized_keys
      src=templates/authorized_keys
      owner=myuser
      group=myuser
      mode=600

模板文件如下:

{% for user in ssh_users %}
{{ lookup('file', './keys/'+user+'.pub') }}
{% endfor %}

这是我最后的ansible配置:

[defaults]
hostfile = inventory
host_key_checking = false
roles_path = ./roles
private_key_file = ~/.ssh/id_rsa
deprecation_warnings=False
remote_user = ubuntu
callback_whitelist = profile_tasks
pipelining = true

[ssh_connection]
ssh_args=-o ForwardAgent=yes  -o ControlMaster=auto -o ControlPersist=60s

现在它看起来更快。

$ ansible-playbook -i ./inventory setup_ssh.yaml -vv
Using /vagrant/ansible.cfg as config file
1 plays in setup_backend_test.yaml

PLAY [Configure common packages] ***********************************************

TASK [setup] *******************************************************************
Wednesday 27 July 2016  14:23:48 +0000 (0:00:00.063)       0:00:00.063 ********
ok: [XXX.XXX.XXX.XXX]

TASK [ssh_keys : Add ssh Users] ************************************************
Wednesday 27 July 2016  14:23:54 +0000 (0:00:06.025)       0:00:06.088 ********
changed: [XXX.XXX.XXX.XXX] => {"changed": true, "checksum": "3df874356f41d3dc5592441a86060d2796b4a714", "dest": "/home/myuser/.ssh/authorized_keys", "gid": 1000, "group": "myuser", "md5sum": "4c7d6c58a618a9fbd5e5ed3b29a3e7d3", "mode": "0600", "owner": "myuser", "size": 5357, "src": "/home/myuser/.ansible/tmp/ansible-tmp-1469629434.59-30865046320342/source", "state": "file", "uid": 1000}

PLAY RECAP *********************************************************************
XXX.XXX.XXX.XXX              : ok=2    changed=1    unreachable=0    failed=0

Wednesday 27 July 2016  14:24:02 +0000 (0:00:07.855)       0:00:13.944 ********
===============================================================================
TASK: ssh_keys : Add ssh Users ------------------------------------------ 7.85s
TASK: setup ------------------------------------------------------------- 6.03s