美文网首页收藏
【ansible】ansile基于动态主机安装

【ansible】ansile基于动态主机安装

作者: Bogon | 来源:发表于2022-03-17 00:01 被阅读0次

    $ cat install_harbor.yml

    - hosts: localhost
      gather_facts: false
      connection: local
      tasks:
      - add_host:
          name: "{{ item }}"
          group: "harbor"
        with_items: "{{ hosts.split(',') }}"
    
    - name: install harbor
      gather_facts: true
      hosts: "harbor"
      user: "{{ ansible_ssh_user | default(test) }}"
    
      vars:
        ADMIN_PASSWORD: "{{ admin_password|default('Admin@123') }}"
        BASE_DIR: "{{ path|default('/opt/harbor') }}"
        DATA_DIR: "{{ BASE_DIR }}/data"
        CONFIG_DIR: "{{ BASE_DIR }}/harbor"
        DOMAIN: "{{ domain|default('www.example.com') }}"
        PROJECT_LIST: "{{ project_list|default('') }}"
        USERNAME: "{{ username|default('') }}"
        PASSWORD: "{{ password|default('') }}"
        SCHEME_TYPE: "{{ scheme_type|default('http') }}"
    

    注:vars处定义的变量既可以被继承,也可以在此role其他文件中。


    image.png

    安装命令(一行式):

    $ ansible-playbook install_harbor.yml -e '{"hosts":"192.168.1.110", "path":"/opt/harbor", "admin_password":"Admin@123", "domain":"www.example.com","project_list":"test01,test02,test03", "username":"test", "password":"Test@123", "scheme_type":"http", "ansible_ssh_user":"test", "ansible_become":"yes","ansible_become_user":"root", "ansible_become_method":"sudo", "ansible_become_pass":"123456"}'
    

    安装命令(美化式):

    $ ansible-playbook install_harbor.yml -e  '{
        "hosts":"192.168.1.110",
        "path":"/opt/harbor",
        "admin_password":"Admin@123",
        "domain":"www.example.com",
        "project_list":"test01,test02,test03",
        "username":"test",
        "password":"Test@123",
        "scheme_type":"http",
        "ansible_ssh_user":"test",
        "ansible_become":"yes",
        "ansible_become_user":"root",
        "ansible_become_method":"sudo",
        "ansible_become_pass":"123456"
    }'
    

    参考

    ansible.builtin.add_host
    Add a host (and alternatively a group) to the ansible-playbook in-memory inventory
    https://docs.ansible.com/ansible/latest/collections/ansible/builtin/add_host_module.html

    Ansible add_host没有添加到hosts文件
    https://www.thinbug.com/q/31350479

    关于yml配置文件的json转换
    https://www.jianshu.com/p/d683f9a71b40

    相关文章

      网友评论

        本文标题:【ansible】ansile基于动态主机安装

        本文链接:https://www.haomeiwen.com/subject/isjmdrtx.html