使用预定义的用户名和密码运行Ansible剧本任务

时间:2018-08-16 06:43:54

标签: ansible

这是我的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剧本>>> 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 }}"

请指导,我在做什么错了。

预先感谢...

2 个答案:

答案 0 :(得分:1)

看看该线程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)。

答案 1 :(得分: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文件中,并使用Ansible Vault进行加密。 / p>

相关问题