我想将Ansible用作另一个Python软件的一部分.在该软件中,我有一个主机列表及其用户/密码.
有没有办法将SSH连接的用户/传递传递给Ansible ad-hoc命令或以加密方式将其写入任何文件?
或者我是否理解这一切都是错误的,唯一的方法是使用SSH认证?
在构建Vagrant项目时,运行临时Ansible任务而不是将测试命令添加到剧本中会很有帮助.我已经尝试了几种针对VM的方法,但不断收到此错误:
default | FAILED => SSH encountered an unknown error during the connection. We
recommend you re-run the command using -vvvv, which will enable SSH debugging
output to help diagnose the issue
Run Code Online (Sandbox Code Playgroud)
我正在使用Vagrant生成的Ansible清单文件,该框有一个工作主机名.如何使用单个Ansible任务来定位我的Vagrant VM?
我有一些困难使Ansible配置适应我的环境.
我的测试环境:
我的ansible主机库存如下所示:
[servers]
server1 ansible_ssh_host=192.168.0.1 ansible_sudo=true ansible_ssh_user=foo ansible_sudo_user=bar
Run Code Online (Sandbox Code Playgroud)
我有一个用于foo用户的SSH密钥.
当我使用条形码密码尝试这个ad hoc命令(或任何其他命令)时:
ansible server1 -m raw -a "echo test > /etc/testfile" --ask-sudo-pass
server1 | FAILED => Incorrect sudo password
Run Code Online (Sandbox Code Playgroud)
然后,如果我使用foo密码执行相同的命令:
ansible server1 -m raw -a "echo test > /etc/testfile" --ask-sudo-pass
Sorry, user foo is not allowed to execute '/bin/bash -c echo SUDO-SUCCESS-rlpfhamukjnsfyokqbjpbttviiuildif; echo test > /etc/testfile' as bar on server1.
Run Code Online (Sandbox Code Playgroud)
所以Ansible肯定使用foo作为sudo用户而不是我指定的bar.有没有办法强制使用bar而不是foo?我真的不明白Ansible上的sudo功能,即使我使用相同的用户一切(SSH和条形码密码)ansible给我回报:
server1 | FAILED | rc=1 >>
echo test > /etc/testfile : Permission denied
Run Code Online (Sandbox Code Playgroud)
当我在主机上记录为条形并执行"sudo echo …
我是 Ansible 的初学者,并尝试使用Ad-Hoc 命令将字符串写入文件,我正在尝试使用该replace
模块。我要写入的文件是/etc/motd/
.
ansible replace --sudo /etc/motd "This server is managed by Ansible"
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激谢谢!
当我运行这个:
ansible host -u myuser -a "sudo su - otheruser -s /bin/bash"
我收到警告:
[WARNING]: Consider using 'become', 'become_method', and 'become_user' rather
than running sudo
host | SUCCESS | rc=0 >>
Run Code Online (Sandbox Code Playgroud)
但是什么是等效的使用become
for sudo su - otheruser -s /bin/bash
?
我试过:
ansible host -u myuser --become --become-user otheruser -a "/bin/echo hello"
Run Code Online (Sandbox Code Playgroud)
但我得到:
host | FAILED! => {
"changed": false,
"module_stderr": "Shared connection to host closed.\r\n",
"module_stdout": "sudo: a password is required\r\n",
"msg": "MODULE FAILURE",
"rc": 1
}
Run Code Online (Sandbox Code Playgroud)
PS Ansible 版本:2.6.4
是否可以使用ansible_facts
Ansible adhoc命令中通常包含的内容?
例如,我/tmp/myFile
所有服务器上都有一个文件,我想这样做:
ansible all -i [inventory file] -m fetch -a "src=/tmp/myFile dest=myFile-[insert ansible_hostname here]"
Run Code Online (Sandbox Code Playgroud)
无需为此编写一本完整的剧本。
Ansible v2.2.1.0
我想收集有关在我的inv/remote_hosts
文件中定义的远程主机的 Ansible 事实,如下所示:
[remote-host-0]
10.22.123.234
[remote-host-1]
10.22.234.123
Run Code Online (Sandbox Code Playgroud)
如何运行 setup 命令并告诉它为 remote-host-0 收集事实?
$ ansible ??? -m setup ???
Run Code Online (Sandbox Code Playgroud)
如果我只是这样做:
$ ansible remote-host-0 -m setup
Run Code Online (Sandbox Code Playgroud)
我得到:
错误!尝试将“/path_to_my_ansible_dir/ansible//hosts”读作 YAML:加载 YAML 时出现语法错误。
它正在寻找错误的主机文件,但我收到了 YAML 错误。
我想格式化 Ansible 输出:
my_local | SUCCESS | rc=0 >>
116G
my_local2 | SUCCESS | rc=0 >>
116G
Run Code Online (Sandbox Code Playgroud)
到
my_local >> 116G
my_local2 >> 116G
Run Code Online (Sandbox Code Playgroud)
是否可以?
inventory.ini
:
my_local ansible_connection=local
my_local2 ansible_connection=local
Run Code Online (Sandbox Code Playgroud)
重击命令:
ansible all -i inventory.ini -u root -m shell -a "df -h / | tail -1 | awk '{print \$4}'"
Run Code Online (Sandbox Code Playgroud) 我想在EC2实例列表上运行ansible adhoc命令.我希望ansible按顺序运行它,但是ansible随机运行它们.例如:
13:42:21 @cnayak ansible :? ansible aws -a "hostname"
ec2 | SUCCESS | rc=0 >>
ip-172-31-36-255
ec3 | SUCCESS | rc=0 >>
ip-172-31-45-174
13:42:26 @cnayak ansible :? ansible aws -a "hostname"
ec3 | SUCCESS | rc=0 >>
ip-172-31-45-174
ec2 | SUCCESS | rc=0 >>
ip-172-31-36-255
Run Code Online (Sandbox Code Playgroud)
有什么方法让它们按顺序运行?
在剧本中,我尝试提取变量"ansible_hostname"的最后一个字符.
我尝试使用regex_replace过滤器来做到这一点,但没有任何作用.
我用这个ad-hoc命令简化了我的脚本:
ansible localhost -m debug -a"msg = {{'devserver01'| regex_replace('[0-9] {1} $','\ 1')}}"
我想提取最后一个字符:'1'.
我正在使用Ansible 2.0.
我知道可以为每个模块一个接一个地运行多个即席命令,并使用剧本。
剧本:
- hosts: webservers
tasks:
- name: create .ssh dir
file: path ~/.ssh state=directory
- name: copy pub key
copy: src:~/.ssh/id.rsa_pub dest=~/.ssh/authorized_keys
Run Code Online (Sandbox Code Playgroud)
我希望以上内容在一行中使用即席执行。有可能这样做吗?
我试图执行这个命令:
ansible somegroup -m raw -a "docker ps -a" --ask-pass -K --become-user root
Run Code Online (Sandbox Code Playgroud)
但错误是:
尝试连接到 unix:///var/run/docker.sock 上的 Docker 守护进程套接字时,权限被拒绝:
如何更正 ansible adhoc 的 set 指令以在 root 下使用sudo su -
?