我目前已成功使用Ansible在AWS专用子网中的主机上运行任务,以下内容已在其中group_vars设置:
ansible_ssh_common_args: '-o ProxyCommand="ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -W %h:%p -q ec2-user@bastionhost@example.com"'
Run Code Online (Sandbox Code Playgroud)
一切正常。
对于不在私有子网中的Windows实例,可以进行以下group_vars工作:
---
ansible_user: "AnsibleUser"
ansible_password: "Password"
ansible_port: 5986
ansible_connection: winrm
ansible_winrm_server_cert_validation: ignore
Run Code Online (Sandbox Code Playgroud)
现在,仅通过使用ProxyCommand将无法使Ansible部署到堡垒后面的Windows服务器上就行了-据我所知。我相信尽管有一个可以使用的新协议/模块psrp。
我想group_vars我的Windows主机需要更改为以下内容:
---
ansible_user: "AnsibleUser"
ansible_password: "Password"
ansible_port: 5986
ansible_connection: psrp
ansible_psrp_cert_validation: ignore
Run Code Online (Sandbox Code Playgroud)
如果仅对公开可用的实例进行上述更改(并且不尝试通过堡垒连接),则我的任务似乎正常运行:
Using module file /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ansible/modules/windows/win_shell.ps1
<10.100.11.14> ESTABLISH PSRP CONNECTION FOR USER: Administrator ON PORT 5986 TO 10.100.11.14
PSRP: EXEC (via pipeline wrapper)
Run Code Online (Sandbox Code Playgroud)
我知道必须进行更多更改,然后才能在堡垒后面的Windows服务器上尝试此操作,但无论如何都要运行它以查看出现的错误,以为下一步提供提示。在堡垒服务器后面的实例上运行此命令时,结果如下:
Using module file /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ansible/modules/windows/setup.ps1
<10.100.11.14> ESTABLISH PSRP CONNECTION FOR USER: Administrator …Run Code Online (Sandbox Code Playgroud) 我尝试在我的剧本中通过 ansible 安装 boto3。
我试图在我的主机上创建一个新用户。
- name: "test user"
hosts: test
tasks:
- name: "install boto3"
pip:
name: boto3
executable: pip3
Run Code Online (Sandbox Code Playgroud)
我收到了这条消息:
{"changed": false, "msg": "Unable to find any of pip3 to use. pip needs to be installed."}
Run Code Online (Sandbox Code Playgroud) 我希望能够根据本地目录中的文件列表进行校验和。然后能够获取这些文件校验和并将其与远程系统上相同文件的校验和进行比较。
我知道我可以通过以下方式获得
# Local File
- stat:
path: "{{ playbook_dir }}/roles/common/files/myfile.dat"
checksum_algorithm: sha1
delegate_to: localhost
run_once: true
register: localsha_result
# Remote file
- stat:
path: "{{ rmt_dest_dir }}/myfile.dat"
checksum_algorithm: sha1
register: sha_result
Run Code Online (Sandbox Code Playgroud)
我试图遍历我想要校验和的文件:
# Gather Files
- name: gather names of files
local_action: shell ls {{ playbook_dir }}/roles/common/files/*.dat | awk -F '/' '{ print $NF }'
register: datfiles
# Local File
- stat:
path: "{{ playbook_dir }}/roles/common/files/{{ item }}"
checksum_algorithm: sha1
with_items: "{{ datfiles.stdout_lines }}"
delegate_to: localhost
run_once: true
register: …Run Code Online (Sandbox Code Playgroud) 我有一个list实际上是 a 的键列表dict。我想得到一个连接字符串,并dict在这个list键上过滤并在模块选项中使用它。
我在这里的用例是拥有公钥名称列表的用户生成一个authorized_keys 文件。
1 ---
2 - hosts: localhost
3 become: false
4 vars:
5 pub_keys:
6 key01: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ[…]5/ someuser@somehost
7 key02: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ[…]ea otheruser@somewher
8 key03: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ[…]dN anyser@anyhost
9 users:
10 root:
11 home: /root
12 shell: /bin/bash
13 authorized_keys:
14 - key01
15 mgmtusr:
16 home: /home/mgmtusr
17 shell: /bin/bash
18 authorized_keys:
19 - key01
20 - key02
21 - key03
22
23 tasks:
24 - …Run Code Online (Sandbox Code Playgroud)