【问题标题】:Switching user for delegation to host outside of inventory with Ansible/awx使用 Ansible/awx 将委托用户切换到库存之外的主机
【发布时间】:2020-07-17 10:40:10
【问题描述】:

我正在尝试使用 Ansible 2.8.4 和 awx 执行以下操作:

  • 从 Cisco IOS 设备中阅读一些事实(有效)
  • 使用模板将结果放入本地文件(工作)
  • 将生成的文件复制/移动到其他服务器

由于我必须使用不同的用户来访问 IOS 设备和服务器,而且这些服务器不是用于剧本的清单的一部分,因此我尝试使用 become_user 和 delegate_to 来实现这一点。 初始用户(在 awx 模板中定义)被允许连接到 IOS 设备,而 different_user 可以使用 ssh 私钥连接到服务器。

剧本:

---
  - name: Read Switch Infos
    hosts: all
    gather_facts: no
    tasks:

      - name: Gather IOS Facts
        ios_facts:
      - debug: var=ansible_net_version
      - name: Set Facts IOS
        set_fact:
          ios_version: "{{ ansible_net_version }}"

      - name: Create Output file
        file: path=/tmp/test state=directory mode=0755
        delegate_to: 127.0.0.1
        run_once: true

      - name: Run Template
        template:
          src: ios_firmware_check.j2
          dest: /tmp/test/output.txt
        delegate_to: 127.0.0.1
        run_once: true

      - name: Set up keys
        become: yes
        become_method: su
        become_user: different_user
        authorized_key:
          user: different_user
          state: present
          key: "{{ lookup('file', '/home/different_user/.ssh/key_file') }}"
        delegate_to: 127.0.0.1
        run_once: true
      - name: Copy to remote server
        remote_user: different_user
        copy:
          src: /tmp/test/output.txt
          dest: /tmp/test/output.txt
        delegate_to: remote.server.fqdn
        run_once: true

运行时,playbook 在尝试使用 ssh 密钥访问主目录的设置密钥任务中失败:

TASK [Set up keys] *************************************************************
task path: /tmp/awx_2206_mz90qvh9/project/IOS/ios_version.yml:23
 [WARNING]: Unable to find '/home/different_user/.ssh/key_file' in expected paths
(use -vvvvv to see paths)
File lookup using None as file
fatal: [host]: FAILED! => {
    "msg": "An unhandled exception occurred while running the lookup plugin 'file'. Error was a <class 'ansible.errors.AnsibleError'>, original message: could not locate file in lookup: /home/different_user/.ssh/key_file"
}

我假设我的错误与哪个用户试图访问哪个设备上的 /home/ 目录有关。 有没有更好/更优雅/有效的方式来使用 ssh 键连接到不同的服务器来移动文件? 我知道一种可能性是只使用 shell 模块进行 scp,但这总是感觉有点 hacky。

【问题讨论】:

    标签: ansible ansible-awx


    【解决方案1】:

    (有点)在带有 Ansible 保险库的主机变量中使用加密变量解决。 如何到达那里:

    加密密码:

    这需要从任何安装了 Ansible 的命令行中完成,由于某种原因,这不能在 tower/awx 中完成

    ansible-vault encrypt_string "password"
    

    系统将提示您输入密码以进行加密/解密。 如果您为 Cisco 设备执行此操作,则需要使用此方法加密 ssh 和启用密码。

    向库存添加加密密码

    为了测试,我将它放在 hostvars 中用于单个开关,应该可以将它放入 groupvars 并在多个开关上使用它。

    ansible_ssh_pass 应该是访问交换机的密码,ansible_become_pass 是启用密码。

    ---
    all:
      children:
        Cisco:
          children:
            switches:
    switches:
      hosts:
        HOSTNAME:
          ansible_host: ip-address
          ansible_user: username
          ansible_ssh_pass: !vault |
              $ANSIBLE_VAULT;1.1;AES256
              [encrypted string]
          ansible_connection: network_cli
          ansible_network_os: ios
          ansible_become: yes
          ansible_become_method: enable
          ansible_become_pass: !vault |
              $ANSIBLE_VAULT;1.1;AES256
             [encrypted string]
    

    将保管库密码添加到 tower/awx

    添加凭据类型为“Vault”的新凭据以及您之前用于加密字符串的密码。 现在,您需要做的就是将凭证添加到您的工作模板(模板可以有一个“正常”凭证(机器、网络等)和多个保管库)。

    该剧本然后自动访问保险库凭据以解密库存中的字符串。

    获取 Switch Infos 并在服务器上放置模板文件的手册

    剧本现在看起来如下所示,并执行以下操作:

    • 收集库存中所有交换机的信息
    • 使用模板将所有事实写入 .csv,将文件保存在 ansible 主机上
    • 使用不同的用户将所述文件复制到不同的服务器

    模板配置了能够访问服务器的用户,用于访问交换机的用户使用密码存储在清单中,如上所示。

    ---
      - name: Read Switch Infos
        hosts: all
        gather_facts: no
        tasks:
          - name: Create Output file
            file: path=/output/directory state=directory mode=0755
            delegate_to: 127.0.0.1
            run_once: true
          - debug:
              var: network
          
          - name: Gather IOS Facts
            remote_user: username
            ios_facts:
          - debug: var=ansible_net_version
          
          - name: Set Facts IOS
            set_fact:
              ios_version: "{{ ansible_net_version }}"
    
          - name: Run Template
            template:
              src: ios_firmware_check.csv.j2
              dest: /output/directory/filename.csv
            delegate_to: 127.0.0.1
            run_once: true
          
          - name: Create Destination folder on remote server outside inventory
            remote_user: different_username
            file: path=/destination/directory mode=0755
            delegate_to: remote.server.fqdn
            run_once: true
          
          - name: Copy to remote server outside inventory
            remote_user: different_username
            copy:
              src: /output/directory/filename.csv
              dest: /destination/directory/filename.csv
            delegate_to: remote.server.fqdn
            run_once: true
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-22
      • 2019-08-23
      • 1970-01-01
      • 2019-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多