【问题标题】:Run Ansible playbook task with predefined username and password使用预定义的用户名和密码运行 Ansible playbook 任务
【发布时间】:2018-08-16 15:56:22
【问题描述】:

这是我的 ansible 脚本的代码。

---
- hosts: "{{ host }}"
  remote_user: "{{ user }}"
  ansible_become_pass: "{{ pass }}"
  tasks:
    - name: Creates directory to keep files on the server
      file: path=/home/{{ user }}/fabric_shell state=directory

    - name: Move sh file to remote
      copy:

        src: /home/pankaj/my_ansible_scripts/normal_script/installation/install.sh
        dest: /home/{{ user }}/fabric_shell/install.sh



    - name: Execute the script
      command: sh /home/{{ user }}/fabric_shell/install.sh
      become: yes

我正在使用命令运行 ansible playbook>>> ansible-playbook send_run_shell.yml --extra-vars "user=sakshi host=192.168.0.238 pass=Welcome01".

但我不知道为什么会出错

ERROR! 'ansible_become_pass' is not a valid attribute for a Play

The error appears to have been in '/home/pankaj/go/src/shell_code/send_run_shell.yml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

---
- hosts: "{{ host }}"
  ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

请指导,我做错了什么。

提前致谢...

【问题讨论】:

    标签: ansible


    【解决方案1】:

    ansible_become_pass 是一个连接参数,可以设置为变量:

    ---
    - hosts: "{{ host }}"
      remote_user: "{{ user }}"
      vars:
        ansible_become_pass: "{{ pass }}"
      tasks:
        # ...
    

    也就是说,您也可以将 remote_user 移动到变量中(参考整个 list of connection parameters),将其保存到单独的 host_vars- 或 group_vars-file 并使用 Ansible Vault 加密。

    【讨论】:

      【解决方案2】:

      看看这个线程thread Ansible Page。我建议这样使用become_user

      - hosts: all
        tasks:
          - include_tasks: task/java_tomcat_install.yml
            when: activity == 'Install'
            become: yes
            become_user: "{{ aplication_user }}"
      

      尝试不要使用pass=Welcome01

      当与远程机器对话时,Ansible 默认假定您使用的是 SSH 密钥。鼓励使用 SSH 密钥,但也可以在需要时通过提供选项 --ask-pass 使用密码验证。如果使用 sudo 功能并且当 sudo 需要密码时,还需要提供 --ask-become-pass(以前 --ask-sudo-pass 已被弃用)。

      【讨论】:

      • 让我试试@3sky 你建议我的方式。
      • ansible-playbook send_run_shell.yml --extra-vars "user=sakshi host=192.168.0.238" --ask-become-pass
      • 我运行这个命令。更改后的 yaml 文件为 hastebin.com/komuzavusa.coffeescript
      • 但出现错误"msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).\r\n" @3sky
      • --ask-sudo-pass 在您以用户身份登录(使用例如 ssh 密钥)并想要 sudo 时出现。要以普通用户身份登录,请尝试--ask-pass
      猜你喜欢
      • 2023-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-15
      • 2015-02-19
      相关资源
      最近更新 更多