使用 Ansible/awx 切换用户以委托托管库存外

Switching user for delegation to host outside of inventory with Ansible/awx

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

由于我必须使用不同的用户来访问 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

当 运行 时,剧本在尝试使用 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/ 目录有关。 是否有一种 better/more elegant/working 方法可以使用 ssh 密钥连接到不同的服务器来移动文件? 我知道一种可能性是只使用 shell 模块的 scp,但这总是感觉有点老套。

(有点)使用带有 Ansible 保险库的 hostvars 中的加密变量解决了问题。 如何到达:

正在加密密码:

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

中无法完成
ansible-vault encrypt_string "password"

系统将提示您输入 encrypt/decrypt 的密码。 如果您为 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”的新凭据和您之前用于加密字符串的密码。 现在,您需要做的就是将凭据添加到您的作业模板(模板可以有一个“普通”凭据(机器、网络等)和多个保管库)。

剧本然后自动访问保管库凭据以解密清单中的字符串。

用于获取开关信息并在服务器上放置模板文件的手册

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

  • 收集清单中所有开关的资料
  • 使用模板将所有事实写入.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