Pankaj Cheema
Pankaj Cheema

Reputation: 1058

Run Ansible playbook task with predefined username and password

This is code of my ansible script .

---
- 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

I am running the ansible playbook using command>>> ansible-playbook send_run_shell.yml --extra-vars "user=sakshi host=192.168.0.238 pass=Welcome01" .

But I don't know why am getting error

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 }}"

Please guide , what I am doing wrong.

Thanks in advance ...

Upvotes: 4

Views: 28050

Answers (2)

techraf
techraf

Reputation: 68639

ansible_become_pass is a connection parameter which you can set as variable:

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

That said, you can move remote_user to variables too (refer to the whole list of connection parameters), save it to a separate host_vars- or group_vars-file and encrypt with Ansible Vault.

Upvotes: 3

3sky
3sky

Reputation: 890

Take a look on this thread thread and Ansible Page. I propose to use become_user in this way:

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

Try do not use pass=Welcome01,

When speaking with remote machines, Ansible by default assumes you are using SSH keys. SSH keys are encouraged but password authentication can also be used where needed by supplying the option --ask-pass. If using sudo features and when sudo requires a password, also supply --ask-become-pass (previously --ask-sudo-pass which has been deprecated).

Upvotes: 1

Related Questions